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) |
Wenzel Jakob | 8d862b3 | 2016-03-06 13:37:22 +0100 | [diff] [blame] | 7 | [](https://ci.appveyor.com/project/wjakob/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 | 06f56ee | 2016-04-28 16:25:24 +0200 | [diff] [blame] | 35 | A PDF version of the manual is available |
| 36 | [here](https://media.readthedocs.org/pdf/pybind11/latest/pybind11.pdf). |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 37 | |
| 38 | ## Core features |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 39 | pybind11 can map the following core C++ features to Python |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 40 | |
| 41 | - Functions accepting and returning custom data structures per value, reference, or pointer |
| 42 | - Instance methods and static methods |
| 43 | - Overloaded functions |
| 44 | - Instance attributes and static attributes |
Wenzel Jakob | 58ec1ca | 2016-07-11 23:39:39 +0200 | [diff] [blame] | 45 | - Arbitrary exception types |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 46 | - Enumerations |
| 47 | - Callbacks |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 48 | - Iterators and ranges |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 49 | - Custom operators |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 50 | - Single and multiple inheritance |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 51 | - STL data structures |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 52 | - Iterators and ranges |
Wenzel Jakob | 8e5dceb | 2016-09-11 20:00:40 +0900 | [diff] [blame] | 53 | - Smart pointers with reference counting like ``std::shared_ptr`` |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 54 | - Internal references with correct reference counting |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 55 | - 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] | 56 | |
| 57 | ## Goodies |
| 58 | In addition to the core functionality, pybind11 provides some extra goodies: |
| 59 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 60 | - pybind11 uses C++11 move constructors and move assignment operators whenever |
| 61 | possible to efficiently transfer custom data types. |
| 62 | |
| 63 | - It is possible to bind C++11 lambda functions with captured variables. The |
| 64 | lambda capture data is stored inside the resulting Python function object. |
| 65 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 66 | - It's easy to expose the internal storage of custom data types through |
| 67 | Pythons' buffer protocols. This is handy e.g. for fast conversion between |
| 68 | C++ matrix classes like Eigen and NumPy without expensive copy operations. |
| 69 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 70 | - pybind11 can automatically vectorize functions so that they are transparently |
| 71 | applied to all entries of one or more NumPy array arguments. |
| 72 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 73 | - Python's slice-based access and assignment operations can be supported with |
| 74 | just a few lines of code. |
| 75 | |
John Kirkham | 648e196 | 2015-12-04 17:51:42 -0500 | [diff] [blame] | 76 | - 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] | 77 | against any additional libraries. |
Wenzel Jakob | 24fe090 | 2015-12-05 14:41:25 +0100 | [diff] [blame] | 78 | |
Wenzel Jakob | 68b193e | 2016-08-19 09:32:58 +0200 | [diff] [blame] | 79 | - Binaries are generally smaller by a factor of at least 2 compared to |
| 80 | equivalent bindings generated by Boost.Python. A recent pybind11 conversion |
Wenzel Jakob | d4285a6 | 2016-09-21 19:30:23 +0200 | [diff] [blame] | 81 | of PyRosetta, an enormous Boost.Python binding project, |
Wenzel Jakob | 68b193e | 2016-08-19 09:32:58 +0200 | [diff] [blame] | 82 | [reported](http://graylab.jhu.edu/RosettaCon2016/PyRosetta-4.pdf) a binary |
| 83 | size reduction of **5.4x** and compile time reduction by **5.8x**. |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 84 | |
Wenzel Jakob | 6eb11da | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 85 | - 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] | 86 | return value deduction) are used to precompute function signatures at compile |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 87 | time, leading to smaller binaries. |
| 88 | |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 89 | - With little extra effort, C++ types can be pickled and unpickled similar to |
| 90 | regular Python objects. |
| 91 | |
Wenzel Jakob | 240e404 | 2016-02-20 21:00:45 +0100 | [diff] [blame] | 92 | ## Supported compilers |
| 93 | |
| 94 | 1. Clang/LLVM (any non-ancient version with C++11 support) |
| 95 | 2. GCC (any non-ancient version with C++11 support) |
| 96 | 3. Microsoft Visual Studio 2015 or newer |
Wenzel Jakob | 59b240a | 2016-07-12 16:56:47 +0200 | [diff] [blame] | 97 | 4. Intel C++ compiler 16 or newer (15 with a [workaround](https://github.com/pybind/pybind11/issues/276)) |
Wenzel Jakob | 9767c48 | 2016-06-02 20:31:17 +0200 | [diff] [blame] | 98 | 5. Cygwin/GCC (tested on 2.5.1) |
Wenzel Jakob | 240e404 | 2016-02-20 21:00:45 +0100 | [diff] [blame] | 99 | |
Wenzel Jakob | 74982c3 | 2016-03-01 12:45:08 +0100 | [diff] [blame] | 100 | ## About |
| 101 | |
| 102 | This project was created by [Wenzel Jakob](https://www.mitsuba-renderer.org/~wenzel/). |
| 103 | Significant features and/or improvements to the code were contributed by |
| 104 | Jonas Adler, |
| 105 | Sylvain Corlay, |
Wenzel Jakob | 8862887 | 2016-09-27 18:02:20 +0200 | [diff] [blame] | 106 | Trent Houliston, |
Wenzel Jakob | 74982c3 | 2016-03-01 12:45:08 +0100 | [diff] [blame] | 107 | Axel Huebl, |
Wenzel Jakob | 81dfd2c | 2016-03-08 19:40:32 +0100 | [diff] [blame] | 108 | @hulucc, |
Wenzel Jakob | 25c03ce | 2016-05-15 20:50:38 +0200 | [diff] [blame] | 109 | Sergey Lyskov |
Wenzel Jakob | 8d862b3 | 2016-03-06 13:37:22 +0100 | [diff] [blame] | 110 | Johan Mabille, |
Wenzel Jakob | 7da7b67 | 2016-05-29 12:37:11 +0200 | [diff] [blame] | 111 | Tomasz Miąsko, |
| 112 | Dean Moldovan, |
Wenzel Jakob | 4d727e1 | 2016-07-12 16:58:55 +0200 | [diff] [blame] | 113 | Ben Pritchard, |
Wenzel Jakob | bb6c1f9 | 2016-08-12 00:58:49 +0200 | [diff] [blame] | 114 | Jason Rhinelander, |
Ivan Smirnov | bccbc10 | 2016-08-13 21:17:26 +0100 | [diff] [blame] | 115 | Boris Schäling, |
Wenzel Jakob | 68b193e | 2016-08-19 09:32:58 +0200 | [diff] [blame] | 116 | Pim Schellart, and |
Ivan Smirnov | bccbc10 | 2016-08-13 21:17:26 +0100 | [diff] [blame] | 117 | Ivan Smirnov. |
Wenzel Jakob | 74982c3 | 2016-03-01 12:45:08 +0100 | [diff] [blame] | 118 | |
Wenzel Jakob | 24fe090 | 2015-12-05 14:41:25 +0100 | [diff] [blame] | 119 | ### License |
| 120 | |
| 121 | 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] | 122 | ``LICENSE`` file. By using, distributing, or contributing to this project, |
Wenzel Jakob | 24fe090 | 2015-12-05 14:41:25 +0100 | [diff] [blame] | 123 | you agree to the terms and conditions of this license. |