If you need to call third-party libraries written in C + + with Python, you only need a scripting language to glue them. At this point, the Python ctypes makes it easy to implement calls.
StackOverflow on the topic of calling, C + + from Python describes cTYPES's simplest way to get started, as summarized below:
- If it is a C function library, then load the library directly, and then call it;
- If it is a C + + function library, you need to encapsulate a function for C using the extern keyword, that is, to hide the class in some C-style functions, and then use extern to mark these functions for easy external invocation.
In these two methods, understand the cTYPES call C + + library method, will use cTYPES call C function library, the basic method of C + + library is as follows.
For example, there is a C + + class Foo:
class Foo Public void Bar () {std::cout << "Hello" << Std::endl; }};
Then encapsulate the following C-style interface functions:
extern Foo return New Foo void Foo_bar (foo* foo) {foo->bar (); }}
Compile the above code into a dynamic-link library:
g++-c-fpic foo.cpp-o foo.og++-shared-wl,-soname,libfoo.so-o libfoo.so foo.o
, and then use the Python code to call this class, you can write the above two C interface functions into a Python class, or directly call:
From ctypes import Cdll lib = cdll. LoadLibrary ('./libfoo.so ') class Foo (object): Def __init__ (self): Self.obj = lib. Foo_new () def bar (self): lib. Foo_bar (Self.obj)
, and then you can call this Python class in a Python script:
f = Foo () f.bar () #and you'll see ' Hello ' on the screen
The method of using Python cTYPES under Windows is the same as above, but there are two points to note:
- When writing Python code, it is best to use absolute paths to load when you start linking the required dynamic-link libraries to reduce the probability of errors and speed up debugging
When I have written the above code as described above, when you run the script, you will be prompted with the following error message:
$ python Linkcpp.pytraceback (most recent call last): File "linkcpp.py", line 2, <module> lib = cdll. LoadLibrary ('./linkexample ') File "C:\Python27\lib\ctypes\__init__.py", line 431, in LoadLibrary return self . _dlltype (name) File "C:\Python27\lib\ctypes\__init__.py", line 353, in __init__ self._handle = _dlopen (self. _name, Mode) Windowserror: [Error 126]
This is because I used this code in my Code to import a dynamic-link library:
From ctypes import Cdll lib = cdll. LoadLibrary ('./linkexample ')
If you replace the phrase./linkexample with the absolute path e:/pythoncode/linkcpp/linkexample under Windows, there is no error message. Of course, you can also find the link library by replacing the./linkexample directly with Linkexample.
So, in the beginning, use the absolute path to make sure that you don't get stuck with the problem of being able to find a link library.
When running the above script, there is an error in Windowserror: [Error 126], which is nothing more than two reasons:
- Your DLL is not loaded correctly;
- Other DLLs that your DLL relies on are not found or failed to load.
In addition, note that Windows because the library is divided into Lib and dll two files, so you can only enter the name of the library, for example, you want to link LinkExample.dll library, you can only need to declare the link named Linkexample Library in cTYPES.
- If the library is written in C + +, you need to use the extern keyword, which is the same as the general C + + library header file for the call
In the extern declaration function, you can use keywords that are not in C + +, such as my function declares:
extern int linkexample (constintconstChar* name);
The above code can be run from a Python call.
Extern:extern can be placed before a variable or function to represent the definition of a variable or function in another file, prompting the compiler to find its definition in other modules when it encounters this variable or function. In addition, extern can also be used to specify links.
Achieve it Yourself
C++
g++ compiling g++-o libpycallclass.so-shared-fpic ex.cpp
#include <iostream>using namespacestd; classTestLib { Public: voiddisplay (); voidDisplayinta); }; voidTestLib::d isplay () {cout<<"First display"<<Endl; } voidTestLib::d isplay (inta) {cout<<"Second Display:"<<a<<Endl; } extern "C"{TestLib obj; voiddisplay () {obj.display (); } voidDisplay_int () {Obj.display (2); } }
Python calls
Import ctypes = ctypes.cdll.LoadLibrary = SO ("./libpycallclass.so") Print'display ()' lib.display () Print ' display (+) ' lib.display_int (100)
Resources:
Stackoverflow.com Calling-c-c-from-python
Python calls the C + + related tool: Boost.python
Two times forwarding: http://blog.csdn.net/suwei19870312/article/details/18181675
Python as a glue and C + +