Python is one of the most widely used “glue” languages. There are a multitude of ways to interact with C/C++ code from Python. I have summarised some of the tools here:
Using the Python C API directly
It is possible to write extension modules (modules implemented in C) directly using the Python C API. This will only work with the CPython implementation however.
I don’t really recommend this approach however. Interacting with Python objects from C isn’t particularly easy. For example you must manually call
Py_INCREF and then
Py_DECREF when finished to keep the reference count updated.
Ctypes is a foreign function library included in the Python standard library.
This is probably the easiest tool to work with. Libraries a loaded at runtime by giving ctypes the
.dll filename. You can then call the functions defined in the library as if they were defined in Python. Primitive types such as
char * are automatically converted to the correct python types.
Cython is a much more powerful tool than ctypes. Not only can you call C/C++ code from Python, but you can statically compile Python to C code with almost no code modifications.
This will provide a moderate performance boost by removing the interpreter overhead and calling the C API directly. You can gain significant performance improvements by adding type definitions to the Python code.
Boost Python is a tool specifically designed for binding C++ code to Python.
Among the features it lists are:
- Automatic exception translation.
- Support for converting C++ iterators to Python iterators.
- Support for accessing Python objects in C++.
Use this if you have custom Qt widgets or classes implemented in C++ that you would like to use from Python.
SciPy Weave is a tool for calling blocks of C code from Python. You can also use FORTRAN which is still sometimes used for its good linear algebra support. Being part of the SciPy project, it integrates well with NumPy.
Use this if you would like to generate code bindings for multiple languages.