intro.rst revision 14299
111986Sandreas.sandberg@arm.com.. image:: pybind11-logo.png 211986Sandreas.sandberg@arm.com 311986Sandreas.sandberg@arm.comAbout this project 411986Sandreas.sandberg@arm.com================== 511986Sandreas.sandberg@arm.com**pybind11** is a lightweight header-only library that exposes C++ types in Python 611986Sandreas.sandberg@arm.comand vice versa, mainly to create Python bindings of existing C++ code. Its 711986Sandreas.sandberg@arm.comgoals and syntax are similar to the excellent `Boost.Python`_ library by David 811986Sandreas.sandberg@arm.comAbrahams: to minimize boilerplate code in traditional extension modules by 911986Sandreas.sandberg@arm.cominferring type information using compile-time introspection. 1011986Sandreas.sandberg@arm.com 1111986Sandreas.sandberg@arm.com.. _Boost.Python: http://www.boost.org/doc/libs/release/libs/python/doc/index.html 1211986Sandreas.sandberg@arm.com 1311986Sandreas.sandberg@arm.comThe main issue with Boost.Python—and the reason for creating such a similar 1411986Sandreas.sandberg@arm.comproject—is Boost. Boost is an enormously large and complex suite of utility 1511986Sandreas.sandberg@arm.comlibraries that works with almost every C++ compiler in existence. This 1611986Sandreas.sandberg@arm.comcompatibility has its cost: arcane template tricks and workarounds are 1711986Sandreas.sandberg@arm.comnecessary to support the oldest and buggiest of compiler specimens. Now that 1811986Sandreas.sandberg@arm.comC++11-compatible compilers are widely available, this heavy machinery has 1911986Sandreas.sandberg@arm.combecome an excessively large and unnecessary dependency. 2011986Sandreas.sandberg@arm.comThink of this library as a tiny self-contained version of Boost.Python with 2111986Sandreas.sandberg@arm.comeverything stripped away that isn't relevant for binding generation. Without 2212037Sandreas.sandberg@arm.comcomments, the core header files only require ~4K lines of code and depend on 2312037Sandreas.sandberg@arm.comPython (2.7 or 3.x, or PyPy2.7 >= 5.7) and the C++ standard library. This 2412037Sandreas.sandberg@arm.comcompact implementation was possible thanks to some of the new C++11 language 2512037Sandreas.sandberg@arm.comfeatures (specifically: tuples, lambda functions and variadic templates). Since 2612037Sandreas.sandberg@arm.comits creation, this library has grown beyond Boost.Python in many ways, leading 2712037Sandreas.sandberg@arm.comto dramatically simpler binding code in many common situations. 2811986Sandreas.sandberg@arm.com 2911986Sandreas.sandberg@arm.comCore features 3011986Sandreas.sandberg@arm.com************* 3111986Sandreas.sandberg@arm.comThe following core C++ features can be mapped to Python 3211986Sandreas.sandberg@arm.com 3311986Sandreas.sandberg@arm.com- Functions accepting and returning custom data structures per value, reference, or pointer 3411986Sandreas.sandberg@arm.com- Instance methods and static methods 3511986Sandreas.sandberg@arm.com- Overloaded functions 3611986Sandreas.sandberg@arm.com- Instance attributes and static attributes 3711986Sandreas.sandberg@arm.com- Arbitrary exception types 3811986Sandreas.sandberg@arm.com- Enumerations 3911986Sandreas.sandberg@arm.com- Callbacks 4011986Sandreas.sandberg@arm.com- Iterators and ranges 4111986Sandreas.sandberg@arm.com- Custom operators 4211986Sandreas.sandberg@arm.com- Single and multiple inheritance 4311986Sandreas.sandberg@arm.com- STL data structures 4411986Sandreas.sandberg@arm.com- Smart pointers with reference counting like ``std::shared_ptr`` 4511986Sandreas.sandberg@arm.com- Internal references with correct reference counting 4611986Sandreas.sandberg@arm.com- C++ classes with virtual (and pure virtual) methods can be extended in Python 4711986Sandreas.sandberg@arm.com 4811986Sandreas.sandberg@arm.comGoodies 4911986Sandreas.sandberg@arm.com******* 5011986Sandreas.sandberg@arm.comIn addition to the core functionality, pybind11 provides some extra goodies: 5111986Sandreas.sandberg@arm.com 5212037Sandreas.sandberg@arm.com- Python 2.7, 3.x, and PyPy (PyPy2.7 >= 5.7) are supported with an 5312037Sandreas.sandberg@arm.com implementation-agnostic interface. 5412037Sandreas.sandberg@arm.com 5511986Sandreas.sandberg@arm.com- It is possible to bind C++11 lambda functions with captured variables. The 5611986Sandreas.sandberg@arm.com lambda capture data is stored inside the resulting Python function object. 5711986Sandreas.sandberg@arm.com 5811986Sandreas.sandberg@arm.com- pybind11 uses C++11 move constructors and move assignment operators whenever 5911986Sandreas.sandberg@arm.com possible to efficiently transfer custom data types. 6011986Sandreas.sandberg@arm.com 6111986Sandreas.sandberg@arm.com- It's easy to expose the internal storage of custom data types through 6211986Sandreas.sandberg@arm.com Pythons' buffer protocols. This is handy e.g. for fast conversion between 6311986Sandreas.sandberg@arm.com C++ matrix classes like Eigen and NumPy without expensive copy operations. 6411986Sandreas.sandberg@arm.com 6511986Sandreas.sandberg@arm.com- pybind11 can automatically vectorize functions so that they are transparently 6611986Sandreas.sandberg@arm.com applied to all entries of one or more NumPy array arguments. 6711986Sandreas.sandberg@arm.com 6811986Sandreas.sandberg@arm.com- Python's slice-based access and assignment operations can be supported with 6911986Sandreas.sandberg@arm.com just a few lines of code. 7011986Sandreas.sandberg@arm.com 7111986Sandreas.sandberg@arm.com- Everything is contained in just a few header files; there is no need to link 7211986Sandreas.sandberg@arm.com against any additional libraries. 7311986Sandreas.sandberg@arm.com 7411986Sandreas.sandberg@arm.com- Binaries are generally smaller by a factor of at least 2 compared to 7511986Sandreas.sandberg@arm.com equivalent bindings generated by Boost.Python. A recent pybind11 conversion 7611986Sandreas.sandberg@arm.com of `PyRosetta`_, an enormous Boost.Python binding project, reported a binary 7711986Sandreas.sandberg@arm.com size reduction of **5.4x** and compile time reduction by **5.8x**. 7811986Sandreas.sandberg@arm.com 7914299Sbbruce@ucdavis.edu- Function signatures are precomputed at compile time (using ``constexpr``), 8014299Sbbruce@ucdavis.edu leading to smaller binaries. 8111986Sandreas.sandberg@arm.com 8211986Sandreas.sandberg@arm.com- With little extra effort, C++ types can be pickled and unpickled similar to 8311986Sandreas.sandberg@arm.com regular Python objects. 8411986Sandreas.sandberg@arm.com 8511986Sandreas.sandberg@arm.com.. _PyRosetta: http://graylab.jhu.edu/RosettaCon2016/PyRosetta-4.pdf 8611986Sandreas.sandberg@arm.com 8711986Sandreas.sandberg@arm.comSupported compilers 8811986Sandreas.sandberg@arm.com******************* 8911986Sandreas.sandberg@arm.com 9011986Sandreas.sandberg@arm.com1. Clang/LLVM (any non-ancient version with C++11 support) 9112037Sandreas.sandberg@arm.com2. GCC 4.8 or newer 9211986Sandreas.sandberg@arm.com3. Microsoft Visual Studio 2015 or newer 9314299Sbbruce@ucdavis.edu4. Intel C++ compiler v17 or newer (v16 with pybind11 v2.0 and v15 with pybind11 v2.0 and a `workaround <https://github.com/pybind/pybind11/issues/276>`_ ) 94