compiling.rst 4.8 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
2
3
4
5
6
7
8
9
Build systems
#############

Building with setuptools
========================

For projects on PyPI, building with setuptools is the way to go. Sylvain Corlay
has kindly provided an example project which shows how to set up everything,
including automatic generation of documentation using Sphinx. Please refer to
Wenzel Jakob's avatar
Wenzel Jakob committed
10
the [python_example]_ repository.
Wenzel Jakob's avatar
Wenzel Jakob committed
11

Wenzel Jakob's avatar
Wenzel Jakob committed
12
.. [python_example] https://github.com/pybind/python_example
Wenzel Jakob's avatar
Wenzel Jakob committed
13

14
15
16
17
18
19
20
21
22
23
Building with cppimport
========================

 cppimport is a small Python import hook that determines whether there is a C++
 source file whose name matches the requested module. If there is, the file is
 compiled as a Python extension using pybind11 and placed in the same folder as
 the C++ source file. Python is then able to find the module and load it.

.. [cppimport] https://github.com/tbenthompson/cppimport

24
25
26
27
28
.. _cmake:

Building with CMake
===================

Wenzel Jakob's avatar
Wenzel Jakob committed
29
For C++ codebases that have an existing CMake-based build system, a Python
30
extension module can be created with just a few lines of code:
31
32
33

.. code-block:: cmake

34
    cmake_minimum_required(VERSION 2.8.12)
35
36
    project(example)

37
38
39
    add_subdirectory(pybind11)
    pybind11_add_module(example example.cpp)

Wenzel Jakob's avatar
Wenzel Jakob committed
40
This assumes that the pybind11 repository is located in a subdirectory named
41
42
43
44
45
:file:`pybind11` and that the code is located in a file named :file:`example.cpp`.
The CMake command ``add_subdirectory`` will import a function with the signature
``pybind11_add_module(<name> source1 [source2 ...])``. It will take care of all
the details needed to build a Python extension module on any platform.

Wenzel Jakob's avatar
Wenzel Jakob committed
46
47
The target Python version can be selected by setting the ``PYBIND11_PYTHON_VERSION``
variable before adding the pybind11 subdirectory. Alternatively, an exact Python
48
49
50
51
52
installation can be specified by setting ``PYTHON_EXECUTABLE``.

A working sample project, including a way to invoke CMake from :file:`setup.py` for
PyPI integration, can be found in the [cmake_example]_  repository.

Wenzel Jakob's avatar
Wenzel Jakob committed
53
.. [cmake_example] https://github.com/pybind/cmake_example
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

For CMake-based projects that don't include the pybind11
repository internally, an external installation can be detected
through `find_package(pybind11 ... CONFIG ...)`. See the `Config file
<https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in>`_
docstring for details of relevant CMake variables.

Once detected, and after setting any variables to guide Python and C++
standard detection, the aforementioned ``pybind11_add_module``
wrapper to ``add_library`` can
be employed as described above (after ``include(pybind11Tools)``). This
procedure is available when using CMake >= 2.8.12. A
working example can be found at [test_installed_module]_ .

.. code-block:: cmake

    cmake_minimum_required(VERSION 2.8.12)
    project(example)

    find_package(pybind11 REQUIRED)
    pybind11_add_module(example example.cpp)

.. [test_installed_module] https://github.com/pybind/pybind11/blob/master/tests/test_installed_module/CMakeLists.txt

When using a version of CMake greater than 3.0, pybind11 can
additionally be used as a special *interface library* following the call
to ``find_package``. CMake
variables to guide Python and C++ standard detection should be set
*before* ``find_package``. When ``find_package`` returns, the target
``pybind11::pybind11`` is available with pybind11 headers, Python headers
and libraries as needed, and C++ compile definitions attached. This
target is suitable for linking to an independently constructed (through
``add_library``, not ``pybind11_add_module``) target in the consuming
project. A working example can be found at [test_installed_target]_ .

.. code-block:: cmake

    cmake_minimum_required(VERSION 3.0)
    project(example)

    add_library(example MODULE main.cpp)

    find_package(pybind11 REQUIRED)
    target_link_libraries(example PRIVATE pybind11::pybind11)
    set_target_properties(example PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
                                             SUFFIX "${PYTHON_MODULE_EXTENSION}")

.. warning::

    Since pybind11 is a metatemplate library, it is crucial that certain
    compiler flags are provided to ensure high quality code generation. In
    contrast to the ``pybind11_add_module()`` command, the CMake interface
    library only provides the *minimal* set of parameters to ensure that the
    code using pybind11 compiles, but it does **not** pass these extra compiler
    flags (i.e. this is up to you).

    These include Link Time Optimization (``-flto`` on GCC/Clang/ICPC, ``/GL``
    and ``/LTCG`` on Visual Studio). Default-hidden symbols on GCC/Clang/ICPC
    (``-fvisibility=hidden``) and .OBJ files with many sections on Visual Studio
    (``/bigobj``). The :ref:`FAQ <faq:symhidden>` contains an
    explanation on why these are needed.

.. [test_installed_target] https://github.com/pybind/pybind11/blob/master/tests/test_installed_target/CMakeLists.txt