You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
5.4 KiB

  1. ![pybind11 logo](https://github.com/pybind/pybind11/raw/master/docs/pybind11-logo.png)
  2. # pybind11 — Seamless operability between C++11 and Python
  3. [![Documentation Status](https://readthedocs.org/projects/pybind11/badge/?version=latest)](http://pybind11.readthedocs.org/en/latest/?badge=latest)
  4. [![Build Status](https://travis-ci.org/pybind/pybind11.svg?branch=master)](https://travis-ci.org/pybind/pybind11)
  5. [![Build status](https://ci.appveyor.com/api/projects/status/riaj54pn4h08xy40?svg=true)](https://ci.appveyor.com/project/wjakob/pybind11)
  6. **pybind11** is a lightweight header-only library that exposes C++ types in Python
  7. and vice versa, mainly to create Python bindings of existing C++ code. Its
  8. goals and syntax are similar to the excellent
  9. [Boost.Python](http://www.boost.org/doc/libs/1_58_0/libs/python/doc/) library
  10. by David Abrahams: to minimize boilerplate code in traditional extension
  11. modules by inferring type information using compile-time introspection.
  12. The main issue with Boost.Python—and the reason for creating such a similar
  13. project—is Boost. Boost is an enormously large and complex suite of utility
  14. libraries that works with almost every C++ compiler in existence. This
  15. compatibility has its cost: arcane template tricks and workarounds are
  16. necessary to support the oldest and buggiest of compiler specimens. Now that
  17. C++11-compatible compilers are widely available, this heavy machinery has
  18. become an excessively large and unnecessary dependency.
  19. Think of this library as a tiny self-contained version of Boost.Python with
  20. everything stripped away that isn't relevant for binding generation. Without
  21. comments, the core header files only require ~2.5K lines of code and depend on
  22. Python (2.7 or 3.x) and the C++ standard library. This compact implementation
  23. was possible thanks to some of the new C++11 language features (specifically:
  24. tuples, lambda functions and variadic templates). Since its creation, this
  25. library has grown beyond Boost.Python in many ways, leading to dramatically
  26. simpler binding code in many common situations.
  27. Tutorial and reference documentation is provided at
  28. [http://pybind11.readthedocs.org/en/latest](http://pybind11.readthedocs.org/en/latest).
  29. A PDF version of the manual is available
  30. [here](https://media.readthedocs.org/pdf/pybind11/latest/pybind11.pdf).
  31. ## Core features
  32. pybind11 can map the following core C++ features to Python
  33. - Functions accepting and returning custom data structures per value, reference, or pointer
  34. - Instance methods and static methods
  35. - Overloaded functions
  36. - Instance attributes and static attributes
  37. - Arbitrary exception types
  38. - Enumerations
  39. - Callbacks
  40. - Iterators and ranges
  41. - Custom operators
  42. - Single and multiple inheritance
  43. - STL data structures
  44. - Iterators and ranges
  45. - Smart pointers with reference counting like ``std::shared_ptr``
  46. - Internal references with correct reference counting
  47. - C++ classes with virtual (and pure virtual) methods can be extended in Python
  48. ## Goodies
  49. In addition to the core functionality, pybind11 provides some extra goodies:
  50. - pybind11 uses C++11 move constructors and move assignment operators whenever
  51. possible to efficiently transfer custom data types.
  52. - It is possible to bind C++11 lambda functions with captured variables. The
  53. lambda capture data is stored inside the resulting Python function object.
  54. - It's easy to expose the internal storage of custom data types through
  55. Pythons' buffer protocols. This is handy e.g. for fast conversion between
  56. C++ matrix classes like Eigen and NumPy without expensive copy operations.
  57. - pybind11 can automatically vectorize functions so that they are transparently
  58. applied to all entries of one or more NumPy array arguments.
  59. - Python's slice-based access and assignment operations can be supported with
  60. just a few lines of code.
  61. - Everything is contained in just a few header files; there is no need to link
  62. against any additional libraries.
  63. - Binaries are generally smaller by a factor of at least 2 compared to
  64. equivalent bindings generated by Boost.Python. A recent pybind11 conversion
  65. of PyRosetta, an enormous Boost.Python binding project,
  66. [reported](http://graylab.jhu.edu/RosettaCon2016/PyRosetta-4.pdf) a binary
  67. size reduction of **5.4x** and compile time reduction by **5.8x**.
  68. - When supported by the compiler, two new C++14 features (relaxed constexpr and
  69. return value deduction) are used to precompute function signatures at compile
  70. time, leading to smaller binaries.
  71. - With little extra effort, C++ types can be pickled and unpickled similar to
  72. regular Python objects.
  73. ## Supported compilers
  74. 1. Clang/LLVM (any non-ancient version with C++11 support)
  75. 2. GCC (any non-ancient version with C++11 support)
  76. 3. Microsoft Visual Studio 2015 or newer
  77. 4. Intel C++ compiler 16 or newer (15 with a [workaround](https://github.com/pybind/pybind11/issues/276))
  78. 5. Cygwin/GCC (tested on 2.5.1)
  79. ## About
  80. This project was created by [Wenzel Jakob](https://www.mitsuba-renderer.org/~wenzel/).
  81. Significant features and/or improvements to the code were contributed by
  82. Jonas Adler,
  83. Sylvain Corlay,
  84. Trent Houliston,
  85. Axel Huebl,
  86. @hulucc,
  87. Sergey Lyskov
  88. Johan Mabille,
  89. Tomasz Miąsko,
  90. Dean Moldovan,
  91. Ben Pritchard,
  92. Jason Rhinelander,
  93. Boris Schäling,
  94. Pim Schellart, and
  95. Ivan Smirnov.
  96. ### License
  97. pybind11 is provided under a BSD-style license that can be found in the
  98. ``LICENSE`` file. By using, distributing, or contributing to this project,
  99. you agree to the terms and conditions of this license.