First step: Install the compiler
Recommended use of MinGW, the most convenient to use, you can avoid a variety of difficult to remember and do not understand the settings.
Download just install the GCC section in it, and add the folder where the compiler is located under the environment variable path, for example:
PAH =%path%;c:\mingw\bin
Step Two: Install Python
Recommended to use PYTHONXY, installation is most convenient, save a lot of unnecessary trouble.
Step three: Write a test code
The basic method is: C function +c API wrapper, static array, module initialization
Pythonc.c#include <python.h> #include <stdio.h>void hello_pythoncapi (void) {printf ("Hello Python");} Static pyobject* Pythoncapi (Pyobject *self,pyobject*args) {char *inargs = NULL; Pyarg_parsetuple (args, "s", &inargs);p rintf ("%s\n", Inargs); Hello_pythoncapi (); return Pystring_fromformat (" Hello Pyhton C API ");} Static Pymethoddef methods[]={{"Pythoncapi", Pythoncapi,meth_varargs, "Test python extension"},{NULL, null}};/*__ Declspec (dllexport) */void Initpythonc (void)/*the string after "init" must be same with code file */{py_initmodule ("Pytho NC ", methods);/*the 1st parameter string must be same with code file */}
Fourth Step: Compiling
Open cmd and run the following command
GCC C:\mingw\pythonc.c-shared-ic:\python27\include-lc:\python27\libs-lpython27-o pythonc.pyd
The current path in the diagram is C:\Python27\libs, and after the compilation is successful, the PYTHONC.PYD will be saved under this path.
Fifth step using the extension library
Copy the Pythonc.pyd to the Lib\site_packages folder under the Python path, and you can use import to export the module and call the Pythoncapi () function.
Description
-ic:\python27\include used to indicate the folder where the header file Python.h resides
-lc:\python27\libs and-lpython27 indicate the folder and library file name (libpython27.a under Windows) for the Python C API function library
Reference:
Http://www.linuxidc.com/Linux/2012-02/55038.htm
Http://oldwiki.mingw.org/index.php/Python%20extensions
https://docs.python.org/2/extending/extending.html#
Implementation of the Python Extended C API method process full record (Windows)