Interfacing CPP and Python using Python Boost
Sometimes there will be situations where we need to pass data between CPP and Python codes. We can use Boost. Python to interface between CPP and Python. In this note, let’s see how to call CPP functions from Python and transfer Numpy arrays between them.
Test System Configuration
- Ubuntu 20.04 LTS on WSL 1, Windows 10
- Python 3.8
- Boost 1.71 [To see Boost version, open up
/usr/include/boost/version.hpp
and look for#define BOOST_LIB_VERSION
]
Install dependencies, if needed
sudo apt-get install libboost-all-dev
sudo apt install python-dev
orsudo apt-get install python3-dev
depending upon the Pythonsudo apt install libboost-numpy-dev
Find out library names and their paths
We need the library names and their paths for linking.
- Python - Find out where the
pyconfig
is [usefind /usr/include/ -name pyconfig*
]. In my system it was at/usr/include/python3.8/
. - Boost Python - use
find /usr/lib -name libboost*
. In my system, boost_python library was at/usr/lib/x86_64-linux-gnu/libboost_python38
. So the name of the library isboost_python38
. - Boost Numpy - Just like Boost Python, I found out libboost_numpy was at
/usr/lib/x86_64-linux-gnu/libboost_numpy38
, and hence the library name wasboost_numpy38
Code
We need to create a shared library (.so
) using Boost by compiling cpp
code and linking existing libraries (boost_python
, python
, boost_numpy
).
CPP header file - sample_interface.h
CPP definition file - sample_interface.cpp
Generating shared library (.so) file
g++ sample_interface.cpp -I/usr/include/python3.8/ -lboost_python38 -lpython3.8 -lboost_numpy38 -fPIC --shared -o cpp_interface.so
Python file - calling_cpp_from_python.py
All of these snippets are available here.
Acknowledgement
Reference: