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 | |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 36 | cmake_minimum_required(VERSION 2.8.12) |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 37 | project(example) |
| 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 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 53 | pybind11_add_module |
| 54 | ------------------- |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 55 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 56 | To ease the creation of Python extension modules, pybind11 provides a CMake |
| 57 | function with the following signature: |
| 58 | |
| 59 | .. code-block:: cmake |
| 60 | |
| 61 | pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL] |
| 62 | [NO_EXTRAS] [THIN_LTO] source1 [source2 ...]) |
| 63 | |
| 64 | This function behaves very much like CMake's builtin ``add_library`` (in fact, |
| 65 | it's a wrapper function around that command). It will add a library target |
| 66 | called ``<name>`` to be built from the listed source files. In addition, it |
| 67 | will take care of all the Python-specific compiler and linker flags as well |
| 68 | as the OS- and Python-version-specific file extension. The produced target |
| 69 | ``<name>`` can be further manipulated with regular CMake commands. |
| 70 | |
| 71 | ``MODULE`` or ``SHARED`` may be given to specify the type of library. If no |
| 72 | type is given, ``MODULE`` is used by default which ensures the creation of a |
| 73 | Python-exclusive module. Specifying ``SHARED`` will create a more traditional |
| 74 | dynamic library which can also be linked from elsewhere. ``EXCLUDE_FROM_ALL`` |
| 75 | removes this target from the default build (see CMake docs for details). |
| 76 | |
| 77 | Since pybind11 is a template library, ``pybind11_add_module`` adds compiler |
| 78 | flags to ensure high quality code generation without bloat arising from long |
Jason Rhinelander | 97aa54f | 2017-08-10 12:08:42 -0400 | [diff] [blame] | 79 | symbol names and duplication of code in different translation units. It |
| 80 | sets default visibility to *hidden*, which is required for some pybind11 |
| 81 | features and functionality when attempting to load multiple pybind11 modules |
| 82 | compiled under different pybind11 versions. It also adds additional flags |
| 83 | enabling LTO (Link Time Optimization) and strip unneeded symbols. See the |
| 84 | :ref:`FAQ entry <faq:symhidden>` for a more detailed explanation. These |
| 85 | latter optimizations are never applied in ``Debug`` mode. If ``NO_EXTRAS`` is |
| 86 | given, they will always be disabled, even in ``Release`` mode. However, this |
| 87 | will result in code bloat and is generally not recommended. |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 88 | |
| 89 | As stated above, LTO is enabled by default. Some newer compilers also support |
| 90 | different flavors of LTO such as `ThinLTO`_. Setting ``THIN_LTO`` will cause |
| 91 | the function to prefer this flavor if available. The function falls back to |
| 92 | regular LTO if ``-flto=thin`` is not available. |
| 93 | |
| 94 | .. _ThinLTO: http://clang.llvm.org/docs/ThinLTO.html |
| 95 | |
| 96 | Configuration variables |
| 97 | ----------------------- |
| 98 | |
Jason Rhinelander | 77710ff | 2017-05-09 14:37:48 -0400 | [diff] [blame] | 99 | By default, pybind11 will compile modules with the C++14 standard, if available |
| 100 | on the target compiler, falling back to C++11 if C++14 support is not |
| 101 | available. Note, however, that this default is subject to change: future |
| 102 | pybind11 releases are expected to migrate to newer C++ standards as they become |
| 103 | available. To override this, the standard flag can be given explicitly in |
| 104 | ``PYBIND11_CPP_STANDARD``: |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 105 | |
| 106 | .. code-block:: cmake |
| 107 | |
Jason Rhinelander | 77710ff | 2017-05-09 14:37:48 -0400 | [diff] [blame] | 108 | # Use just one of these: |
| 109 | # GCC/clang: |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 110 | set(PYBIND11_CPP_STANDARD -std=c++11) |
Jason Rhinelander | 77710ff | 2017-05-09 14:37:48 -0400 | [diff] [blame] | 111 | set(PYBIND11_CPP_STANDARD -std=c++14) |
| 112 | set(PYBIND11_CPP_STANDARD -std=c++1z) # Experimental C++17 support |
| 113 | # MSVC: |
| 114 | set(PYBIND11_CPP_STANDARD /std:c++14) |
| 115 | set(PYBIND11_CPP_STANDARD /std:c++latest) # Enables some MSVC C++17 features |
| 116 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 117 | add_subdirectory(pybind11) # or find_package(pybind11) |
| 118 | |
| 119 | Note that this and all other configuration variables must be set **before** the |
Jason Rhinelander | 77710ff | 2017-05-09 14:37:48 -0400 | [diff] [blame] | 120 | call to ``add_subdirectory`` or ``find_package``. The variables can also be set |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 121 | when calling CMake from the command line using the ``-D<variable>=<value>`` flag. |
| 122 | |
| 123 | The target Python version can be selected by setting ``PYBIND11_PYTHON_VERSION`` |
| 124 | or an exact Python installation can be specified with ``PYTHON_EXECUTABLE``. |
| 125 | For example: |
| 126 | |
| 127 | .. code-block:: bash |
| 128 | |
| 129 | cmake -DPYBIND11_PYTHON_VERSION=3.6 .. |
| 130 | # or |
| 131 | cmake -DPYTHON_EXECUTABLE=path/to/python .. |
| 132 | |
| 133 | find_package vs. add_subdirectory |
| 134 | --------------------------------- |
| 135 | |
| 136 | For CMake-based projects that don't include the pybind11 repository internally, |
| 137 | an external installation can be detected through ``find_package(pybind11)``. |
| 138 | See the `Config file`_ docstring for details of relevant CMake variables. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 139 | |
| 140 | .. code-block:: cmake |
| 141 | |
| 142 | cmake_minimum_required(VERSION 2.8.12) |
| 143 | project(example) |
| 144 | |
| 145 | find_package(pybind11 REQUIRED) |
| 146 | pybind11_add_module(example example.cpp) |
| 147 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 148 | Once detected, the aforementioned ``pybind11_add_module`` can be employed as |
| 149 | before. The function usage and configuration variables are identical no matter |
| 150 | if pybind11 is added as a subdirectory or found as an installed package. You |
| 151 | can refer to the same [cmake_example]_ repository for a full sample project |
| 152 | -- just swap out ``add_subdirectory`` for ``find_package``. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 153 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 154 | .. _Config file: https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in |
| 155 | |
| 156 | Advanced: interface library target |
| 157 | ---------------------------------- |
| 158 | |
| 159 | When using a version of CMake greater than 3.0, pybind11 can additionally |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 160 | be used as a special *interface library* . The target ``pybind11::module`` |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 161 | is available with pybind11 headers, Python headers and libraries as needed, |
| 162 | and C++ compile definitions attached. This target is suitable for linking |
| 163 | to an independently constructed (through ``add_library``, not |
| 164 | ``pybind11_add_module``) target in the consuming project. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 165 | |
| 166 | .. code-block:: cmake |
| 167 | |
| 168 | cmake_minimum_required(VERSION 3.0) |
| 169 | project(example) |
| 170 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 171 | find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 172 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 173 | add_library(example MODULE main.cpp) |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 174 | target_link_libraries(example PRIVATE pybind11::module) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 175 | set_target_properties(example PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}" |
| 176 | SUFFIX "${PYTHON_MODULE_EXTENSION}") |
| 177 | |
| 178 | .. warning:: |
| 179 | |
| 180 | Since pybind11 is a metatemplate library, it is crucial that certain |
| 181 | compiler flags are provided to ensure high quality code generation. In |
| 182 | contrast to the ``pybind11_add_module()`` command, the CMake interface |
| 183 | library only provides the *minimal* set of parameters to ensure that the |
| 184 | code using pybind11 compiles, but it does **not** pass these extra compiler |
| 185 | flags (i.e. this is up to you). |
| 186 | |
| 187 | These include Link Time Optimization (``-flto`` on GCC/Clang/ICPC, ``/GL`` |
Jason Rhinelander | 97aa54f | 2017-08-10 12:08:42 -0400 | [diff] [blame] | 188 | and ``/LTCG`` on Visual Studio) and .OBJ files with many sections on Visual |
| 189 | Studio (``/bigobj``). The :ref:`FAQ <faq:symhidden>` contains an |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 190 | explanation on why these are needed. |
Wenzel Jakob | f3de2d5 | 2016-12-26 13:34:28 +0100 | [diff] [blame] | 191 | |
Dean Moldovan | 6d2411f | 2017-04-22 23:24:13 +0200 | [diff] [blame] | 192 | Embedding the Python interpreter |
| 193 | -------------------------------- |
| 194 | |
| 195 | In addition to extension modules, pybind11 also supports embedding Python into |
| 196 | a C++ executable or library. In CMake, simply link with the ``pybind11::embed`` |
| 197 | target. It provides everything needed to get the interpreter running. The Python |
| 198 | headers and libraries are attached to the target. Unlike ``pybind11::module``, |
| 199 | there is no need to manually set any additional properties here. For more |
| 200 | information about usage in C++, see :doc:`/advanced/embedding`. |
| 201 | |
| 202 | .. code-block:: cmake |
| 203 | |
| 204 | cmake_minimum_required(VERSION 3.0) |
| 205 | project(example) |
| 206 | |
| 207 | find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11) |
| 208 | |
Dean Moldovan | 8f6c129 | 2017-05-31 13:48:39 +0200 | [diff] [blame] | 209 | add_executable(example main.cpp) |
Dean Moldovan | 6d2411f | 2017-04-22 23:24:13 +0200 | [diff] [blame] | 210 | target_link_libraries(example PRIVATE pybind11::embed) |
| 211 | |
Ivan Smirnov | 5cbfda5 | 2017-08-30 20:58:43 +0100 | [diff] [blame^] | 212 | .. _building_manually: |
| 213 | |
| 214 | Building manually |
| 215 | ================= |
| 216 | |
| 217 | pybind11 is a header-only library, hence it is not necessary to link against |
| 218 | any special libraries and there are no intermediate (magic) translation steps. |
| 219 | |
| 220 | On Linux, you can compile an example such as the one given in |
| 221 | :ref:`simple_example` using the following command: |
| 222 | |
| 223 | .. code-block:: bash |
| 224 | |
| 225 | $ c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix` |
| 226 | |
| 227 | The flags given here assume that you're using Python 3. For Python 2, just |
| 228 | change the executable appropriately (to ``python`` or ``python2``). |
| 229 | |
| 230 | The ``python3 -m pybind11 --includes`` command fetches the include paths for |
| 231 | both pybind11 and Python headers. This assumes that pybind11 has been installed |
| 232 | using ``pip`` or ``conda``. If it hasn't, you can also manually specify |
| 233 | ``-I <path-to-pybind11>/include`` together with the Python includes path |
| 234 | ``python3-config --includes``. |
| 235 | |
| 236 | Note that Python 2.7 modules don't use a special suffix, so you should simply |
| 237 | use ``example.so`` instead of ``example`python3-config --extension-suffix```. |
| 238 | Besides, the ``--extension-suffix`` option may or may not be available, depending |
| 239 | on the distribution; in the latter case, the module extension can be manually |
| 240 | set to ``.so``. |
| 241 | |
| 242 | On Mac OS: the build command is almost the same but it also requires passing |
| 243 | the ``-undefined dynamic_lookup`` flag so as to ignore missing symbols when |
| 244 | building the module: |
| 245 | |
| 246 | .. code-block:: bash |
| 247 | |
| 248 | $ c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix` |
| 249 | |
| 250 | In general, it is advisable to include several additional build parameters |
| 251 | that can considerably reduce the size of the created binary. Refer to section |
| 252 | :ref:`cmake` for a detailed example of a suitable cross-platform CMake-based |
| 253 | build system that works on all platforms including Windows. |
| 254 | |
| 255 | .. note:: |
| 256 | |
| 257 | On Linux and macOS, it's better to (intentionally) not link against |
| 258 | ``libpython``. The symbols will be resolved when the extension library |
| 259 | is loaded into a Python binary. This is preferable because you might |
| 260 | have several different installations of a given Python version (e.g. the |
| 261 | system-provided Python, and one that ships with a piece of commercial |
| 262 | software). In this way, the plugin will work with both versions, instead |
| 263 | of possibly importing a second Python library into a process that already |
| 264 | contains one (which will lead to a segfault). |
Dean Moldovan | 6d2411f | 2017-04-22 23:24:13 +0200 | [diff] [blame] | 265 | |
Wenzel Jakob | f3de2d5 | 2016-12-26 13:34:28 +0100 | [diff] [blame] | 266 | Generating binding code automatically |
| 267 | ===================================== |
| 268 | |
| 269 | The ``Binder`` project is a tool for automatic generation of pybind11 binding |
| 270 | code by introspecting existing C++ codebases using LLVM/Clang. See the |
| 271 | [binder]_ documentation for details. |
| 272 | |
| 273 | .. [binder] http://cppbinder.readthedocs.io/en/latest/about.html |