blob: 08b58bf722026a1e52af967c12a2b10611b81beb [file] [log] [blame] [view]
Wenzel Jakob937d6462016-03-01 15:41:02 +01001![pybind11 logo](https://github.com/pybind/pybind11/raw/master/docs/pybind11-logo.png)
Wenzel Jakob10d992e2015-08-04 13:59:51 +02002
3# pybind11 — Seamless operability between C++11 and Python
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02005[![Documentation Status](https://readthedocs.org/projects/pybind11/badge/?version=latest)](http://pybind11.readthedocs.org/en/latest/?badge=latest)
Wenzel Jakob937d6462016-03-01 15:41:02 +01006[![Build Status](https://travis-ci.org/pybind/pybind11.svg?branch=master)](https://travis-ci.org/pybind/pybind11)
Wenzel Jakob8d862b32016-03-06 13:37:22 +01007[![Build status](https://ci.appveyor.com/api/projects/status/riaj54pn4h08xy40?svg=true)](https://ci.appveyor.com/project/wjakob/pybind11)
Wenzel Jakob3b806d42015-10-11 16:29:35 +02008
Wenzel Jakob7641c1d2015-10-18 14:48:24 +02009**pybind11** is a lightweight header-only library that exposes C++ types in Python
Wenzel Jakob38bd7112015-07-05 20:05:44 +020010and vice versa, mainly to create Python bindings of existing C++ code. Its
11goals and syntax are similar to the excellent
12[Boost.Python](http://www.boost.org/doc/libs/1_58_0/libs/python/doc/) library
13by David Abrahams: to minimize boilerplate code in traditional extension
14modules by inferring type information using compile-time introspection.
15
16The main issue with Boost.Pythonand the reason for creating such a similar
17projectis Boost. Boost is an enormously large and complex suite of utility
18libraries that works with almost every C++ compiler in existence. This
19compatibility has its cost: arcane template tricks and workarounds are
20necessary to support the oldest and buggiest of compiler specimens. Now that
21C++11-compatible compilers are widely available, this heavy machinery has
22become an excessively large and unnecessary dependency.
23
24Think of this library as a tiny self-contained version of Boost.Python with
Wenzel Jakob678d7872016-01-17 22:36:41 +010025everything stripped away that isn't relevant for binding generation. Without
26comments, the core header files only require ~2.5K lines of code and depend on
27Python (2.7 or 3.x) and the C++ standard library. This compact implementation
28was possible thanks to some of the new C++11 language features (specifically:
29tuples, lambda functions and variadic templates). Since its creation, this
30library has grown beyond Boost.Python in many ways, leading to dramatically
31simpler binding code in many common situations.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020032
33Tutorial and reference documentation is provided at
34[http://pybind11.readthedocs.org/en/latest](http://pybind11.readthedocs.org/en/latest).
Wenzel Jakob06f56ee2016-04-28 16:25:24 +020035A PDF version of the manual is available
36[here](https://media.readthedocs.org/pdf/pybind11/latest/pybind11.pdf).
Wenzel Jakob38bd7112015-07-05 20:05:44 +020037
38## Core features
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020039pybind11 can map the following core C++ features to Python
Wenzel Jakob38bd7112015-07-05 20:05:44 +020040
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
45- Exceptions
46- Enumerations
47- Callbacks
48- Custom operators
49- STL data structures
Wenzel Jakobb2825952016-04-13 23:33:00 +020050- Iterators and ranges
Wenzel Jakob38bd7112015-07-05 20:05:44 +020051- Smart pointers with reference counting like `std::shared_ptr`
52- Internal references with correct reference counting
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020053- C++ classes with virtual (and pure virtual) methods can be extended in Python
Wenzel Jakob38bd7112015-07-05 20:05:44 +020054
55## Goodies
56In addition to the core functionality, pybind11 provides some extra goodies:
57
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020058- pybind11 uses C++11 move constructors and move assignment operators whenever
59 possible to efficiently transfer custom data types.
60
61- It is possible to bind C++11 lambda functions with captured variables. The
62 lambda capture data is stored inside the resulting Python function object.
63
Wenzel Jakob38bd7112015-07-05 20:05:44 +020064- It's easy to expose the internal storage of custom data types through
65 Pythons' buffer protocols. This is handy e.g. for fast conversion between
66 C++ matrix classes like Eigen and NumPy without expensive copy operations.
67
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020068- pybind11 can automatically vectorize functions so that they are transparently
69 applied to all entries of one or more NumPy array arguments.
70
Wenzel Jakob38bd7112015-07-05 20:05:44 +020071- Python's slice-based access and assignment operations can be supported with
72 just a few lines of code.
73
John Kirkham648e1962015-12-04 17:51:42 -050074- Everything is contained in just a few header files; there is no need to link
Wenzel Jakob7641c1d2015-10-18 14:48:24 +020075 against any additional libraries.
Wenzel Jakob24fe0902015-12-05 14:41:25 +010076
Wenzel Jakob66c9a402016-01-17 22:36:36 +010077- Binaries are generally smaller by a factor of 2 or more compared to
78 equivalent bindings generated by Boost.Python.
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.
83
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 Jakob240e4042016-02-20 21:00:45 +010087## Supported compilers
88
891. Clang/LLVM (any non-ancient version with C++11 support)
902. GCC (any non-ancient version with C++11 support)
913. Microsoft Visual Studio 2015 or newer
924. Intel C++ compiler v15 or newer
93
Wenzel Jakob74982c32016-03-01 12:45:08 +010094## About
95
96This project was created by [Wenzel Jakob](https://www.mitsuba-renderer.org/~wenzel/).
97Significant features and/or improvements to the code were contributed by
98Jonas Adler,
99Sylvain Corlay,
100Axel Huebl,
Wenzel Jakob81dfd2c2016-03-08 19:40:32 +0100101@hulucc,
Wenzel Jakob25c03ce2016-05-15 20:50:38 +0200102Sergey Lyskov
Wenzel Jakob8d862b32016-03-06 13:37:22 +0100103Johan Mabille,
Wenzel Jakob7da7b672016-05-29 12:37:11 +0200104Tomasz Miąsko,
105Dean Moldovan,
106Ben Pritchard, and
107Boris Schäling.
Wenzel Jakob74982c32016-03-01 12:45:08 +0100108
Wenzel Jakob24fe0902015-12-05 14:41:25 +0100109### License
110
111pybind11 is provided under a BSD-style license that can be found in the
Wenzel Jakob1ae77fe2016-01-17 22:36:43 +0100112``LICENSE`` file. By using, distributing, or contributing to this project,
Wenzel Jakob24fe0902015-12-05 14:41:25 +0100113you agree to the terms and conditions of this license.