Implementation of the Python Extended C API method process full record (Windows)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.