Python calls C++__python

Source: Internet
Author: User

Find some Python call C + + method, found that Python provides ctypes this package, it is easy to use Python directly invoke the interface of C language implementation, which makes the development of the workload greatly reduced, but cTYPES is based on libffi implementation of the Cross-language encapsulation, Performance may be slower than using C extending. Reproduced under the original link: http://blog.csdn.net/kuaile123/article/details/11024873


Because Python's ctypes makes it easy for Python to invoke a dynamic-link library, it invokes a C + + program. Use GCC to compile C + + files as dynamic-link library DLLs or so files, and then use Python to invoke them.

1. Download gcc

System for Win7 X32, download windows version mingw, download address: http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/

Download Download mingw-get-setup.exe (86.5 kB) (Note that this is the Win32 version)

To install:

Select the default installation directory C:\MinGW;

Select the install component, if this window does not appear, click the Quick Launch bar, click MinGW Installation Manager, and choose Install the GUN C + + Compiler and the GUN objectiv-c.

Exit after completion and add C:\MinGW\bin to environment variables. When Setup is complete, Windows does not seem to automatically update the environment variables unless the machine is restarted, so we can set the PATH (such as set path=c:\) in the command line below, and then exit the command line, and the system environment variable is forced to flush.

Click cmd, enter g++ v to view the compiler version.

2. Compile C + +

Create a new C + + file: such as Test1.cpp [HTML] view plain copy #include <iostream> using namespace std;     void Foo2 (int a,int b) {cout<<a<< "" <<b<<endl;      //compile C + +, be sure to add extern "C", note that C is uppercase, the lowercase will not recognize extern "C" {void Cfoo2 (int a,int b) {foo2 (a,b); }     }

In the CMD window, CD to the directory where the file is located, enter: g++-o test1.so-shared-fpic test1.cpp

-shared This option specifies that a dynamic connection library is generated (which allows the connector to generate an export symbol table of type T, and sometimes a weakly connected W-type export symbol) without which the external program cannot connect. Equivalent to an executable file;
-fpic: For code that is compiled as location independent, the compiled code is location-dependent without this option, so dynamic loading is a way to copy the code to meet the needs of different processes, but not the purpose of real code segment sharing.

will produce a test1.so dynamic link library file.

3, Python call

Write Python files, such as test1.py [HTML] view plain copy import ctypes ll = Ctypes.cdll.LoadLibrary lib = ll ("./test1. So ") Lib.cfoo2 (3, 4)

Run python files python test1.py

Run successfully display: 1 3

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.