Ivan Smirnov | 5cbfda5 | 2017-08-30 20:58:43 +0100 | [diff] [blame] | 1 | .. _compiling: |
| 2 | |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 3 | Build systems |
| 4 | ############# |
| 5 | |
| 6 | Building with setuptools |
| 7 | ======================== |
| 8 | |
| 9 | For projects on PyPI, building with setuptools is the way to go. Sylvain Corlay |
| 10 | has kindly provided an example project which shows how to set up everything, |
| 11 | including automatic generation of documentation using Sphinx. Please refer to |
Wenzel Jakob | ca8dc08 | 2016-06-03 14:24:17 +0200 | [diff] [blame] | 12 | the [python_example]_ repository. |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 13 | |
Wenzel Jakob | ca8dc08 | 2016-06-03 14:24:17 +0200 | [diff] [blame] | 14 | .. [python_example] https://github.com/pybind/python_example |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 15 | |
Wenzel Jakob | a439cca | 2016-05-17 10:47:52 +0200 | [diff] [blame] | 16 | Building with cppimport |
| 17 | ======================== |
| 18 | |
Dean Moldovan | 8665ee8 | 2017-08-17 15:01:43 +0200 | [diff] [blame] | 19 | [cppimport]_ is a small Python import hook that determines whether there is a C++ |
| 20 | source file whose name matches the requested module. If there is, the file is |
| 21 | compiled as a Python extension using pybind11 and placed in the same folder as |
| 22 | the C++ source file. Python is then able to find the module and load it. |
Wenzel Jakob | a439cca | 2016-05-17 10:47:52 +0200 | [diff] [blame] | 23 | |
| 24 | .. [cppimport] https://github.com/tbenthompson/cppimport |
| 25 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 26 | .. _cmake: |
| 27 | |
| 28 | Building with CMake |
| 29 | =================== |
| 30 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 31 | For C++ codebases that have an existing CMake-based build system, a Python |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 32 | extension module can be created with just a few lines of code: |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 33 | |
| 34 | .. code-block:: cmake |
| 35 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 36 | cmake_minimum_required(VERSION 3.4...3.18) |
| 37 | project(example LANGUAGES CXX) |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 38 | |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 39 | add_subdirectory(pybind11) |
| 40 | pybind11_add_module(example example.cpp) |
Wenzel Jakob | f64feaf | 2016-04-28 14:33:45 +0200 | [diff] [blame] | 41 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 42 | This assumes that the pybind11 repository is located in a subdirectory named |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 43 | :file:`pybind11` and that the code is located in a file named :file:`example.cpp`. |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 44 | The CMake command ``add_subdirectory`` will import the pybind11 project which |
| 45 | provides the ``pybind11_add_module`` function. It will take care of all the |
| 46 | details needed to build a Python extension module on any platform. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 47 | |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 48 | A working sample project, including a way to invoke CMake from :file:`setup.py` for |
| 49 | PyPI integration, can be found in the [cmake_example]_ repository. |
Wenzel Jakob | caa9d44 | 2016-01-17 22:36:34 +0100 | [diff] [blame] | 50 | |
Wenzel Jakob | aa79af0 | 2016-06-03 12:23:24 +0200 | [diff] [blame] | 51 | .. [cmake_example] https://github.com/pybind/cmake_example |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 52 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 53 | .. versionchanged:: 2.6 |
| 54 | CMake 3.4+ is required. |
| 55 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 56 | pybind11_add_module |
| 57 | ------------------- |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 58 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 59 | To ease the creation of Python extension modules, pybind11 provides a CMake |
| 60 | function with the following signature: |
| 61 | |
| 62 | .. code-block:: cmake |
| 63 | |
| 64 | pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL] |
Wenzel Jakob | 36c666f | 2020-09-04 23:31:05 +0200 | [diff] [blame] | 65 | [NO_EXTRAS] [THIN_LTO] [OPT_SIZE] source1 [source2 ...]) |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 66 | |
| 67 | This function behaves very much like CMake's builtin ``add_library`` (in fact, |
| 68 | it's a wrapper function around that command). It will add a library target |
| 69 | called ``<name>`` to be built from the listed source files. In addition, it |
| 70 | will take care of all the Python-specific compiler and linker flags as well |
| 71 | as the OS- and Python-version-specific file extension. The produced target |
| 72 | ``<name>`` can be further manipulated with regular CMake commands. |
| 73 | |
| 74 | ``MODULE`` or ``SHARED`` may be given to specify the type of library. If no |
| 75 | type is given, ``MODULE`` is used by default which ensures the creation of a |
| 76 | Python-exclusive module. Specifying ``SHARED`` will create a more traditional |
| 77 | dynamic library which can also be linked from elsewhere. ``EXCLUDE_FROM_ALL`` |
| 78 | removes this target from the default build (see CMake docs for details). |
| 79 | |
| 80 | Since pybind11 is a template library, ``pybind11_add_module`` adds compiler |
| 81 | flags to ensure high quality code generation without bloat arising from long |
Jason Rhinelander | 97aa54f | 2017-08-10 12:08:42 -0400 | [diff] [blame] | 82 | symbol names and duplication of code in different translation units. It |
| 83 | sets default visibility to *hidden*, which is required for some pybind11 |
| 84 | features and functionality when attempting to load multiple pybind11 modules |
| 85 | compiled under different pybind11 versions. It also adds additional flags |
| 86 | enabling LTO (Link Time Optimization) and strip unneeded symbols. See the |
| 87 | :ref:`FAQ entry <faq:symhidden>` for a more detailed explanation. These |
| 88 | latter optimizations are never applied in ``Debug`` mode. If ``NO_EXTRAS`` is |
| 89 | given, they will always be disabled, even in ``Release`` mode. However, this |
| 90 | will result in code bloat and is generally not recommended. |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 91 | |
| 92 | As stated above, LTO is enabled by default. Some newer compilers also support |
| 93 | different flavors of LTO such as `ThinLTO`_. Setting ``THIN_LTO`` will cause |
| 94 | the function to prefer this flavor if available. The function falls back to |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 95 | regular LTO if ``-flto=thin`` is not available. If |
| 96 | ``CMAKE_INTERPROCEDURAL_OPTIMIZATION`` is set (either ON or OFF), then that |
| 97 | will be respected instead of the built-in flag search. |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 98 | |
Wenzel Jakob | 36c666f | 2020-09-04 23:31:05 +0200 | [diff] [blame] | 99 | The ``OPT_SIZE`` flag enables size-based optimization equivalent to the |
| 100 | standard ``/Os`` or ``-Os`` compiler flags and the ``MinSizeRel`` build type, |
| 101 | which avoid optimizations that that can substantially increase the size of the |
| 102 | resulting binary. This flag is particularly useful in projects that are split |
| 103 | into performance-critical parts and associated bindings. In this case, we can |
| 104 | compile the project in release mode (and hence, optimize performance globally), |
| 105 | and specify ``OPT_SIZE`` for the binding target, where size might be the main |
| 106 | concern as performance is often less critical here. A ~25% size reduction has |
| 107 | been observed in practice. This flag only changes the optimization behavior at |
| 108 | a per-target level and takes precedence over the global CMake build type |
| 109 | (``Release``, ``RelWithDebInfo``) except for ``Debug`` builds, where |
| 110 | optimizations remain disabled. |
| 111 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 112 | .. _ThinLTO: http://clang.llvm.org/docs/ThinLTO.html |
| 113 | |
| 114 | Configuration variables |
| 115 | ----------------------- |
| 116 | |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 117 | By default, pybind11 will compile modules with the compiler default or the |
Henry Schreiner | 1651c32 | 2020-07-30 16:04:26 -0400 | [diff] [blame] | 118 | minimum standard required by pybind11, whichever is higher. You can set the |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 119 | standard explicitly with |
| 120 | `CMAKE_CXX_STANDARD <https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html>`_: |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 121 | |
| 122 | .. code-block:: cmake |
| 123 | |
Henry Schreiner | 1b92cd1 | 2020-07-29 15:02:53 -0400 | [diff] [blame] | 124 | set(CMAKE_CXX_STANDARD 14) # or 11, 14, 17, 20 |
| 125 | set(CMAKE_CXX_STANDARD_REQUIRED ON) # optional, ensure standard is supported |
| 126 | set(CMAKE_CXX_EXTENSIONS OFF) # optional, keep compiler extensionsn off |
Jason Rhinelander | 77710ff | 2017-05-09 14:37:48 -0400 | [diff] [blame] | 127 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 128 | |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 129 | The variables can also be set when calling CMake from the command line using |
| 130 | the ``-D<variable>=<value>`` flag. You can also manually set ``CXX_STANDARD`` |
| 131 | on a target or use ``target_compile_features`` on your targets - anything that |
| 132 | CMake supports. |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 133 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 134 | Classic Python support: The target Python version can be selected by setting |
| 135 | ``PYBIND11_PYTHON_VERSION`` or an exact Python installation can be specified |
| 136 | with ``PYTHON_EXECUTABLE``. For example: |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 137 | |
| 138 | .. code-block:: bash |
| 139 | |
| 140 | cmake -DPYBIND11_PYTHON_VERSION=3.6 .. |
Henry Schreiner | 1b92cd1 | 2020-07-29 15:02:53 -0400 | [diff] [blame] | 141 | |
| 142 | # Another method: |
| 143 | cmake -DPYTHON_EXECUTABLE=/path/to/python .. |
| 144 | |
Henry Schreiner | 1651c32 | 2020-07-30 16:04:26 -0400 | [diff] [blame] | 145 | # This often is a good way to get the current Python, works in environments: |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 146 | cmake -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") .. |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 147 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 148 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 149 | find_package vs. add_subdirectory |
| 150 | --------------------------------- |
| 151 | |
| 152 | For CMake-based projects that don't include the pybind11 repository internally, |
| 153 | an external installation can be detected through ``find_package(pybind11)``. |
| 154 | See the `Config file`_ docstring for details of relevant CMake variables. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 155 | |
| 156 | .. code-block:: cmake |
| 157 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 158 | cmake_minimum_required(VERSION 3.4...3.18) |
| 159 | project(example LANGUAGES CXX) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 160 | |
| 161 | find_package(pybind11 REQUIRED) |
| 162 | pybind11_add_module(example example.cpp) |
| 163 | |
nstelzen | c251434 | 2019-06-10 16:35:36 +0200 | [diff] [blame] | 164 | Note that ``find_package(pybind11)`` will only work correctly if pybind11 |
| 165 | has been correctly installed on the system, e. g. after downloading or cloning |
| 166 | the pybind11 repository : |
| 167 | |
| 168 | .. code-block:: bash |
| 169 | |
Henry Schreiner | 1b92cd1 | 2020-07-29 15:02:53 -0400 | [diff] [blame] | 170 | # Classic CMake |
nstelzen | c251434 | 2019-06-10 16:35:36 +0200 | [diff] [blame] | 171 | cd pybind11 |
| 172 | mkdir build |
| 173 | cd build |
| 174 | cmake .. |
| 175 | make install |
| 176 | |
Henry Schreiner | 1b92cd1 | 2020-07-29 15:02:53 -0400 | [diff] [blame] | 177 | # CMake 3.15+ |
| 178 | cd pybind11 |
| 179 | cmake -S . -B build |
| 180 | cmake --build build -j 2 # Build on 2 cores |
| 181 | cmake --install build |
| 182 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 183 | Once detected, the aforementioned ``pybind11_add_module`` can be employed as |
| 184 | before. The function usage and configuration variables are identical no matter |
| 185 | if pybind11 is added as a subdirectory or found as an installed package. You |
| 186 | can refer to the same [cmake_example]_ repository for a full sample project |
| 187 | -- just swap out ``add_subdirectory`` for ``find_package``. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 188 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 189 | .. _Config file: https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in |
| 190 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 191 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 192 | .. _find-python-mode: |
| 193 | |
| 194 | FindPython mode |
| 195 | --------------- |
| 196 | |
| 197 | CMake 3.12+ (3.15+ recommended) added a new module called FindPython that had a |
| 198 | highly improved search algorithm and modern targets and tools. If you use |
| 199 | FindPython, pybind11 will detect this and use the existing targets instead: |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 200 | |
| 201 | .. code-block:: cmake |
| 202 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 203 | cmake_minumum_required(VERSION 3.15...3.18) |
| 204 | project(example LANGUAGES CXX) |
| 205 | |
| 206 | find_package(Python COMPONENTS Interpreter Development REQUIRED) |
| 207 | find_package(pybind11 CONFIG REQUIRED) |
| 208 | # or add_subdirectory(pybind11) |
| 209 | |
| 210 | pybind11_add_module(example example.cpp) |
| 211 | |
| 212 | You can also use the targets (as listed below) with FindPython. If you define |
| 213 | ``PYBIND11_FINDPYTHON``, pybind11 will perform the FindPython step for you |
| 214 | (mostly useful when building pybind11's own tests, or as a way to change search |
| 215 | algorithms from the CMake invocation, with ``-DPYBIND11_FINDPYTHON=ON``. |
| 216 | |
| 217 | .. warning:: |
| 218 | |
| 219 | If you use FindPython2 and FindPython3 to dual-target Python, use the |
| 220 | individual targets listed below, and avoid targets that directly include |
| 221 | Python parts. |
| 222 | |
| 223 | There are `many ways to hint or force a discovery of a specific Python |
| 224 | installation <https://cmake.org/cmake/help/latest/module/FindPython.html>`_), |
| 225 | setting ``Python_ROOT_DIR`` may be the most common one (though with |
| 226 | virtualenv/venv support, and Conda support, this tends to find the correct |
| 227 | Python version more often than the old system did). |
| 228 | |
| 229 | .. versionadded:: 2.6 |
| 230 | |
| 231 | Advanced: interface library targets |
| 232 | ----------------------------------- |
| 233 | |
| 234 | Pybind11 supports modern CMake usage patterns with a set of interface targets, |
| 235 | available in all modes. The targets provided are: |
| 236 | |
| 237 | ``pybind11::headers`` |
| 238 | Just the pybind11 headers and minimum compile requirements |
| 239 | |
| 240 | ``pybind11::python2_no_register`` |
| 241 | Quiets the warning/error when mixing C++14 or higher and Python 2 |
| 242 | |
| 243 | ``pybind11::pybind11`` |
| 244 | Python headers + ``pybind11::headers`` + ``pybind11::python2_no_register`` (Python 2 only) |
| 245 | |
| 246 | ``pybind11::python_link_helper`` |
| 247 | Just the "linking" part of pybind11:module |
| 248 | |
| 249 | ``pybind11::module`` |
| 250 | Everything for extension modules - ``pybind11::pybind11`` + ``Python::Module`` (FindPython CMake 3.15+) or ``pybind11::python_link_helper`` |
| 251 | |
| 252 | ``pybind11::embed`` |
| 253 | Everything for embedding the Python interpreter - ``pybind11::pybind11`` + ``Python::Embed`` (FindPython) or Python libs |
| 254 | |
| 255 | ``pybind11::lto`` / ``pybind11::thin_lto`` |
| 256 | An alternative to `INTERPROCEDURAL_OPTIMIZATION` for adding link-time optimization. |
| 257 | |
| 258 | ``pybind11::windows_extras`` |
| 259 | ``/bigobj`` and ``/mp`` for MSVC. |
| 260 | |
Wenzel Jakob | 36c666f | 2020-09-04 23:31:05 +0200 | [diff] [blame] | 261 | ``pybind11::opt_size`` |
| 262 | ``/Os`` for MSVC, ``-Os`` for other compilers. Does nothing for debug builds. |
| 263 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 264 | Two helper functions are also provided: |
| 265 | |
| 266 | ``pybind11_strip(target)`` |
| 267 | Strips a target (uses ``CMAKE_STRIP`` after the target is built) |
| 268 | |
| 269 | ``pybind11_extension(target)`` |
| 270 | Sets the correct extension (with SOABI) for a target. |
| 271 | |
| 272 | You can use these targets to build complex applications. For example, the |
| 273 | ``add_python_module`` function is identical to: |
| 274 | |
| 275 | .. code-block:: cmake |
| 276 | |
| 277 | cmake_minimum_required(VERSION 3.4) |
| 278 | project(example LANGUAGES CXX) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 279 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 280 | find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 281 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 282 | add_library(example MODULE main.cpp) |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 283 | |
| 284 | target_link_libraries(example PRIVATE pybind11::module pybind11::lto pybind11::windows_extras) |
| 285 | |
| 286 | pybind11_extension(example) |
| 287 | pybind11_strip(example) |
| 288 | |
| 289 | set_target_properties(example PROPERTIES CXX_VISIBILITY_PRESET "hidden" |
| 290 | CUDA_VISIBILITY_PRESET "hidden") |
| 291 | |
| 292 | Instead of setting properties, you can set ``CMAKE_*`` variables to initialize these correctly. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 293 | |
| 294 | .. warning:: |
| 295 | |
| 296 | Since pybind11 is a metatemplate library, it is crucial that certain |
| 297 | compiler flags are provided to ensure high quality code generation. In |
| 298 | contrast to the ``pybind11_add_module()`` command, the CMake interface |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 299 | provides a *composable* set of targets to ensure that you retain flexibility. |
| 300 | It can be expecially important to provide or set these properties; the |
| 301 | :ref:`FAQ <faq:symhidden>` contains an explanation on why these are needed. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 302 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 303 | .. versionadded:: 2.6 |
Wenzel Jakob | f3de2d5 | 2016-12-26 13:34:28 +0100 | [diff] [blame] | 304 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 305 | .. _nopython-mode: |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 306 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 307 | Advanced: NOPYTHON mode |
| 308 | ----------------------- |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 309 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 310 | If you want complete control, you can set ``PYBIND11_NOPYTHON`` to completely |
| 311 | disable Python integration (this also happens if you run ``FindPython2`` and |
| 312 | ``FindPython3`` without running ``FindPython``). This gives you complete |
| 313 | freedom to integrate into an existing system (like `Scikit-Build's |
| 314 | <https://scikit-build.readthedocs.io>`_ ``PythonExtensions``). |
| 315 | ``pybind11_add_module`` and ``pybind11_extension`` will be unavailable, and the |
| 316 | targets will be missing any Python specific behavior. |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 317 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 318 | .. versionadded:: 2.6 |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame] | 319 | |
Dean Moldovan | 6d2411f | 2017-04-22 23:24:13 +0200 | [diff] [blame] | 320 | Embedding the Python interpreter |
| 321 | -------------------------------- |
| 322 | |
| 323 | In addition to extension modules, pybind11 also supports embedding Python into |
| 324 | a C++ executable or library. In CMake, simply link with the ``pybind11::embed`` |
| 325 | target. It provides everything needed to get the interpreter running. The Python |
| 326 | headers and libraries are attached to the target. Unlike ``pybind11::module``, |
| 327 | there is no need to manually set any additional properties here. For more |
| 328 | information about usage in C++, see :doc:`/advanced/embedding`. |
| 329 | |
| 330 | .. code-block:: cmake |
| 331 | |
Henry Schreiner | 1729aae | 2020-08-19 12:26:26 -0400 | [diff] [blame] | 332 | cmake_minimum_required(VERSION 3.4...3.18) |
| 333 | project(example LANGUAGES CXX) |
Dean Moldovan | 6d2411f | 2017-04-22 23:24:13 +0200 | [diff] [blame] | 334 | |
| 335 | find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11) |
| 336 | |
Dean Moldovan | 8f6c129 | 2017-05-31 13:48:39 +0200 | [diff] [blame] | 337 | add_executable(example main.cpp) |
Dean Moldovan | 6d2411f | 2017-04-22 23:24:13 +0200 | [diff] [blame] | 338 | target_link_libraries(example PRIVATE pybind11::embed) |
| 339 | |
Ivan Smirnov | 5cbfda5 | 2017-08-30 20:58:43 +0100 | [diff] [blame] | 340 | .. _building_manually: |
| 341 | |
| 342 | Building manually |
| 343 | ================= |
| 344 | |
| 345 | pybind11 is a header-only library, hence it is not necessary to link against |
| 346 | any special libraries and there are no intermediate (magic) translation steps. |
| 347 | |
| 348 | On Linux, you can compile an example such as the one given in |
| 349 | :ref:`simple_example` using the following command: |
| 350 | |
| 351 | .. code-block:: bash |
| 352 | |
| 353 | $ c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix` |
| 354 | |
| 355 | The flags given here assume that you're using Python 3. For Python 2, just |
| 356 | change the executable appropriately (to ``python`` or ``python2``). |
| 357 | |
| 358 | The ``python3 -m pybind11 --includes`` command fetches the include paths for |
| 359 | both pybind11 and Python headers. This assumes that pybind11 has been installed |
| 360 | using ``pip`` or ``conda``. If it hasn't, you can also manually specify |
| 361 | ``-I <path-to-pybind11>/include`` together with the Python includes path |
| 362 | ``python3-config --includes``. |
| 363 | |
| 364 | Note that Python 2.7 modules don't use a special suffix, so you should simply |
| 365 | use ``example.so`` instead of ``example`python3-config --extension-suffix```. |
| 366 | Besides, the ``--extension-suffix`` option may or may not be available, depending |
| 367 | on the distribution; in the latter case, the module extension can be manually |
| 368 | set to ``.so``. |
| 369 | |
| 370 | On Mac OS: the build command is almost the same but it also requires passing |
| 371 | the ``-undefined dynamic_lookup`` flag so as to ignore missing symbols when |
| 372 | building the module: |
| 373 | |
| 374 | .. code-block:: bash |
| 375 | |
| 376 | $ c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix` |
| 377 | |
| 378 | In general, it is advisable to include several additional build parameters |
| 379 | that can considerably reduce the size of the created binary. Refer to section |
| 380 | :ref:`cmake` for a detailed example of a suitable cross-platform CMake-based |
| 381 | build system that works on all platforms including Windows. |
| 382 | |
| 383 | .. note:: |
| 384 | |
| 385 | On Linux and macOS, it's better to (intentionally) not link against |
| 386 | ``libpython``. The symbols will be resolved when the extension library |
| 387 | is loaded into a Python binary. This is preferable because you might |
| 388 | have several different installations of a given Python version (e.g. the |
| 389 | system-provided Python, and one that ships with a piece of commercial |
| 390 | software). In this way, the plugin will work with both versions, instead |
| 391 | of possibly importing a second Python library into a process that already |
| 392 | contains one (which will lead to a segfault). |
Dean Moldovan | 6d2411f | 2017-04-22 23:24:13 +0200 | [diff] [blame] | 393 | |
Griffin Downs | a4cee36 | 2020-09-16 05:07:06 -0700 | [diff] [blame^] | 394 | |
| 395 | Building with vcpkg |
| 396 | =================== |
| 397 | You can download and install pybind11 using the Microsoft `vcpkg |
| 398 | <https://github.com/Microsoft/vcpkg/>`_ dependency manager: |
| 399 | |
| 400 | .. code-block:: bash |
| 401 | |
| 402 | git clone https://github.com/Microsoft/vcpkg.git |
| 403 | cd vcpkg |
| 404 | ./bootstrap-vcpkg.sh |
| 405 | ./vcpkg integrate install |
| 406 | vcpkg install pybind11 |
| 407 | |
| 408 | The pybind11 port in vcpkg is kept up to date by Microsoft team members and |
| 409 | community contributors. If the version is out of date, please `create an issue |
| 410 | or pull request <https://github.com/Microsoft/vcpkg/>`_ on the vcpkg |
| 411 | repository. |
| 412 | |
Wenzel Jakob | f3de2d5 | 2016-12-26 13:34:28 +0100 | [diff] [blame] | 413 | Generating binding code automatically |
| 414 | ===================================== |
| 415 | |
| 416 | The ``Binder`` project is a tool for automatic generation of pybind11 binding |
| 417 | code by introspecting existing C++ codebases using LLVM/Clang. See the |
| 418 | [binder]_ documentation for details. |
| 419 | |
| 420 | .. [binder] http://cppbinder.readthedocs.io/en/latest/about.html |
Dustin Spicuzza | 2c4cd84 | 2019-11-24 02:36:48 -0500 | [diff] [blame] | 421 | |
| 422 | [AutoWIG]_ is a Python library that wraps automatically compiled libraries into |
| 423 | high-level languages. It parses C++ code using LLVM/Clang technologies and |
| 424 | generates the wrappers using the Mako templating engine. The approach is automatic, |
| 425 | extensible, and applies to very complex C++ libraries, composed of thousands of |
| 426 | classes or incorporating modern meta-programming constructs. |
| 427 | |
| 428 | .. [AutoWIG] https://github.com/StatisKit/AutoWIG |
Dustin Spicuzza | 6f3470f | 2020-08-10 16:10:45 -0400 | [diff] [blame] | 429 | |
| 430 | [robotpy-build]_ is a is a pure python, cross platform build tool that aims to |
| 431 | simplify creation of python wheels for pybind11 projects, and provide |
| 432 | cross-project dependency management. Additionally, it is able to autogenerate |
| 433 | customizable pybind11-based wrappers by parsing C++ header files. |
| 434 | |
| 435 | .. [robotpy-build] https://robotpy-build.readthedocs.io |