6

The pybind11 documentation is generally good, but one area in which it is not is explaining the install process and the process of getting and running examples using cmake.

I've managed to figure out how to get and build examples. But it leads to more questions. Almost all the examples count on downloading the pybind11 repo into the examples folder and including the root folder of the repo in a cmake run (the root folder contains a cmakelists.txt file). The contents of that repo have a lot in common with the content which is added to a python environment when you install pybind11 using pip or conda. But the folder organization is completely different.

So I'm curious:

  • Why the difference?
  • Is there a way to use the content in the environment install in such a way that you don't also have to download the repo in order to build examples using cmake?
  • Failing that, what is the best way to put the pybind11 repo in a common place so it doesn't have to be copied all over the place in order to build examples, or in order to provide the important added cmake functionality for one's own code?

I'm really uncomfortable in general not understanding the "how this works" aspect of such things, so this will really help me.

1 Answer 1

3

Not sure which examples you mention, but to install pybind11 in your system and use it in different projects, just follow the standard procedure for installing CMake based packages

mkdir build
cd build
cmake ../  # optionally you can specify -DPYBIND11_PYTHON_VERSION=<your python version>
make
sudo make install

Then in one of your other project's CMakeLists.txt you can use it e.g. like this:

find_package(pybind11 CONFIG REQUIRED)
message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}")
add_library(<name of your lib> MODULE <your sources>)
target_link_libraries(mylib pybind11::module)

For more CMake commands consult pybind11Config.cmake.

Then if you don't want to install it in your system, you can just embed pybind11 repo in your project tree with add_subdirectory instead of find_package. All the offered features will be the same. The package is well designed and it detects whether it is used as master project or not and either defined INSTALL targets or not.

So I guess that your last 2 questions are answered?

2
  • That works beautifully. Thank you. A related question - do you know anywhere I could find a plain starter makefile (not cmake generated) that is reasonably well set up for pybind11? Cmake isn't my favorite tool, and I know how to write out of source makefiles. Apr 13, 2020 at 22:28
  • Mmm plain Makefile, no idea. Most of the open projects use CMake right now. The language is not perfect and there is a bit of hacking in every CMake version, but it is still the most (or one of) popular build tool.
    – pptaszni
    Apr 14, 2020 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.