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.

53 lines
2.0 KiB

  1. Build systems
  2. #############
  3. Building with setuptools
  4. ========================
  5. For projects on PyPI, building with setuptools is the way to go. Sylvain Corlay
  6. has kindly provided an example project which shows how to set up everything,
  7. including automatic generation of documentation using Sphinx. Please refer to
  8. the [python_example]_ repository.
  9. .. [python_example] https://github.com/pybind/python_example
  10. Building with cppimport
  11. ========================
  12. cppimport is a small Python import hook that determines whether there is a C++
  13. source file whose name matches the requested module. If there is, the file is
  14. compiled as a Python extension using pybind11 and placed in the same folder as
  15. the C++ source file. Python is then able to find the module and load it.
  16. .. [cppimport] https://github.com/tbenthompson/cppimport
  17. .. _cmake:
  18. Building with CMake
  19. ===================
  20. For C++ codebases that have an existing CMake-based build system, a Python
  21. extension module can be created with just a few lines of code:
  22. .. code-block:: cmake
  23. cmake_minimum_required(VERSION 2.8.12)
  24. project(example)
  25. add_subdirectory(pybind11)
  26. pybind11_add_module(example example.cpp)
  27. This assumes that the pybind11 repository is located in a subdirectory named
  28. :file:`pybind11` and that the code is located in a file named :file:`example.cpp`.
  29. The CMake command ``add_subdirectory`` will import a function with the signature
  30. ``pybind11_add_module(<name> source1 [source2 ...])``. It will take care of all
  31. the details needed to build a Python extension module on any platform.
  32. The target Python version can be selected by setting the ``PYBIND11_PYTHON_VERSION``
  33. variable before adding the pybind11 subdirectory. Alternatively, an exact Python
  34. installation can be specified by setting ``PYTHON_EXECUTABLE``.
  35. A working sample project, including a way to invoke CMake from :file:`setup.py` for
  36. PyPI integration, can be found in the [cmake_example]_ repository.
  37. .. [cmake_example] https://github.com/pybind/cmake_example