I'm looking at using python (CPython) in my program to both allow user scripting in my environment and to allow me to use pyside, the qt bindings for c++ to create the GUI for my application. These can be effectively separated with the idea that the GUI python code can later be compiled away for speed (if that would be possible).
I'm very new to python and i'm really looking for the most efficient way to generate solid bindings with the absolute minimum of additional code to maintain as the bindings are likely to change often as the project evolves. I need it so that python classes extend c++ classes that have virtuals.
I have already looked into PyBindGen and it chokes too often on things in my library for it to be practically useful.
Any help/advice/links/workflows you recommend in this regard would be very helpful.
The approach Cython takes to creating Python bindings uses a Python-like language to define the bindings and then generates C or C++ code that can be compiled into the module. There are several methods for building Python bindings with Cython . The most common one is to use setup from distutils .
pybind11 is a lightweight header-only library that exposes C++ types in Python and vice versa, mainly to create Python bindings of existing C++ code. Its goals and syntax are similar to the excellent Boost.
cppyy is an automatic, run-time, Python-C++ bindings generator, for calling C++ from Python and Python from C++.
There are only two projects I know to have automatic binding generators for C++. The first one is SWIG. As some other answer has already said, it is a little bit old style, but it works. The second one is Boost.Python - by itself, it does not generate the bindings automatically, but you can use Boost.Pyste to do it for you. It requires GCC-XML to parse your original source code and write the Boost.Python bindings. Both options do support virtual methods in C++ that can be overloaded from Python.
That said, I must complement that, normally, when you are binding, you don't blindly bind everything you have in C++ into Python - if you do it like this you won't get a very pythonic feeling from the python side. Instead, you design how you'd like your library to be used in Python, in the most pythonic possible way, and then you walk back and see how to patch it through to your C++ code using one of the possible binding libraries. For example, instead of dealing with std::vector
's, you'd prefer your library calls handle Python lists or iterables. If your C++ library receives a std::map
, you'd like that is handled with Python dictionaries. If it is an array, maybe a numpy.ndarray
would be more convenient. And so on...
That said, you can still design your bindings so that your maintenance is minimized.
Here is a list of other Python/C++ wrapping, in case you decide to look around a bit further:
These are now inactive:
For completeness, it is also possible to load compiled C code directly into Python without creating formal bindings. You can do this using FFI with any of these two Python modules:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With