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:
- SWIG - as you already know
- Boost.Python - this is what we normally use around here - pretty well structured
- Cython - very neat syntax close to Python - they claim to be much faster than Boost.Python
- SIP - not very much spread, but it is there
- pybind11 - Syntax similar to Boost.Python, compact implementation thanks to C++11.
These are now inactive:
- PyBindGen - claims to be the fastest, but inactive since may 21st, 2017, previous release on 2014 - currently maintained on github (https://github.com/gjcarneiro/pybindgen/releases)
- ECS:Python - inactive since december 6th, 2014 (v2.8), moved to github (https://github.com/MarcusTomlinson/ECS-Python)
- PyCXX - C++ facilities to make it easier to write Python extensions - inactive? Last release was v7.0.2 on 23rd april 2017
- CLIF - CLIF provides a common foundation for creating C++ wrapper generators for various languages - inactive? Not much activity on repo (only 14 commits)
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:
- ctypes - This native to Python and requires no external module installation.
- cffi - This is a new package inspired by the equivalent design on the Lua JIT.