blob: b9bf134bba2e9cdaa5331cf6b2abb478ffabf622 [file] [log] [blame]
Ivan Smirnov5cbfda52017-08-30 20:58:43 +01001.. _compiling:
2
Wenzel Jakob4a48afb2016-03-09 21:31:21 +01003Build systems
4#############
5
Henry Schreinerfd61f502020-09-16 17:13:41 -04006.. _build-setuptools:
7
Wenzel Jakob4a48afb2016-03-09 21:31:21 +01008Building with setuptools
9========================
10
11For projects on PyPI, building with setuptools is the way to go. Sylvain Corlay
12has kindly provided an example project which shows how to set up everything,
13including automatic generation of documentation using Sphinx. Please refer to
Wenzel Jakobca8dc082016-06-03 14:24:17 +020014the [python_example]_ repository.
Wenzel Jakob4a48afb2016-03-09 21:31:21 +010015
Wenzel Jakobca8dc082016-06-03 14:24:17 +020016.. [python_example] https://github.com/pybind/python_example
Wenzel Jakob4a48afb2016-03-09 21:31:21 +010017
Henry Schreinerfd61f502020-09-16 17:13:41 -040018A helper file is provided with pybind11 that can simplify usage with setuptools.
19
20To use pybind11 inside your ``setup.py``, you have to have some system to
21ensure that ``pybind11`` is installed when you build your package. There are
22four possible ways to do this, and pybind11 supports all four: You can ask all
23users to install pybind11 beforehand (bad), you can use
24:ref:`setup_helpers-pep518` (good, but very new and requires Pip 10),
25:ref:`setup_helpers-setup_requires` (discouraged by Python packagers now that
26PEP 518 is available, but it still works everywhere), or you can
27:ref:`setup_helpers-copy-manually` (always works but you have to manually sync
28your copy to get updates).
29
30An example of a ``setup.py`` using pybind11's helpers:
31
32.. code-block:: python
33
34 from setuptools import setup
35 from pybind11.setup_helpers import Pybind11Extension
36
37 ext_modules = [
38 Pybind11Extension(
39 "python_example",
40 ["src/main.cpp"],
41 ),
42 ]
43
44 setup(
45 ...,
46 ext_modules=ext_modules
47 )
48
49If you want to do an automatic search for the highest supported C++ standard,
50that is supported via a ``build_ext`` command override; it will only affect
51``Pybind11Extensions``:
52
53.. code-block:: python
54
55 from setuptools import setup
56 from pybind11.setup_helpers import Pybind11Extension, build_ext
57
58 ext_modules = [
59 Pybind11Extension(
60 "python_example",
61 ["src/main.cpp"],
62 ),
63 ]
64
65 setup(
66 ...,
67 cmdclass={"build_ext": build_ext},
68 ext_modules=ext_modules
69 )
70
71.. _setup_helpers-pep518:
72
73PEP 518 requirements (Pip 10+ required)
74---------------------------------------
75
76If you use `PEP 518's <https://www.python.org/dev/peps/pep-0518/>`_
77``pyproject.toml`` file, you can ensure that ``pybind11`` is available during
78the compilation of your project. When this file exists, Pip will make a new
79virtual environment, download just the packages listed here in ``requires=``,
80and build a wheel (binary Python package). It will then throw away the
81environment, and install your wheel.
82
83Your ``pyproject.toml`` file will likely look something like this:
84
85.. code-block:: toml
86
87 [build-system]
88 requires = ["setuptools", "wheel", "pybind11==2.6.0"]
89 build-backend = "setuptools.build_meta"
90
91.. note::
92
93 The main drawback to this method is that a `PEP 517`_ compliant build tool,
94 such as Pip 10+, is required for this approach to work; older versions of
95 Pip completely ignore this file. If you distribute binaries (called wheels
96 in Python) using something like `cibuildwheel`_, remember that ``setup.py``
97 and ``pyproject.toml`` are not even contained in the wheel, so this high
98 Pip requirement is only for source builds, and will not affect users of
99 your binary wheels.
100
101.. _PEP 517: https://www.python.org/dev/peps/pep-0517/
102.. _cibuildwheel: https://cibuildwheel.readthedocs.io
103
104.. _setup_helpers-setup_requires:
105
106Classic ``setup_requires``
107--------------------------
108
109If you want to support old versions of Pip with the classic
110``setup_requires=["pybind11"]`` keyword argument to setup, which triggers a
111two-phase ``setup.py`` run, then you will need to use something like this to
112ensure the first pass works (which has not yet installed the ``setup_requires``
113packages, since it can't install something it does not know about):
114
115.. code-block:: python
116
117 try:
118 from pybind11.setup_helpers import Pybind11Extension
119 except ImportError:
120 from setuptools import Extension as Pybind11Extension
121
122
123It doesn't matter that the Extension class is not the enhanced subclass for the
124first pass run; and the second pass will have the ``setup_requires``
125requirements.
126
127This is obviously more of a hack than the PEP 518 method, but it supports
128ancient versions of Pip.
129
130.. _setup_helpers-copy-manually:
131
132Copy manually
133-------------
134
135You can also copy ``setup_helpers.py`` directly to your project; it was
136designed to be usable standalone, like the old example ``setup.py``. You can
137set ``include_pybind11=False`` to skip including the pybind11 package headers,
138so you can use it with git submodules and a specific git version. If you use
139this, you will need to import from a local file in ``setup.py`` and ensure the
140helper file is part of your MANIFEST.
141
142
143.. versionchanged:: 2.6
144
145 Added ``setup_helpers`` file.
146
Wenzel Jakoba439cca2016-05-17 10:47:52 +0200147Building with cppimport
148========================
149
Dean Moldovan8665ee82017-08-17 15:01:43 +0200150[cppimport]_ is a small Python import hook that determines whether there is a C++
151source file whose name matches the requested module. If there is, the file is
152compiled as a Python extension using pybind11 and placed in the same folder as
153the C++ source file. Python is then able to find the module and load it.
Wenzel Jakoba439cca2016-05-17 10:47:52 +0200154
155.. [cppimport] https://github.com/tbenthompson/cppimport
156
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200157.. _cmake:
158
159Building with CMake
160===================
161
Wenzel Jakobfe342412016-09-06 13:02:29 +0900162For C++ codebases that have an existing CMake-based build system, a Python
Dean Moldovan24ddf4b2016-05-27 00:11:52 +0200163extension module can be created with just a few lines of code:
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200164
165.. code-block:: cmake
166
Henry Schreiner1729aae2020-08-19 12:26:26 -0400167 cmake_minimum_required(VERSION 3.4...3.18)
168 project(example LANGUAGES CXX)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200169
Dean Moldovan24ddf4b2016-05-27 00:11:52 +0200170 add_subdirectory(pybind11)
171 pybind11_add_module(example example.cpp)
Wenzel Jakobf64feaf2016-04-28 14:33:45 +0200172
Wenzel Jakobfe342412016-09-06 13:02:29 +0900173This assumes that the pybind11 repository is located in a subdirectory named
Dean Moldovan24ddf4b2016-05-27 00:11:52 +0200174:file:`pybind11` and that the code is located in a file named :file:`example.cpp`.
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100175The CMake command ``add_subdirectory`` will import the pybind11 project which
176provides the ``pybind11_add_module`` function. It will take care of all the
177details needed to build a Python extension module on any platform.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200178
Dean Moldovan24ddf4b2016-05-27 00:11:52 +0200179A working sample project, including a way to invoke CMake from :file:`setup.py` for
180PyPI integration, can be found in the [cmake_example]_ repository.
Wenzel Jakobcaa9d442016-01-17 22:36:34 +0100181
Wenzel Jakobaa79af02016-06-03 12:23:24 +0200182.. [cmake_example] https://github.com/pybind/cmake_example
Lori A. Burns5cafc992016-12-13 10:55:38 -0500183
Henry Schreiner1729aae2020-08-19 12:26:26 -0400184.. versionchanged:: 2.6
185 CMake 3.4+ is required.
186
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100187pybind11_add_module
188-------------------
Lori A. Burns5cafc992016-12-13 10:55:38 -0500189
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100190To ease the creation of Python extension modules, pybind11 provides a CMake
191function with the following signature:
192
193.. code-block:: cmake
194
195 pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL]
Wenzel Jakob36c666f2020-09-04 23:31:05 +0200196 [NO_EXTRAS] [THIN_LTO] [OPT_SIZE] source1 [source2 ...])
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100197
198This function behaves very much like CMake's builtin ``add_library`` (in fact,
199it's a wrapper function around that command). It will add a library target
200called ``<name>`` to be built from the listed source files. In addition, it
201will take care of all the Python-specific compiler and linker flags as well
202as the OS- and Python-version-specific file extension. The produced target
203``<name>`` can be further manipulated with regular CMake commands.
204
205``MODULE`` or ``SHARED`` may be given to specify the type of library. If no
206type is given, ``MODULE`` is used by default which ensures the creation of a
207Python-exclusive module. Specifying ``SHARED`` will create a more traditional
208dynamic library which can also be linked from elsewhere. ``EXCLUDE_FROM_ALL``
209removes this target from the default build (see CMake docs for details).
210
211Since pybind11 is a template library, ``pybind11_add_module`` adds compiler
212flags to ensure high quality code generation without bloat arising from long
Jason Rhinelander97aa54f2017-08-10 12:08:42 -0400213symbol names and duplication of code in different translation units. It
214sets default visibility to *hidden*, which is required for some pybind11
215features and functionality when attempting to load multiple pybind11 modules
216compiled under different pybind11 versions. It also adds additional flags
217enabling LTO (Link Time Optimization) and strip unneeded symbols. See the
218:ref:`FAQ entry <faq:symhidden>` for a more detailed explanation. These
219latter optimizations are never applied in ``Debug`` mode. If ``NO_EXTRAS`` is
220given, they will always be disabled, even in ``Release`` mode. However, this
221will result in code bloat and is generally not recommended.
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100222
223As stated above, LTO is enabled by default. Some newer compilers also support
224different flavors of LTO such as `ThinLTO`_. Setting ``THIN_LTO`` will cause
225the function to prefer this flavor if available. The function falls back to
Henry Schreiner1729aae2020-08-19 12:26:26 -0400226regular LTO if ``-flto=thin`` is not available. If
227``CMAKE_INTERPROCEDURAL_OPTIMIZATION`` is set (either ON or OFF), then that
228will be respected instead of the built-in flag search.
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100229
Wenzel Jakob36c666f2020-09-04 23:31:05 +0200230The ``OPT_SIZE`` flag enables size-based optimization equivalent to the
231standard ``/Os`` or ``-Os`` compiler flags and the ``MinSizeRel`` build type,
232which avoid optimizations that that can substantially increase the size of the
233resulting binary. This flag is particularly useful in projects that are split
234into performance-critical parts and associated bindings. In this case, we can
235compile the project in release mode (and hence, optimize performance globally),
236and specify ``OPT_SIZE`` for the binding target, where size might be the main
237concern as performance is often less critical here. A ~25% size reduction has
238been observed in practice. This flag only changes the optimization behavior at
239a per-target level and takes precedence over the global CMake build type
240(``Release``, ``RelWithDebInfo``) except for ``Debug`` builds, where
241optimizations remain disabled.
242
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100243.. _ThinLTO: http://clang.llvm.org/docs/ThinLTO.html
244
245Configuration variables
246-----------------------
247
Henry Schreiner6ec17752020-07-28 00:43:12 -0400248By default, pybind11 will compile modules with the compiler default or the
Henry Schreiner1651c322020-07-30 16:04:26 -0400249minimum standard required by pybind11, whichever is higher. You can set the
Henry Schreiner6ec17752020-07-28 00:43:12 -0400250standard explicitly with
251`CMAKE_CXX_STANDARD <https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html>`_:
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100252
253.. code-block:: cmake
254
Henry Schreiner1b92cd12020-07-29 15:02:53 -0400255 set(CMAKE_CXX_STANDARD 14) # or 11, 14, 17, 20
256 set(CMAKE_CXX_STANDARD_REQUIRED ON) # optional, ensure standard is supported
257 set(CMAKE_CXX_EXTENSIONS OFF) # optional, keep compiler extensionsn off
Jason Rhinelander77710ff2017-05-09 14:37:48 -0400258
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100259
Henry Schreiner6ec17752020-07-28 00:43:12 -0400260The variables can also be set when calling CMake from the command line using
261the ``-D<variable>=<value>`` flag. You can also manually set ``CXX_STANDARD``
262on a target or use ``target_compile_features`` on your targets - anything that
263CMake supports.
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100264
Henry Schreiner1729aae2020-08-19 12:26:26 -0400265Classic Python support: The target Python version can be selected by setting
266``PYBIND11_PYTHON_VERSION`` or an exact Python installation can be specified
267with ``PYTHON_EXECUTABLE``. For example:
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100268
269.. code-block:: bash
270
271 cmake -DPYBIND11_PYTHON_VERSION=3.6 ..
Henry Schreiner1b92cd12020-07-29 15:02:53 -0400272
273 # Another method:
274 cmake -DPYTHON_EXECUTABLE=/path/to/python ..
275
Henry Schreiner1651c322020-07-30 16:04:26 -0400276 # This often is a good way to get the current Python, works in environments:
Henry Schreiner6ec17752020-07-28 00:43:12 -0400277 cmake -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") ..
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100278
Henry Schreiner1729aae2020-08-19 12:26:26 -0400279
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100280find_package vs. add_subdirectory
281---------------------------------
282
283For CMake-based projects that don't include the pybind11 repository internally,
284an external installation can be detected through ``find_package(pybind11)``.
285See the `Config file`_ docstring for details of relevant CMake variables.
Lori A. Burns5cafc992016-12-13 10:55:38 -0500286
287.. code-block:: cmake
288
Henry Schreiner1729aae2020-08-19 12:26:26 -0400289 cmake_minimum_required(VERSION 3.4...3.18)
290 project(example LANGUAGES CXX)
Lori A. Burns5cafc992016-12-13 10:55:38 -0500291
292 find_package(pybind11 REQUIRED)
293 pybind11_add_module(example example.cpp)
294
nstelzenc2514342019-06-10 16:35:36 +0200295Note that ``find_package(pybind11)`` will only work correctly if pybind11
296has been correctly installed on the system, e. g. after downloading or cloning
297the pybind11 repository :
298
299.. code-block:: bash
300
Henry Schreiner1b92cd12020-07-29 15:02:53 -0400301 # Classic CMake
nstelzenc2514342019-06-10 16:35:36 +0200302 cd pybind11
303 mkdir build
304 cd build
305 cmake ..
306 make install
307
Henry Schreiner1b92cd12020-07-29 15:02:53 -0400308 # CMake 3.15+
309 cd pybind11
310 cmake -S . -B build
311 cmake --build build -j 2 # Build on 2 cores
312 cmake --install build
313
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100314Once detected, the aforementioned ``pybind11_add_module`` can be employed as
315before. The function usage and configuration variables are identical no matter
316if pybind11 is added as a subdirectory or found as an installed package. You
317can refer to the same [cmake_example]_ repository for a full sample project
318-- just swap out ``add_subdirectory`` for ``find_package``.
Lori A. Burns5cafc992016-12-13 10:55:38 -0500319
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100320.. _Config file: https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in
321
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100322
Henry Schreiner1729aae2020-08-19 12:26:26 -0400323.. _find-python-mode:
324
325FindPython mode
326---------------
327
328CMake 3.12+ (3.15+ recommended) added a new module called FindPython that had a
329highly improved search algorithm and modern targets and tools. If you use
330FindPython, pybind11 will detect this and use the existing targets instead:
Lori A. Burns5cafc992016-12-13 10:55:38 -0500331
332.. code-block:: cmake
333
Henry Schreiner1729aae2020-08-19 12:26:26 -0400334 cmake_minumum_required(VERSION 3.15...3.18)
335 project(example LANGUAGES CXX)
336
337 find_package(Python COMPONENTS Interpreter Development REQUIRED)
338 find_package(pybind11 CONFIG REQUIRED)
339 # or add_subdirectory(pybind11)
340
341 pybind11_add_module(example example.cpp)
342
343You can also use the targets (as listed below) with FindPython. If you define
344``PYBIND11_FINDPYTHON``, pybind11 will perform the FindPython step for you
345(mostly useful when building pybind11's own tests, or as a way to change search
346algorithms from the CMake invocation, with ``-DPYBIND11_FINDPYTHON=ON``.
347
348.. warning::
349
350 If you use FindPython2 and FindPython3 to dual-target Python, use the
351 individual targets listed below, and avoid targets that directly include
352 Python parts.
353
354There are `many ways to hint or force a discovery of a specific Python
355installation <https://cmake.org/cmake/help/latest/module/FindPython.html>`_),
356setting ``Python_ROOT_DIR`` may be the most common one (though with
357virtualenv/venv support, and Conda support, this tends to find the correct
358Python version more often than the old system did).
359
360.. versionadded:: 2.6
361
362Advanced: interface library targets
363-----------------------------------
364
365Pybind11 supports modern CMake usage patterns with a set of interface targets,
366available in all modes. The targets provided are:
367
368 ``pybind11::headers``
369 Just the pybind11 headers and minimum compile requirements
370
371 ``pybind11::python2_no_register``
372 Quiets the warning/error when mixing C++14 or higher and Python 2
373
374 ``pybind11::pybind11``
375 Python headers + ``pybind11::headers`` + ``pybind11::python2_no_register`` (Python 2 only)
376
377 ``pybind11::python_link_helper``
378 Just the "linking" part of pybind11:module
379
380 ``pybind11::module``
381 Everything for extension modules - ``pybind11::pybind11`` + ``Python::Module`` (FindPython CMake 3.15+) or ``pybind11::python_link_helper``
382
383 ``pybind11::embed``
384 Everything for embedding the Python interpreter - ``pybind11::pybind11`` + ``Python::Embed`` (FindPython) or Python libs
385
386 ``pybind11::lto`` / ``pybind11::thin_lto``
387 An alternative to `INTERPROCEDURAL_OPTIMIZATION` for adding link-time optimization.
388
389 ``pybind11::windows_extras``
390 ``/bigobj`` and ``/mp`` for MSVC.
391
Wenzel Jakob36c666f2020-09-04 23:31:05 +0200392 ``pybind11::opt_size``
393 ``/Os`` for MSVC, ``-Os`` for other compilers. Does nothing for debug builds.
394
Henry Schreiner1729aae2020-08-19 12:26:26 -0400395Two helper functions are also provided:
396
397 ``pybind11_strip(target)``
398 Strips a target (uses ``CMAKE_STRIP`` after the target is built)
399
400 ``pybind11_extension(target)``
401 Sets the correct extension (with SOABI) for a target.
402
403You can use these targets to build complex applications. For example, the
404``add_python_module`` function is identical to:
405
406.. code-block:: cmake
407
408 cmake_minimum_required(VERSION 3.4)
409 project(example LANGUAGES CXX)
Lori A. Burns5cafc992016-12-13 10:55:38 -0500410
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100411 find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)
Lori A. Burns5cafc992016-12-13 10:55:38 -0500412
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100413 add_library(example MODULE main.cpp)
Henry Schreiner1729aae2020-08-19 12:26:26 -0400414
415 target_link_libraries(example PRIVATE pybind11::module pybind11::lto pybind11::windows_extras)
416
417 pybind11_extension(example)
418 pybind11_strip(example)
419
420 set_target_properties(example PROPERTIES CXX_VISIBILITY_PRESET "hidden"
421 CUDA_VISIBILITY_PRESET "hidden")
422
423Instead of setting properties, you can set ``CMAKE_*`` variables to initialize these correctly.
Lori A. Burns5cafc992016-12-13 10:55:38 -0500424
425.. warning::
426
427 Since pybind11 is a metatemplate library, it is crucial that certain
428 compiler flags are provided to ensure high quality code generation. In
429 contrast to the ``pybind11_add_module()`` command, the CMake interface
Henry Schreiner1729aae2020-08-19 12:26:26 -0400430 provides a *composable* set of targets to ensure that you retain flexibility.
431 It can be expecially important to provide or set these properties; the
432 :ref:`FAQ <faq:symhidden>` contains an explanation on why these are needed.
Lori A. Burns5cafc992016-12-13 10:55:38 -0500433
Henry Schreiner1729aae2020-08-19 12:26:26 -0400434.. versionadded:: 2.6
Wenzel Jakobf3de2d52016-12-26 13:34:28 +0100435
Henry Schreiner1729aae2020-08-19 12:26:26 -0400436.. _nopython-mode:
Henry Schreiner6ec17752020-07-28 00:43:12 -0400437
Henry Schreiner1729aae2020-08-19 12:26:26 -0400438Advanced: NOPYTHON mode
439-----------------------
Henry Schreiner6ec17752020-07-28 00:43:12 -0400440
Henry Schreiner1729aae2020-08-19 12:26:26 -0400441If you want complete control, you can set ``PYBIND11_NOPYTHON`` to completely
442disable Python integration (this also happens if you run ``FindPython2`` and
443``FindPython3`` without running ``FindPython``). This gives you complete
444freedom to integrate into an existing system (like `Scikit-Build's
445<https://scikit-build.readthedocs.io>`_ ``PythonExtensions``).
446``pybind11_add_module`` and ``pybind11_extension`` will be unavailable, and the
447targets will be missing any Python specific behavior.
Henry Schreiner6ec17752020-07-28 00:43:12 -0400448
Henry Schreiner1729aae2020-08-19 12:26:26 -0400449.. versionadded:: 2.6
Henry Schreiner6ec17752020-07-28 00:43:12 -0400450
Dean Moldovan6d2411f2017-04-22 23:24:13 +0200451Embedding the Python interpreter
452--------------------------------
453
454In addition to extension modules, pybind11 also supports embedding Python into
455a C++ executable or library. In CMake, simply link with the ``pybind11::embed``
456target. It provides everything needed to get the interpreter running. The Python
457headers and libraries are attached to the target. Unlike ``pybind11::module``,
458there is no need to manually set any additional properties here. For more
459information about usage in C++, see :doc:`/advanced/embedding`.
460
461.. code-block:: cmake
462
Henry Schreiner1729aae2020-08-19 12:26:26 -0400463 cmake_minimum_required(VERSION 3.4...3.18)
464 project(example LANGUAGES CXX)
Dean Moldovan6d2411f2017-04-22 23:24:13 +0200465
466 find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)
467
Dean Moldovan8f6c1292017-05-31 13:48:39 +0200468 add_executable(example main.cpp)
Dean Moldovan6d2411f2017-04-22 23:24:13 +0200469 target_link_libraries(example PRIVATE pybind11::embed)
470
Ivan Smirnov5cbfda52017-08-30 20:58:43 +0100471.. _building_manually:
472
473Building manually
474=================
475
476pybind11 is a header-only library, hence it is not necessary to link against
477any special libraries and there are no intermediate (magic) translation steps.
478
479On Linux, you can compile an example such as the one given in
480:ref:`simple_example` using the following command:
481
482.. code-block:: bash
483
484 $ c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`
485
486The flags given here assume that you're using Python 3. For Python 2, just
487change the executable appropriately (to ``python`` or ``python2``).
488
489The ``python3 -m pybind11 --includes`` command fetches the include paths for
490both pybind11 and Python headers. This assumes that pybind11 has been installed
491using ``pip`` or ``conda``. If it hasn't, you can also manually specify
492``-I <path-to-pybind11>/include`` together with the Python includes path
493``python3-config --includes``.
494
495Note that Python 2.7 modules don't use a special suffix, so you should simply
496use ``example.so`` instead of ``example`python3-config --extension-suffix```.
497Besides, the ``--extension-suffix`` option may or may not be available, depending
498on the distribution; in the latter case, the module extension can be manually
499set to ``.so``.
500
Henry Schreinerfd61f502020-09-16 17:13:41 -0400501On macOS: the build command is almost the same but it also requires passing
Ivan Smirnov5cbfda52017-08-30 20:58:43 +0100502the ``-undefined dynamic_lookup`` flag so as to ignore missing symbols when
503building the module:
504
505.. code-block:: bash
506
507 $ c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`
508
509In general, it is advisable to include several additional build parameters
510that can considerably reduce the size of the created binary. Refer to section
511:ref:`cmake` for a detailed example of a suitable cross-platform CMake-based
512build system that works on all platforms including Windows.
513
514.. note::
515
516 On Linux and macOS, it's better to (intentionally) not link against
517 ``libpython``. The symbols will be resolved when the extension library
518 is loaded into a Python binary. This is preferable because you might
519 have several different installations of a given Python version (e.g. the
520 system-provided Python, and one that ships with a piece of commercial
521 software). In this way, the plugin will work with both versions, instead
522 of possibly importing a second Python library into a process that already
523 contains one (which will lead to a segfault).
Dean Moldovan6d2411f2017-04-22 23:24:13 +0200524
Griffin Downsa4cee362020-09-16 05:07:06 -0700525
Henry Schreinerdec33c22020-09-16 20:00:19 -0400526Building with Bazel
Griffin Downsa4cee362020-09-16 05:07:06 -0700527===================
Griffin Downsa4cee362020-09-16 05:07:06 -0700528
Henry Schreinerdec33c22020-09-16 20:00:19 -0400529You can build with the Bazel build system using the `pybind11_bazel
530<https://github.com/pybind/pybind11_bazel>`_ repository.
Griffin Downsa4cee362020-09-16 05:07:06 -0700531
Wenzel Jakobf3de2d52016-12-26 13:34:28 +0100532Generating binding code automatically
533=====================================
534
535The ``Binder`` project is a tool for automatic generation of pybind11 binding
536code by introspecting existing C++ codebases using LLVM/Clang. See the
537[binder]_ documentation for details.
538
539.. [binder] http://cppbinder.readthedocs.io/en/latest/about.html
Dustin Spicuzza2c4cd842019-11-24 02:36:48 -0500540
541[AutoWIG]_ is a Python library that wraps automatically compiled libraries into
542high-level languages. It parses C++ code using LLVM/Clang technologies and
543generates the wrappers using the Mako templating engine. The approach is automatic,
544extensible, and applies to very complex C++ libraries, composed of thousands of
545classes or incorporating modern meta-programming constructs.
546
547.. [AutoWIG] https://github.com/StatisKit/AutoWIG
Dustin Spicuzza6f3470f2020-08-10 16:10:45 -0400548
549[robotpy-build]_ is a is a pure python, cross platform build tool that aims to
550simplify creation of python wheels for pybind11 projects, and provide
551cross-project dependency management. Additionally, it is able to autogenerate
552customizable pybind11-based wrappers by parsing C++ header files.
553
554.. [robotpy-build] https://robotpy-build.readthedocs.io