wrap_python_cpp_example
Simple example of compiling C++ shared library (.so file) and wrapping with Python.
In this example we use g++
and python3
on a Unix system.
Step 1 - Compiling C++
Generate a file file.cpp
containing the following:
#include <iostream>
using namespace std;
extern "C" // makes C++ functions accessible
{
int my_function(int arg){
cout<<"In C++ my_function. You passed me: "<<arg<<endl;
return arg*2;
}
} // end extern "C"
Compile object file (.o) from .cpp file
g++ -c -Wall -Werror -fPIC file.cpp -o file.o
Generate shared library file (.so)
g++ file.o -shared -o libfile.so
Your directory structure should now look like:
└── my_folder/
├── file.cpp
├── file.o
└── libfile.so
Step 2 - Wrapping with Python
Generate a file file.py
containing the following:
import ctypes
# load shared library file
c_lib = ctypes.CDLL("./libfile.so")
# now we can access the functions defined within `extern "C"{}`
a = c_lib.my_function(21)
print("a = {}".format(a))
Your directory structure should now look like:
└── my_folder/
├── file.cpp
├── file.o
└── file.py
└── libfile.so
Execute the Python file
python file.py
output:
In C++ my_function. You passed me: 21
a = 42