blob: 9d4827a3e569cdaad28595184fe3adaae3804aed [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
Wenzel Jakob58ec1ca2016-07-11 23:39:39 +020045- Arbitrary exception types
Wenzel Jakob38bd7112015-07-05 20:05:44 +020046- Enumerations
47- Callbacks
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090048- Iterators and ranges
Wenzel Jakob38bd7112015-07-05 20:05:44 +020049- Custom operators
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090050- Single and multiple inheritance
Wenzel Jakob38bd7112015-07-05 20:05:44 +020051- STL data structures
Wenzel Jakobb2825952016-04-13 23:33:00 +020052- Iterators and ranges
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090053- Smart pointers with reference counting like ``std::shared_ptr``
Wenzel Jakob38bd7112015-07-05 20:05:44 +020054- Internal references with correct reference counting
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020055- C++ classes with virtual (and pure virtual) methods can be extended in Python
Wenzel Jakob38bd7112015-07-05 20:05:44 +020056
57## Goodies
58In addition to the core functionality, pybind11 provides some extra goodies:
59
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020060- 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 Jakob38bd7112015-07-05 20:05:44 +020066- 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 Jakobd4258ba2015-07-26 16:33:49 +020070- pybind11 can automatically vectorize functions so that they are transparently
71 applied to all entries of one or more NumPy array arguments.
72
Wenzel Jakob38bd7112015-07-05 20:05:44 +020073- Python's slice-based access and assignment operations can be supported with
74 just a few lines of code.
75
John Kirkham648e1962015-12-04 17:51:42 -050076- Everything is contained in just a few header files; there is no need to link
Wenzel Jakob7641c1d2015-10-18 14:48:24 +020077 against any additional libraries.
Wenzel Jakob24fe0902015-12-05 14:41:25 +010078
Wenzel Jakob68b193e2016-08-19 09:32:58 +020079- 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 Jakobd4285a62016-09-21 19:30:23 +020081 of PyRosetta, an enormous Boost.Python binding project,
Wenzel Jakob68b193e2016-08-19 09:32:58 +020082 [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 Jakob66c9a402016-01-17 22:36:36 +010084
Wenzel Jakob6eb11da2016-01-17 22:36:36 +010085- When supported by the compiler, two new C++14 features (relaxed constexpr and
Wenzel Jakob240e4042016-02-20 21:00:45 +010086 return value deduction) are used to precompute function signatures at compile
Wenzel Jakob66c9a402016-01-17 22:36:36 +010087 time, leading to smaller binaries.
88
Wenzel Jakobb2825952016-04-13 23:33:00 +020089- With little extra effort, C++ types can be pickled and unpickled similar to
90 regular Python objects.
91
Wenzel Jakob240e4042016-02-20 21:00:45 +010092## Supported compilers
93
941. Clang/LLVM (any non-ancient version with C++11 support)
952. GCC (any non-ancient version with C++11 support)
963. Microsoft Visual Studio 2015 or newer
Wenzel Jakob59b240a2016-07-12 16:56:47 +0200974. Intel C++ compiler 16 or newer (15 with a [workaround](https://github.com/pybind/pybind11/issues/276))
Wenzel Jakob9767c482016-06-02 20:31:17 +0200985. Cygwin/GCC (tested on 2.5.1)
Wenzel Jakob240e4042016-02-20 21:00:45 +010099
Wenzel Jakob74982c32016-03-01 12:45:08 +0100100## About
101
102This project was created by [Wenzel Jakob](https://www.mitsuba-renderer.org/~wenzel/).
103Significant features and/or improvements to the code were contributed by
104Jonas Adler,
105Sylvain Corlay,
Wenzel Jakob88628872016-09-27 18:02:20 +0200106Trent Houliston,
Wenzel Jakob74982c32016-03-01 12:45:08 +0100107Axel Huebl,
Wenzel Jakob81dfd2c2016-03-08 19:40:32 +0100108@hulucc,
Wenzel Jakob25c03ce2016-05-15 20:50:38 +0200109Sergey Lyskov
Wenzel Jakob8d862b32016-03-06 13:37:22 +0100110Johan Mabille,
Wenzel Jakob7da7b672016-05-29 12:37:11 +0200111Tomasz Miąsko,
112Dean Moldovan,
Wenzel Jakob4d727e12016-07-12 16:58:55 +0200113Ben Pritchard,
Wenzel Jakobbb6c1f92016-08-12 00:58:49 +0200114Jason Rhinelander,
Ivan Smirnovbccbc102016-08-13 21:17:26 +0100115Boris Schäling,
Wenzel Jakob68b193e2016-08-19 09:32:58 +0200116Pim Schellart, and
Ivan Smirnovbccbc102016-08-13 21:17:26 +0100117Ivan Smirnov.
Wenzel Jakob74982c32016-03-01 12:45:08 +0100118
Wenzel Jakob24fe0902015-12-05 14:41:25 +0100119### License
120
121pybind11 is provided under a BSD-style license that can be found in the
Wenzel Jakob1ae77fe2016-01-17 22:36:43 +0100122``LICENSE`` file. By using, distributing, or contributing to this project,
Wenzel Jakob24fe0902015-12-05 14:41:25 +0100123you agree to the terms and conditions of this license.