How to directly call and use swig to invoke C++ Library by python
Editor to share with you how python directly calls and uses swig to call C++ library. I hope you will get something after reading this article. Let's discuss it together.
C++ is faster than python,python and easier to write. Most of the time, you don't want to rewrite the existing C++ code with python, so it's natural to think of calling c or clocked codes with python, which is the best of both worlds.
However, according to these blogs, python can only call c, and additional processing is required for C++ code if it needs to be called cymbal codes.
First, python calls the c code:
/ / gcc-g-o libpycall_c.so-shared-fPIC pycall_c.c#include # include int foo (int a, int b) {printf ("you input% d and% d\ n", a, b); return astatb;}
Be sure to use gcc to compile here. If you use Gmail +, it will become C++. Python cannot call cymbals directly! (I have made a mistake here for a long time because I am using Gmail +.)
Import ctypes lib = ctypes.CDLL (". / libpycall_c.so") lib.foo (1,3) print'* finish***'
It can be seen that the way python calls c is very straightforward. When calling C++, use extern + to compile the function or method in the code that generates the C dynamic library, you need to use the function "C" to compile.
/ / int +-g-o libpycall.so-shared-fPIC pycall.c#include using namespace std;int foo (int a, int b) {cout