ctypesgen is a Python wrapper generator for `ctypes`, designed to automate the creation of Python bindings for C header files. It parses C/C++ header files and generates Python modules that use the `ctypes` foreign function interface to call functions and access data structures in shared C libraries. The current version is 1.1.1, with releases occurring periodically to address bugs and introduce new features, often yearly or bi-annually.
pip install ctypesgenVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `ctypesgen` programmatically to generate Python bindings for a simple C header file. It creates a dummy header, invokes `ctypesgen.main.main` with appropriate arguments to create a Python file, and then prints instructions for how to use the generated bindings (which requires compiling the C code into a shared library).
Upgrade to Python 3.7+ or use ctypesgen<1.1.0 for Python 2 projects.
Ensure a C/C++ compiler is installed and accessible via your system's PATH. On Linux, install `build-essential`. On macOS, install Xcode Command Line Tools (`xcode-select --install`). On Windows, install MinGW or MSVC.
Always pass arguments as a list: `ctypesgen.main.main(['-o', 'output.py', 'input.h'])`.
Add `--no-embed-preamble` to your ctypesgen arguments for separate preamble/loader files, then import them as needed.
Install `ctypesgen` correctly using `pip install ctypesgen` or manually `pip install ply`.
Install a C/C++ compiler (e.g., `build-essential` on Debian/Ubuntu, Xcode Command Line Tools on macOS, or MinGW/MSVC on Windows) and ensure it's added to your system's PATH.
Pass all arguments as a single list: `ctypesgen.main.main(['-o', 'output.py', 'input.h'])`.