1

I'm working on a project that installs the dependencies using brew.

We need pybind11 to build our python module.

When I ll the available files I get:

@DOCKER:include ^_^$ ll pybind11/
total 548K
-rw-rw-r-- 1 builder docker  24K Nov  8 10:32 attr.h
-rw-rw-r-- 1 builder docker 7.0K Nov  8 10:32 buffer_info.h
-rw-rw-r-- 1 builder docker  64K Nov  8 10:32 cast.h
-rw-rw-r-- 1 builder docker 8.7K Nov  8 10:32 chrono.h
-rw-rw-r-- 1 builder docker  120 Nov  8 10:32 common.h
-rw-rw-r-- 1 builder docker 2.1K Nov  8 10:32 complex.h
drwxrwxr-x 2 builder docker 4.0K Nov  8 10:32 detail
-rw-rw-r-- 1 builder docker  31K Nov  8 10:32 eigen.h
-rw-rw-r-- 1 builder docker  12K Nov  8 10:32 embed.h
-rw-rw-r-- 1 builder docker 5.5K Nov  8 10:32 eval.h
-rw-rw-r-- 1 builder docker 4.7K Nov  8 10:32 functional.h
-rw-rw-r-- 1 builder docker 6.7K Nov  8 10:32 gil.h
-rw-rw-r-- 1 builder docker 8.7K Nov  8 10:32 iostream.h
-rw-rw-r-- 1 builder docker  77K Nov  8 10:32 numpy.h
-rw-rw-r-- 1 builder docker 9.6K Nov  8 10:32 operators.h
-rw-rw-r-- 1 builder docker 2.2K Nov  8 10:32 options.h
-rw-rw-r-- 1 builder docker 123K Nov  8 10:32 pybind11.h
-rw-rw-r-- 1 builder docker  80K Nov  8 10:32 pytypes.h
drwxrwxr-x 2 builder docker 4.0K Nov  8 10:32 stl
-rw-rw-r-- 1 builder docker  27K Nov  8 10:32 stl_bind.h
-rw-rw-r-- 1 builder docker  15K Nov  8 10:32 stl.h

Is this enough?

When I try to add pybind11 to my CMakeLists.txt:

include_directories("/cache/venv/include/pybind11")
#I tried this too: add_subdirectory("/cache/venv/include/pybind11" ".")
pybind11_add_module(pystuff binding.cpp)

I always get the same error:

CMake Error at src/applications/pycore/CMakeLists.txt:60 (pybind11_add_module):
  Unknown CMake command "pybind11_add_module".

What am I missing? I tried installing pybind11 using pip but cmake still can't find the command.

(I'm using a docker image based on ubuntu 22)

I have a very simple code to test this:

#include <pybind11/pybind11.h>

namespace py = pybind11;

int return42(){ return 42;}

PYBIND11_MODULE(pystuff, m) {
    m.def("return42", &return42, "A function that returns 42");
}

If I don't add the pybind11_add_module(pystuff binding.cpp) line on cmake, the project compiles, however I guess pybind11 needs to add the module to actually work

Ivan
  • 1,352
  • 2
  • 13
  • 31
  • I would try to use CMake's `FetchContent`, like [here](https://stackoverflow.com/a/52311760/260313). But that's an old answer. I'd prefer changing the last paragraph, from `FetchContent_GetProperties`, to `FetchContent_MakeAvailable`. Those lines would download the `pybind11` dependency before building your project. Notice though, you will still need something like `target_include_directories( $pybind11_INCLUDE_DIRS)`, and `target_link_libraries( $pybind11`). – rturrado Dec 05 '22 at 14:53
  • 1
    It's my question, its an XY problem relative to my environment – Ivan Dec 05 '22 at 15:27

1 Answers1

3

You need to use:

find_package(pybind11 REQUIRED)

If it doesn't find it right away, then pip install pybind11[global] to make the CMake bindings available from the venv root. Then include /cache/venv in CMAKE_PREFIX_PATH.

If you determine the directory containing the files pybind11*.cmake then you can set pybind11_DIR to the directory containing those files without installing pybind11[global].

In no case should you set() the variables I mentioned inside the CMakeLists.txt file. Prefer to use environment variables in Docker or the PATHS argument to find_package.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86