Exposing classes and functions defined in C++ libraries to Python is now possible in comma by creating C++/Python bindings with Boost.Python.
To illustrate this new capability, bindings for the C++ class
format and its member function
size() declared in
csv/format.h have been defined in
and added to cmake:
Build comma with
BUILD_CPP_PYTHON_BINDINGS=ON, then open a python interpreter and enter the following commands:
size() outputs binary size corresponding to the format string that was passed to the format object
f on construction.
Under the hood
The bindings are placed inside a shared library and saved in the file
csv.so, which is then installed in
comma/cpp_bindings along with the other Python modules. On Ubuntu, it will usually be
/usr/local/lib/python2.7/site-packages/comma/cpp_bindings/csv.so. Note that the name of the module used as a parameter for
BOOST_PYTHON_MODULE macros has to match the name of the shared library declared in the cmake file, e.g.
csv in the above example.
The bindings are exposed as a shared library and hence one is limited to building comma with shared libraries or ensuring that all required static libraries have been compiled with -fPIC. Attempting to link with static libraries without position independent code may cause linking to fail or link with shared libraries instead.