Navigate:
pybind11
~$PYBIN0.1%

pybind11: C++ and Python interoperability bindings

Header-only library creating minimal-boilerplate C++ Python bindings.

LIVE RANKINGS • 10:20 AM • STEADY
OVERALL
#389
110
DEVELOPER TOOLS
#81
22
30 DAY RANKING TREND
ovr#389
·Devel#81
STARS
17.7K
FORKS
2.3K
7D STARS
+9
7D FORKS
+8
See Repo:
Share:

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.

pybind11

1

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.

2

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.

3

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: 8

vv3.0.1

Fixed 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.
vv3.0.0

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.
vv3.0.0rc4

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


[ EXPLORE MORE ]

Related Repositories

Discover similar tools and frameworks used by developers