PyBindGen is a code generator that produces C++ source files to create Python bindings for C/C++ libraries. It helps expose C/C++ functions and classes to Python without writing manual wrapper code. As of version 0.22.1, it's an active project with a slow but steady release cadence, often tied to changes in `ns-3` (its primary consumer).
pip install pybindgenVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a simple Python module that wraps a C++ function `add(int, int)`. The `generate_bindings` function creates a `Module` instance, adds a function definition with its parameters and return value, and then generates the C++ source file `my_bindings_module.cc`. This generated file then needs to be compiled into a Python extension module using a C++ compiler and a build system like `setuptools` or `waf`.
Ensure `python3-dev` (Linux), `python-devel` (RHEL/CentOS), or Xcode Command Line Tools (macOS) are installed, along with a C++ compiler.
Refer to PyBindGen documentation or examples for integrating generated code with `setuptools` (using `Extension` and `build_ext`) or other build systems.
Start with simple functions and classes. Consult PyBindGen's advanced documentation and examples for handling more complex C++ constructs. Be prepared for some manual C++ code (e.g., custom type traits or converters) if PyBindGen's automatic wrapping isn't sufficient.
Install the Python development package for your system. For Debian/Ubuntu: `sudo apt-get install python3-dev`. For Fedora/RHEL: `sudo dnf install python3-devel`. For macOS with Xcode Command Line Tools, Python headers are usually available, but ensure `python3` is linked correctly.
Ensure that the C++ library containing `MyCppNamespace::add` (or your actual C++ code) is correctly linked against the generated binding code. If using `setuptools.Extension`, add `libraries=['mylibrary']` and `library_dirs=['/path/to/lib']` to your `Extension` definition.
Verify that the generated C++ code was compiled against the correct Python version and ABI. Ensure PyBindGen's `Module` constructor's `py_init_name` (if explicitly set) matches the `PyInit_` function expected by your Python interpreter. Rebuild the module cleanly, ensuring all dependencies are met for the target Python environment.
No dependency data recorded yet.