pybind11: C++ and Python interoperability bindings
Header-only library creating minimal-boilerplate C++ Python bindings.
Learn more about pybind11
pybind11 is a C++ library designed to create Python bindings for existing C++ code. It operates as a header-only implementation that leverages C++11 features like tuples, lambda functions, and variadic templates to generate bindings with minimal code. The library supports CPython 3.8+, PyPy 7.3.17+, and GraalPy 24.1+, and can map C++ constructs including functions, methods, classes, exceptions, enumerations, STL containers, and smart pointers to their Python equivalents. Common use cases include wrapping performance-critical C++ libraries for use in Python applications and integrating existing C++ codebases into Python projects.
Header-only implementation
The core library consists of approximately 4,000 lines of code contained entirely in header files with no external dependencies beyond Python and the C++ standard library. This eliminates the need to link against additional libraries or manage complex build configurations.
C++11 language features
pybind11 uses modern C++ constructs to reduce boilerplate compared to earlier binding approaches. Type information is inferred at compile time through introspection, and function signatures are precomputed using constexpr, resulting in smaller binary sizes.
Broad C++ feature coverage
The library maps a wide range of C++ constructs to Python including virtual methods, multiple inheritance, custom operators, internal reference counting, and integration with NumPy arrays. It also supports C++11 lambda functions with captured variables and move semantics for efficient data transfer.
// example.cpp
#include <pybind11/pybind11.h>
int add(int a, int b) {
return a + b;
}
PYBIND11_MODULE(example, m) {
m.def("add", &add, "Add two numbers");
}
# Python usage
import example
result = example.add(5, 3)
print(result) # Output: 8Fixed compilation errors, resolved sub-interpreter crashes and segfaults, and improved compatibility with newer compilers.
- –Fixed compilation error in typecaster enum_type when casting pointer-to-enum types. Added pointer overload to handle dereferencing before enum conversion.
- –Implement binary version of make_index_sequence to reduce template depth requirements for functions with many parameters.
- –Subinterpreter-specific exception handling code was removed to resolve segfaults.
- –Fixed issue that caused PYBIND11_MODULE code to run again if the module was re-imported after being deleted from sys.modules.
- –Prevent concurrent creation of sub-interpreters as a workaround for stdlib concurrency issues in Python 3.12.
Major ABI bump introducing smart pointer support, multi-phase initialization, subinterpreter compatibility, and expanded Python support.
- –Added support for std::shared_ptr<const T> in py::init() when using py::smart_holder, complementing existing support for std::unique_ptr<const T>.
- –Support const-only smart pointers.
- –Changed PYBIND11_MODULE macro implementation to perform multi-phase module initialization (PEP 489) behind the scenes.
- –Rename macro PYBIND11_SUBINTERPRETER_SUPPORT -> PYBIND11_HAS_SUBINTERPRETER_SUPPORT to meet naming convention.
- –Allow subinterpreter support to be disabled if defined to 0. This is mostly an emergency workaround, and is not exposed in CMake.
Release candidate 4 with numpy scalars support, TSan warning fixes, and Android CMake support improvements.
- –feat: numpy scalars
- –fix: TSan warning in sub-interpreter test
- –fix: add support for shared_ptr<const T> in py::init() with smart_holder
- –fix: add support for const-only smart pointers
- –fix: android CMake support
Top in Developer Tools
Related Repositories
Discover similar tools and frameworks used by developers
StatsD
UDP/TCP metrics collector with pluggable backend support.
Redoc
React component rendering OpenAPI specifications as interactive HTML documentation.
Spring Initializr
Extensible API for generating JVM projects with multi-language and build system support.
Selenium
Browser automation framework implementing W3C WebDriver with multi-language support.
EPG
Node.js toolkit for downloading EPG data from hundreds of TV sources into standardized XML format.