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.

648 lines
26 KiB

  1. .. _compiling:
  2. Build systems
  3. #############
  4. .. _build-setuptools:
  5. Building with setuptools
  6. ========================
  7. For projects on PyPI, building with setuptools is the way to go. Sylvain Corlay
  8. has kindly provided an example project which shows how to set up everything,
  9. including automatic generation of documentation using Sphinx. Please refer to
  10. the [python_example]_ repository.
  11. .. [python_example] https://github.com/pybind/python_example
  12. A helper file is provided with pybind11 that can simplify usage with setuptools.
  13. To use pybind11 inside your ``setup.py``, you have to have some system to
  14. ensure that ``pybind11`` is installed when you build your package. There are
  15. four possible ways to do this, and pybind11 supports all four: You can ask all
  16. users to install pybind11 beforehand (bad), you can use
  17. :ref:`setup_helpers-pep518` (good, but very new and requires Pip 10),
  18. :ref:`setup_helpers-setup_requires` (discouraged by Python packagers now that
  19. PEP 518 is available, but it still works everywhere), or you can
  20. :ref:`setup_helpers-copy-manually` (always works but you have to manually sync
  21. your copy to get updates).
  22. An example of a ``setup.py`` using pybind11's helpers:
  23. .. code-block:: python
  24. from glob import glob
  25. from setuptools import setup
  26. from pybind11.setup_helpers import Pybind11Extension
  27. ext_modules = [
  28. Pybind11Extension(
  29. "python_example",
  30. sorted(glob("src/*.cpp")), # Sort source files for reproducibility
  31. ),
  32. ]
  33. setup(..., ext_modules=ext_modules)
  34. If you want to do an automatic search for the highest supported C++ standard,
  35. that is supported via a ``build_ext`` command override; it will only affect
  36. ``Pybind11Extensions``:
  37. .. code-block:: python
  38. from glob import glob
  39. from setuptools import setup
  40. from pybind11.setup_helpers import Pybind11Extension, build_ext
  41. ext_modules = [
  42. Pybind11Extension(
  43. "python_example",
  44. sorted(glob("src/*.cpp")),
  45. ),
  46. ]
  47. setup(..., cmdclass={"build_ext": build_ext}, ext_modules=ext_modules)
  48. If you have single-file extension modules that are directly stored in the
  49. Python source tree (``foo.cpp`` in the same directory as where a ``foo.py``
  50. would be located), you can also generate ``Pybind11Extensions`` using
  51. ``setup_helpers.intree_extensions``: ``intree_extensions(["path/to/foo.cpp",
  52. ...])`` returns a list of ``Pybind11Extensions`` which can be passed to
  53. ``ext_modules``, possibly after further customizing their attributes
  54. (``libraries``, ``include_dirs``, etc.). By doing so, a ``foo.*.so`` extension
  55. module will be generated and made available upon installation.
  56. ``intree_extension`` will automatically detect if you are using a ``src``-style
  57. layout (as long as no namespace packages are involved), but you can also
  58. explicitly pass ``package_dir`` to it (as in ``setuptools.setup``).
  59. Since pybind11 does not require NumPy when building, a light-weight replacement
  60. for NumPy's parallel compilation distutils tool is included. Use it like this:
  61. .. code-block:: python
  62. from pybind11.setup_helpers import ParallelCompile
  63. # Optional multithreaded build
  64. ParallelCompile("NPY_NUM_BUILD_JOBS").install()
  65. setup(...)
  66. The argument is the name of an environment variable to control the number of
  67. threads, such as ``NPY_NUM_BUILD_JOBS`` (as used by NumPy), though you can set
  68. something different if you want; ``CMAKE_BUILD_PARALLEL_LEVEL`` is another choice
  69. a user might expect. You can also pass ``default=N`` to set the default number
  70. of threads (0 will take the number of threads available) and ``max=N``, the
  71. maximum number of threads; if you have a large extension you may want set this
  72. to a memory dependent number.
  73. If you are developing rapidly and have a lot of C++ files, you may want to
  74. avoid rebuilding files that have not changed. For simple cases were you are
  75. using ``pip install -e .`` and do not have local headers, you can skip the
  76. rebuild if an object file is newer than its source (headers are not checked!)
  77. with the following:
  78. .. code-block:: python
  79. from pybind11.setup_helpers import ParallelCompile, naive_recompile
  80. ParallelCompile("NPY_NUM_BUILD_JOBS", needs_recompile=naive_recompile).install()
  81. If you have a more complex build, you can implement a smarter function and pass
  82. it to ``needs_recompile``, or you can use [Ccache]_ instead. ``CXX="cache g++"
  83. pip install -e .`` would be the way to use it with GCC, for example. Unlike the
  84. simple solution, this even works even when not compiling in editable mode, but
  85. it does require Ccache to be installed.
  86. Keep in mind that Pip will not even attempt to rebuild if it thinks it has
  87. already built a copy of your code, which it deduces from the version number.
  88. One way to avoid this is to use [setuptools_scm]_, which will generate a
  89. version number that includes the number of commits since your last tag and a
  90. hash for a dirty directory. Another way to force a rebuild is purge your cache
  91. or use Pip's ``--no-cache-dir`` option.
  92. .. [Ccache] https://ccache.dev
  93. .. [setuptools_scm] https://github.com/pypa/setuptools_scm
  94. .. _setup_helpers-pep518:
  95. PEP 518 requirements (Pip 10+ required)
  96. ---------------------------------------
  97. If you use `PEP 518's <https://www.python.org/dev/peps/pep-0518/>`_
  98. ``pyproject.toml`` file, you can ensure that ``pybind11`` is available during
  99. the compilation of your project. When this file exists, Pip will make a new
  100. virtual environment, download just the packages listed here in ``requires=``,
  101. and build a wheel (binary Python package). It will then throw away the
  102. environment, and install your wheel.
  103. Your ``pyproject.toml`` file will likely look something like this:
  104. .. code-block:: toml
  105. [build-system]
  106. requires = ["setuptools>=42", "wheel", "pybind11~=2.6.1"]
  107. build-backend = "setuptools.build_meta"
  108. .. note::
  109. The main drawback to this method is that a `PEP 517`_ compliant build tool,
  110. such as Pip 10+, is required for this approach to work; older versions of
  111. Pip completely ignore this file. If you distribute binaries (called wheels
  112. in Python) using something like `cibuildwheel`_, remember that ``setup.py``
  113. and ``pyproject.toml`` are not even contained in the wheel, so this high
  114. Pip requirement is only for source builds, and will not affect users of
  115. your binary wheels. If you are building SDists and wheels, then
  116. `pypa-build`_ is the recommended official tool.
  117. .. _PEP 517: https://www.python.org/dev/peps/pep-0517/
  118. .. _cibuildwheel: https://cibuildwheel.readthedocs.io
  119. .. _pypa-build: https://pypa-build.readthedocs.io/en/latest/
  120. .. _setup_helpers-setup_requires:
  121. Classic ``setup_requires``
  122. --------------------------
  123. If you want to support old versions of Pip with the classic
  124. ``setup_requires=["pybind11"]`` keyword argument to setup, which triggers a
  125. two-phase ``setup.py`` run, then you will need to use something like this to
  126. ensure the first pass works (which has not yet installed the ``setup_requires``
  127. packages, since it can't install something it does not know about):
  128. .. code-block:: python
  129. try:
  130. from pybind11.setup_helpers import Pybind11Extension
  131. except ImportError:
  132. from setuptools import Extension as Pybind11Extension
  133. It doesn't matter that the Extension class is not the enhanced subclass for the
  134. first pass run; and the second pass will have the ``setup_requires``
  135. requirements.
  136. This is obviously more of a hack than the PEP 518 method, but it supports
  137. ancient versions of Pip.
  138. .. _setup_helpers-copy-manually:
  139. Copy manually
  140. -------------
  141. You can also copy ``setup_helpers.py`` directly to your project; it was
  142. designed to be usable standalone, like the old example ``setup.py``. You can
  143. set ``include_pybind11=False`` to skip including the pybind11 package headers,
  144. so you can use it with git submodules and a specific git version. If you use
  145. this, you will need to import from a local file in ``setup.py`` and ensure the
  146. helper file is part of your MANIFEST.
  147. Closely related, if you include pybind11 as a subproject, you can run the
  148. ``setup_helpers.py`` inplace. If loaded correctly, this should even pick up
  149. the correct include for pybind11, though you can turn it off as shown above if
  150. you want to input it manually.
  151. Suggested usage if you have pybind11 as a submodule in ``extern/pybind11``:
  152. .. code-block:: python
  153. DIR = os.path.abspath(os.path.dirname(__file__))
  154. sys.path.append(os.path.join(DIR, "extern", "pybind11"))
  155. from pybind11.setup_helpers import Pybind11Extension # noqa: E402
  156. del sys.path[-1]
  157. .. versionchanged:: 2.6
  158. Added ``setup_helpers`` file.
  159. Building with cppimport
  160. ========================
  161. [cppimport]_ is a small Python import hook that determines whether there is a C++
  162. source file whose name matches the requested module. If there is, the file is
  163. compiled as a Python extension using pybind11 and placed in the same folder as
  164. the C++ source file. Python is then able to find the module and load it.
  165. .. [cppimport] https://github.com/tbenthompson/cppimport
  166. .. _cmake:
  167. Building with CMake
  168. ===================
  169. For C++ codebases that have an existing CMake-based build system, a Python
  170. extension module can be created with just a few lines of code:
  171. .. code-block:: cmake
  172. cmake_minimum_required(VERSION 3.4...3.18)
  173. project(example LANGUAGES CXX)
  174. add_subdirectory(pybind11)
  175. pybind11_add_module(example example.cpp)
  176. This assumes that the pybind11 repository is located in a subdirectory named
  177. :file:`pybind11` and that the code is located in a file named :file:`example.cpp`.
  178. The CMake command ``add_subdirectory`` will import the pybind11 project which
  179. provides the ``pybind11_add_module`` function. It will take care of all the
  180. details needed to build a Python extension module on any platform.
  181. A working sample project, including a way to invoke CMake from :file:`setup.py` for
  182. PyPI integration, can be found in the [cmake_example]_ repository.
  183. .. [cmake_example] https://github.com/pybind/cmake_example
  184. .. versionchanged:: 2.6
  185. CMake 3.4+ is required.
  186. Further information can be found at :doc:`cmake/index`.
  187. pybind11_add_module
  188. -------------------
  189. To ease the creation of Python extension modules, pybind11 provides a CMake
  190. function with the following signature:
  191. .. code-block:: cmake
  192. pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL]
  193. [NO_EXTRAS] [THIN_LTO] [OPT_SIZE] source1 [source2 ...])
  194. This function behaves very much like CMake's builtin ``add_library`` (in fact,
  195. it's a wrapper function around that command). It will add a library target
  196. called ``<name>`` to be built from the listed source files. In addition, it
  197. will take care of all the Python-specific compiler and linker flags as well
  198. as the OS- and Python-version-specific file extension. The produced target
  199. ``<name>`` can be further manipulated with regular CMake commands.
  200. ``MODULE`` or ``SHARED`` may be given to specify the type of library. If no
  201. type is given, ``MODULE`` is used by default which ensures the creation of a
  202. Python-exclusive module. Specifying ``SHARED`` will create a more traditional
  203. dynamic library which can also be linked from elsewhere. ``EXCLUDE_FROM_ALL``
  204. removes this target from the default build (see CMake docs for details).
  205. Since pybind11 is a template library, ``pybind11_add_module`` adds compiler
  206. flags to ensure high quality code generation without bloat arising from long
  207. symbol names and duplication of code in different translation units. It
  208. sets default visibility to *hidden*, which is required for some pybind11
  209. features and functionality when attempting to load multiple pybind11 modules
  210. compiled under different pybind11 versions. It also adds additional flags
  211. enabling LTO (Link Time Optimization) and strip unneeded symbols. See the
  212. :ref:`FAQ entry <faq:symhidden>` for a more detailed explanation. These
  213. latter optimizations are never applied in ``Debug`` mode. If ``NO_EXTRAS`` is
  214. given, they will always be disabled, even in ``Release`` mode. However, this
  215. will result in code bloat and is generally not recommended.
  216. As stated above, LTO is enabled by default. Some newer compilers also support
  217. different flavors of LTO such as `ThinLTO`_. Setting ``THIN_LTO`` will cause
  218. the function to prefer this flavor if available. The function falls back to
  219. regular LTO if ``-flto=thin`` is not available. If
  220. ``CMAKE_INTERPROCEDURAL_OPTIMIZATION`` is set (either ``ON`` or ``OFF``), then
  221. that will be respected instead of the built-in flag search.
  222. .. note::
  223. If you want to set the property form on targets or the
  224. ``CMAKE_INTERPROCEDURAL_OPTIMIZATION_<CONFIG>`` versions of this, you should
  225. still use ``set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF)`` (otherwise a
  226. no-op) to disable pybind11's ipo flags.
  227. The ``OPT_SIZE`` flag enables size-based optimization equivalent to the
  228. standard ``/Os`` or ``-Os`` compiler flags and the ``MinSizeRel`` build type,
  229. which avoid optimizations that that can substantially increase the size of the
  230. resulting binary. This flag is particularly useful in projects that are split
  231. into performance-critical parts and associated bindings. In this case, we can
  232. compile the project in release mode (and hence, optimize performance globally),
  233. and specify ``OPT_SIZE`` for the binding target, where size might be the main
  234. concern as performance is often less critical here. A ~25% size reduction has
  235. been observed in practice. This flag only changes the optimization behavior at
  236. a per-target level and takes precedence over the global CMake build type
  237. (``Release``, ``RelWithDebInfo``) except for ``Debug`` builds, where
  238. optimizations remain disabled.
  239. .. _ThinLTO: http://clang.llvm.org/docs/ThinLTO.html
  240. Configuration variables
  241. -----------------------
  242. By default, pybind11 will compile modules with the compiler default or the
  243. minimum standard required by pybind11, whichever is higher. You can set the
  244. standard explicitly with
  245. `CMAKE_CXX_STANDARD <https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html>`_:
  246. .. code-block:: cmake
  247. set(CMAKE_CXX_STANDARD 14 CACHE STRING "C++ version selection") # or 11, 14, 17, 20
  248. set(CMAKE_CXX_STANDARD_REQUIRED ON) # optional, ensure standard is supported
  249. set(CMAKE_CXX_EXTENSIONS OFF) # optional, keep compiler extensions off
  250. The variables can also be set when calling CMake from the command line using
  251. the ``-D<variable>=<value>`` flag. You can also manually set ``CXX_STANDARD``
  252. on a target or use ``target_compile_features`` on your targets - anything that
  253. CMake supports.
  254. Classic Python support: The target Python version can be selected by setting
  255. ``PYBIND11_PYTHON_VERSION`` or an exact Python installation can be specified
  256. with ``PYTHON_EXECUTABLE``. For example:
  257. .. code-block:: bash
  258. cmake -DPYBIND11_PYTHON_VERSION=3.6 ..
  259. # Another method:
  260. cmake -DPYTHON_EXECUTABLE=/path/to/python ..
  261. # This often is a good way to get the current Python, works in environments:
  262. cmake -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") ..
  263. find_package vs. add_subdirectory
  264. ---------------------------------
  265. For CMake-based projects that don't include the pybind11 repository internally,
  266. an external installation can be detected through ``find_package(pybind11)``.
  267. See the `Config file`_ docstring for details of relevant CMake variables.
  268. .. code-block:: cmake
  269. cmake_minimum_required(VERSION 3.4...3.18)
  270. project(example LANGUAGES CXX)
  271. find_package(pybind11 REQUIRED)
  272. pybind11_add_module(example example.cpp)
  273. Note that ``find_package(pybind11)`` will only work correctly if pybind11
  274. has been correctly installed on the system, e. g. after downloading or cloning
  275. the pybind11 repository :
  276. .. code-block:: bash
  277. # Classic CMake
  278. cd pybind11
  279. mkdir build
  280. cd build
  281. cmake ..
  282. make install
  283. # CMake 3.15+
  284. cd pybind11
  285. cmake -S . -B build
  286. cmake --build build -j 2 # Build on 2 cores
  287. cmake --install build
  288. Once detected, the aforementioned ``pybind11_add_module`` can be employed as
  289. before. The function usage and configuration variables are identical no matter
  290. if pybind11 is added as a subdirectory or found as an installed package. You
  291. can refer to the same [cmake_example]_ repository for a full sample project
  292. -- just swap out ``add_subdirectory`` for ``find_package``.
  293. .. _Config file: https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in
  294. .. _find-python-mode:
  295. FindPython mode
  296. ---------------
  297. CMake 3.12+ (3.15+ recommended, 3.18.2+ ideal) added a new module called
  298. FindPython that had a highly improved search algorithm and modern targets
  299. and tools. If you use FindPython, pybind11 will detect this and use the
  300. existing targets instead:
  301. .. code-block:: cmake
  302. cmake_minimum_required(VERSION 3.15...3.19)
  303. project(example LANGUAGES CXX)
  304. find_package(Python COMPONENTS Interpreter Development REQUIRED)
  305. find_package(pybind11 CONFIG REQUIRED)
  306. # or add_subdirectory(pybind11)
  307. pybind11_add_module(example example.cpp)
  308. You can also use the targets (as listed below) with FindPython. If you define
  309. ``PYBIND11_FINDPYTHON``, pybind11 will perform the FindPython step for you
  310. (mostly useful when building pybind11's own tests, or as a way to change search
  311. algorithms from the CMake invocation, with ``-DPYBIND11_FINDPYTHON=ON``.
  312. .. warning::
  313. If you use FindPython2 and FindPython3 to dual-target Python, use the
  314. individual targets listed below, and avoid targets that directly include
  315. Python parts.
  316. There are `many ways to hint or force a discovery of a specific Python
  317. installation <https://cmake.org/cmake/help/latest/module/FindPython.html>`_),
  318. setting ``Python_ROOT_DIR`` may be the most common one (though with
  319. virtualenv/venv support, and Conda support, this tends to find the correct
  320. Python version more often than the old system did).
  321. .. warning::
  322. When the Python libraries (i.e. ``libpythonXX.a`` and ``libpythonXX.so``
  323. on Unix) are not available, as is the case on a manylinux image, the
  324. ``Development`` component will not be resolved by ``FindPython``. When not
  325. using the embedding functionality, CMake 3.18+ allows you to specify
  326. ``Development.Module`` instead of ``Development`` to resolve this issue.
  327. .. versionadded:: 2.6
  328. Advanced: interface library targets
  329. -----------------------------------
  330. Pybind11 supports modern CMake usage patterns with a set of interface targets,
  331. available in all modes. The targets provided are:
  332. ``pybind11::headers``
  333. Just the pybind11 headers and minimum compile requirements
  334. ``pybind11::python2_no_register``
  335. Quiets the warning/error when mixing C++14 or higher and Python 2
  336. ``pybind11::pybind11``
  337. Python headers + ``pybind11::headers`` + ``pybind11::python2_no_register`` (Python 2 only)
  338. ``pybind11::python_link_helper``
  339. Just the "linking" part of pybind11:module
  340. ``pybind11::module``
  341. Everything for extension modules - ``pybind11::pybind11`` + ``Python::Module`` (FindPython CMake 3.15+) or ``pybind11::python_link_helper``
  342. ``pybind11::embed``
  343. Everything for embedding the Python interpreter - ``pybind11::pybind11`` + ``Python::Python`` (FindPython) or Python libs
  344. ``pybind11::lto`` / ``pybind11::thin_lto``
  345. An alternative to `INTERPROCEDURAL_OPTIMIZATION` for adding link-time optimization.
  346. ``pybind11::windows_extras``
  347. ``/bigobj`` and ``/mp`` for MSVC.
  348. ``pybind11::opt_size``
  349. ``/Os`` for MSVC, ``-Os`` for other compilers. Does nothing for debug builds.
  350. Two helper functions are also provided:
  351. ``pybind11_strip(target)``
  352. Strips a target (uses ``CMAKE_STRIP`` after the target is built)
  353. ``pybind11_extension(target)``
  354. Sets the correct extension (with SOABI) for a target.
  355. You can use these targets to build complex applications. For example, the
  356. ``add_python_module`` function is identical to:
  357. .. code-block:: cmake
  358. cmake_minimum_required(VERSION 3.4)
  359. project(example LANGUAGES CXX)
  360. find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)
  361. add_library(example MODULE main.cpp)
  362. target_link_libraries(example PRIVATE pybind11::module pybind11::lto pybind11::windows_extras)
  363. pybind11_extension(example)
  364. pybind11_strip(example)
  365. set_target_properties(example PROPERTIES CXX_VISIBILITY_PRESET "hidden"
  366. CUDA_VISIBILITY_PRESET "hidden")
  367. Instead of setting properties, you can set ``CMAKE_*`` variables to initialize these correctly.
  368. .. warning::
  369. Since pybind11 is a metatemplate library, it is crucial that certain
  370. compiler flags are provided to ensure high quality code generation. In
  371. contrast to the ``pybind11_add_module()`` command, the CMake interface
  372. provides a *composable* set of targets to ensure that you retain flexibility.
  373. It can be especially important to provide or set these properties; the
  374. :ref:`FAQ <faq:symhidden>` contains an explanation on why these are needed.
  375. .. versionadded:: 2.6
  376. .. _nopython-mode:
  377. Advanced: NOPYTHON mode
  378. -----------------------
  379. If you want complete control, you can set ``PYBIND11_NOPYTHON`` to completely
  380. disable Python integration (this also happens if you run ``FindPython2`` and
  381. ``FindPython3`` without running ``FindPython``). This gives you complete
  382. freedom to integrate into an existing system (like `Scikit-Build's
  383. <https://scikit-build.readthedocs.io>`_ ``PythonExtensions``).
  384. ``pybind11_add_module`` and ``pybind11_extension`` will be unavailable, and the
  385. targets will be missing any Python specific behavior.
  386. .. versionadded:: 2.6
  387. Embedding the Python interpreter
  388. --------------------------------
  389. In addition to extension modules, pybind11 also supports embedding Python into
  390. a C++ executable or library. In CMake, simply link with the ``pybind11::embed``
  391. target. It provides everything needed to get the interpreter running. The Python
  392. headers and libraries are attached to the target. Unlike ``pybind11::module``,
  393. there is no need to manually set any additional properties here. For more
  394. information about usage in C++, see :doc:`/advanced/embedding`.
  395. .. code-block:: cmake
  396. cmake_minimum_required(VERSION 3.4...3.18)
  397. project(example LANGUAGES CXX)
  398. find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)
  399. add_executable(example main.cpp)
  400. target_link_libraries(example PRIVATE pybind11::embed)
  401. .. _building_manually:
  402. Building manually
  403. =================
  404. pybind11 is a header-only library, hence it is not necessary to link against
  405. any special libraries and there are no intermediate (magic) translation steps.
  406. On Linux, you can compile an example such as the one given in
  407. :ref:`simple_example` using the following command:
  408. .. code-block:: bash
  409. $ c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix)
  410. The flags given here assume that you're using Python 3. For Python 2, just
  411. change the executable appropriately (to ``python`` or ``python2``).
  412. The ``python3 -m pybind11 --includes`` command fetches the include paths for
  413. both pybind11 and Python headers. This assumes that pybind11 has been installed
  414. using ``pip`` or ``conda``. If it hasn't, you can also manually specify
  415. ``-I <path-to-pybind11>/include`` together with the Python includes path
  416. ``python3-config --includes``.
  417. Note that Python 2.7 modules don't use a special suffix, so you should simply
  418. use ``example.so`` instead of ``example$(python3-config --extension-suffix)``.
  419. Besides, the ``--extension-suffix`` option may or may not be available, depending
  420. on the distribution; in the latter case, the module extension can be manually
  421. set to ``.so``.
  422. On macOS: the build command is almost the same but it also requires passing
  423. the ``-undefined dynamic_lookup`` flag so as to ignore missing symbols when
  424. building the module:
  425. .. code-block:: bash
  426. $ c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix)
  427. In general, it is advisable to include several additional build parameters
  428. that can considerably reduce the size of the created binary. Refer to section
  429. :ref:`cmake` for a detailed example of a suitable cross-platform CMake-based
  430. build system that works on all platforms including Windows.
  431. .. note::
  432. On Linux and macOS, it's better to (intentionally) not link against
  433. ``libpython``. The symbols will be resolved when the extension library
  434. is loaded into a Python binary. This is preferable because you might
  435. have several different installations of a given Python version (e.g. the
  436. system-provided Python, and one that ships with a piece of commercial
  437. software). In this way, the plugin will work with both versions, instead
  438. of possibly importing a second Python library into a process that already
  439. contains one (which will lead to a segfault).
  440. Building with Bazel
  441. ===================
  442. You can build with the Bazel build system using the `pybind11_bazel
  443. <https://github.com/pybind/pybind11_bazel>`_ repository.
  444. Generating binding code automatically
  445. =====================================
  446. The ``Binder`` project is a tool for automatic generation of pybind11 binding
  447. code by introspecting existing C++ codebases using LLVM/Clang. See the
  448. [binder]_ documentation for details.
  449. .. [binder] http://cppbinder.readthedocs.io/en/latest/about.html
  450. [AutoWIG]_ is a Python library that wraps automatically compiled libraries into
  451. high-level languages. It parses C++ code using LLVM/Clang technologies and
  452. generates the wrappers using the Mako templating engine. The approach is automatic,
  453. extensible, and applies to very complex C++ libraries, composed of thousands of
  454. classes or incorporating modern meta-programming constructs.
  455. .. [AutoWIG] https://github.com/StatisKit/AutoWIG
  456. [robotpy-build]_ is a is a pure python, cross platform build tool that aims to
  457. simplify creation of python wheels for pybind11 projects, and provide
  458. cross-project dependency management. Additionally, it is able to autogenerate
  459. customizable pybind11-based wrappers by parsing C++ header files.
  460. .. [robotpy-build] https://robotpy-build.readthedocs.io