blob: 2149c18db88212a8a4489c6c0214c5b4c986a66b [file] [log] [blame]
Wenzel Jakob6eb11da2016-01-17 22:36:36 +01001.. image:: pybind11-logo.png
2
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02003About this project
4==================
Wenzel Jakob7641c1d2015-10-18 14:48:24 +02005**pybind11** is a lightweight header-only library that exposes C++ types in Python
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02006and vice versa, mainly to create Python bindings of existing C++ code. Its
7goals and syntax are similar to the excellent `Boost.Python`_ library by David
8Abrahams: to minimize boilerplate code in traditional extension modules by
Wenzel Jakob93296692015-10-13 23:21:54 +02009inferring type information using compile-time introspection.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020010
11.. _Boost.Python: http://www.boost.org/doc/libs/release/libs/python/doc/index.html
12
13The main issue with Boost.Pythonand the reason for creating such a similar
14projectis Boost. Boost is an enormously large and complex suite of utility
15libraries that works with almost every C++ compiler in existence. This
16compatibility has its cost: arcane template tricks and workarounds are
17necessary to support the oldest and buggiest of compiler specimens. Now that
18C++11-compatible compilers are widely available, this heavy machinery has
19become an excessively large and unnecessary dependency.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020020Think of this library as a tiny self-contained version of Boost.Python with
Wenzel Jakob678d7872016-01-17 22:36:41 +010021everything stripped away that isn't relevant for binding generation. Without
Wenzel Jakob1805c342016-12-26 13:26:17 +010022comments, the core header files only require ~4K lines of code and depend on
23Python (2.7 or 3.x, or PyPy2.7 >= 5.7) and the C++ standard library. This
24compact implementation was possible thanks to some of the new C++11 language
25features (specifically: tuples, lambda functions and variadic templates). Since
26its creation, this library has grown beyond Boost.Python in many ways, leading
27to dramatically simpler binding code in many common situations.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020028
29Core features
30*************
31The following core C++ features can be mapped to Python
32
33- Functions accepting and returning custom data structures per value, reference, or pointer
34- Instance methods and static methods
35- Overloaded functions
36- Instance attributes and static attributes
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090037- Arbitrary exception types
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020038- Enumerations
39- Callbacks
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090040- Iterators and ranges
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020041- Custom operators
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090042- Single and multiple inheritance
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020043- STL data structures
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090044- Iterators and ranges
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020045- Smart pointers with reference counting like ``std::shared_ptr``
46- Internal references with correct reference counting
47- C++ classes with virtual (and pure virtual) methods can be extended in Python
48
49Goodies
50*******
51In addition to the core functionality, pybind11 provides some extra goodies:
52
Wenzel Jakob003a9eb2016-12-18 17:08:13 +010053- Python 2.7, 3.x, and PyPy (PyPy2.7 >= 5.7) are supported with an
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010054 implementation-agnostic interface.
55
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020056- It is possible to bind C++11 lambda functions with captured variables. The
57 lambda capture data is stored inside the resulting Python function object.
58
59- pybind11 uses C++11 move constructors and move assignment operators whenever
60 possible to efficiently transfer custom data types.
61
62- It's easy to expose the internal storage of custom data types through
63 Pythons' buffer protocols. This is handy e.g. for fast conversion between
64 C++ matrix classes like Eigen and NumPy without expensive copy operations.
65
66- pybind11 can automatically vectorize functions so that they are transparently
67 applied to all entries of one or more NumPy array arguments.
68
69- Python's slice-based access and assignment operations can be supported with
70 just a few lines of code.
71
Wenzel Jakob40584ce2015-12-04 23:58:23 +010072- Everything is contained in just a few header files; there is no need to link
Wenzel Jakob7641c1d2015-10-18 14:48:24 +020073 against any additional libraries.
Wenzel Jakob66c9a402016-01-17 22:36:36 +010074
Wenzel Jakob68b193e2016-08-19 09:32:58 +020075- Binaries are generally smaller by a factor of at least 2 compared to
76 equivalent bindings generated by Boost.Python. A recent pybind11 conversion
Wenzel Jakobd4285a62016-09-21 19:30:23 +020077 of `PyRosetta`_, an enormous Boost.Python binding project, reported a binary
Wenzel Jakob68b193e2016-08-19 09:32:58 +020078 size reduction of **5.4x** and compile time reduction by **5.8x**.
79
Wenzel Jakob6eb11da2016-01-17 22:36:36 +010080- When supported by the compiler, two new C++14 features (relaxed constexpr and
Wenzel Jakob240e4042016-02-20 21:00:45 +010081 return value deduction) are used to precompute function signatures at compile
Wenzel Jakob66c9a402016-01-17 22:36:36 +010082 time, leading to smaller binaries.
Wenzel Jakob240e4042016-02-20 21:00:45 +010083
Wenzel Jakobb2825952016-04-13 23:33:00 +020084- With little extra effort, C++ types can be pickled and unpickled similar to
85 regular Python objects.
86
Wenzel Jakob192eb882016-08-19 09:38:14 +020087.. _PyRosetta: http://graylab.jhu.edu/RosettaCon2016/PyRosetta-4.pdf
88
Wenzel Jakob240e4042016-02-20 21:00:45 +010089Supported compilers
90*******************
91
921. Clang/LLVM (any non-ancient version with C++11 support)
Michael Königf3318432016-12-28 12:10:11 +0100932. GCC 4.8 or newer
Wenzel Jakob240e4042016-02-20 21:00:45 +0100943. Microsoft Visual Studio 2015 or newer
954. Intel C++ compiler v15 or newer