Wenzel Jakob | 937d646 | 2016-03-01 15:41:02 +0100 | [diff] [blame] | 1 |  |
Wenzel Jakob | 10d992e | 2015-08-04 13:59:51 +0200 | [diff] [blame] | 2 | |
| 3 | # pybind11 — Seamless operability between C++11 and Python |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 4 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 5 | [](http://pybind11.readthedocs.org/en/latest/?badge=latest) |
Wenzel Jakob | 937d646 | 2016-03-01 15:41:02 +0100 | [diff] [blame] | 6 | [](https://travis-ci.org/pybind/pybind11) |
| 7 | [](https://ci.appveyor.com/project/pybind/pybind11) |
Wenzel Jakob | 3b806d4 | 2015-10-11 16:29:35 +0200 | [diff] [blame] | 8 | |
Wenzel Jakob | 7641c1d | 2015-10-18 14:48:24 +0200 | [diff] [blame] | 9 | **pybind11** is a lightweight header-only library that exposes C++ types in Python |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 10 | and vice versa, mainly to create Python bindings of existing C++ code. Its |
| 11 | goals and syntax are similar to the excellent |
| 12 | [Boost.Python](http://www.boost.org/doc/libs/1_58_0/libs/python/doc/) library |
| 13 | by David Abrahams: to minimize boilerplate code in traditional extension |
| 14 | modules by inferring type information using compile-time introspection. |
| 15 | |
| 16 | The main issue with Boost.Python—and the reason for creating such a similar |
| 17 | project—is Boost. Boost is an enormously large and complex suite of utility |
| 18 | libraries that works with almost every C++ compiler in existence. This |
| 19 | compatibility has its cost: arcane template tricks and workarounds are |
| 20 | necessary to support the oldest and buggiest of compiler specimens. Now that |
| 21 | C++11-compatible compilers are widely available, this heavy machinery has |
| 22 | become an excessively large and unnecessary dependency. |
| 23 | |
| 24 | Think of this library as a tiny self-contained version of Boost.Python with |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 25 | everything stripped away that isn't relevant for binding generation. Without |
| 26 | comments, the core header files only require ~2.5K lines of code and depend on |
| 27 | Python (2.7 or 3.x) and the C++ standard library. This compact implementation |
| 28 | was possible thanks to some of the new C++11 language features (specifically: |
| 29 | tuples, lambda functions and variadic templates). Since its creation, this |
| 30 | library has grown beyond Boost.Python in many ways, leading to dramatically |
| 31 | simpler binding code in many common situations. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 32 | |
| 33 | Tutorial and reference documentation is provided at |
| 34 | [http://pybind11.readthedocs.org/en/latest](http://pybind11.readthedocs.org/en/latest). |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 35 | |
| 36 | ## Core features |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 37 | pybind11 can map the following core C++ features to Python |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 38 | |
| 39 | - Functions accepting and returning custom data structures per value, reference, or pointer |
| 40 | - Instance methods and static methods |
| 41 | - Overloaded functions |
| 42 | - Instance attributes and static attributes |
| 43 | - Exceptions |
| 44 | - Enumerations |
| 45 | - Callbacks |
| 46 | - Custom operators |
| 47 | - STL data structures |
| 48 | - Smart pointers with reference counting like `std::shared_ptr` |
| 49 | - Internal references with correct reference counting |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 50 | - C++ classes with virtual (and pure virtual) methods can be extended in Python |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 51 | |
| 52 | ## Goodies |
| 53 | In addition to the core functionality, pybind11 provides some extra goodies: |
| 54 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 55 | - pybind11 uses C++11 move constructors and move assignment operators whenever |
| 56 | possible to efficiently transfer custom data types. |
| 57 | |
| 58 | - It is possible to bind C++11 lambda functions with captured variables. The |
| 59 | lambda capture data is stored inside the resulting Python function object. |
| 60 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 61 | - It's easy to expose the internal storage of custom data types through |
| 62 | Pythons' buffer protocols. This is handy e.g. for fast conversion between |
| 63 | C++ matrix classes like Eigen and NumPy without expensive copy operations. |
| 64 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 65 | - pybind11 can automatically vectorize functions so that they are transparently |
| 66 | applied to all entries of one or more NumPy array arguments. |
| 67 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 68 | - Python's slice-based access and assignment operations can be supported with |
| 69 | just a few lines of code. |
| 70 | |
John Kirkham | 648e196 | 2015-12-04 17:51:42 -0500 | [diff] [blame] | 71 | - Everything is contained in just a few header files; there is no need to link |
Wenzel Jakob | 7641c1d | 2015-10-18 14:48:24 +0200 | [diff] [blame] | 72 | against any additional libraries. |
Wenzel Jakob | 24fe090 | 2015-12-05 14:41:25 +0100 | [diff] [blame] | 73 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 74 | - Binaries are generally smaller by a factor of 2 or more compared to |
| 75 | equivalent bindings generated by Boost.Python. |
| 76 | |
Wenzel Jakob | 6eb11da | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 77 | - When supported by the compiler, two new C++14 features (relaxed constexpr and |
Wenzel Jakob | 240e404 | 2016-02-20 21:00:45 +0100 | [diff] [blame] | 78 | return value deduction) are used to precompute function signatures at compile |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 79 | time, leading to smaller binaries. |
| 80 | |
Wenzel Jakob | 240e404 | 2016-02-20 21:00:45 +0100 | [diff] [blame] | 81 | ## Supported compilers |
| 82 | |
| 83 | 1. Clang/LLVM (any non-ancient version with C++11 support) |
| 84 | 2. GCC (any non-ancient version with C++11 support) |
| 85 | 3. Microsoft Visual Studio 2015 or newer |
| 86 | 4. Intel C++ compiler v15 or newer |
| 87 | |
Wenzel Jakob | 74982c3 | 2016-03-01 12:45:08 +0100 | [diff] [blame] | 88 | ## About |
| 89 | |
| 90 | This project was created by [Wenzel Jakob](https://www.mitsuba-renderer.org/~wenzel/). |
| 91 | Significant features and/or improvements to the code were contributed by |
| 92 | Jonas Adler, |
| 93 | Sylvain Corlay, |
| 94 | Axel Huebl, |
| 95 | Johan Mabille, and |
| 96 | Tomasz Miąsko. |
| 97 | |
Wenzel Jakob | 24fe090 | 2015-12-05 14:41:25 +0100 | [diff] [blame] | 98 | ### License |
| 99 | |
| 100 | pybind11 is provided under a BSD-style license that can be found in the |
Wenzel Jakob | 1ae77fe | 2016-01-17 22:36:43 +0100 | [diff] [blame] | 101 | ``LICENSE`` file. By using, distributing, or contributing to this project, |
Wenzel Jakob | 24fe090 | 2015-12-05 14:41:25 +0100 | [diff] [blame] | 102 | you agree to the terms and conditions of this license. |