12

I am trying to follow the simple example for embedding python within c++ using pybind11 as found on this page. However, when trying to use cmake to build the solution, I keep getting an error that says

By not providing "Findpybind11.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "pybind11", but CMake did not find one.

Could not find a package configuration file provided by "pybind11" with any of the following names:

pybind11Config.cmake
pybind11-config.cmake

I have a folder called pybindtest on my Desktop which includes CMakeLists.txt and main.cpp as described in the link above, as well as a build folder that I created. While in the build folder, I have tried the following lines to no avail (running on Powershell 7):

cmake ..
cmake .. -Dpybind11_DIR=C:/Users/ben.wolfley/Anaconda3/Library/share/cmake/pybind11/pybind11Config.cmake
cmake .. -DCMAKE_MODULE_PATH=C:/Users/ben.wolfley/Anaconda3/Library/share/cmake/pybind11

I installed pybind11 using conda install pybind11, and pybind11Config.cmake is in C:\Users\ben.wolfley\Anaconda3\Library\share\cmake\pybind11

bwolf
  • 167
  • 1
  • 1
  • 6
  • If you try using the cmakegui, there should be a variable called pybind11_DIR or something. Can you try to set it to the *.cmake dir and try again? – user3389943 Aug 04 '20 at 20:51
  • Or I think you were supposed to set the CMAKE_PREFIX_PATH and not the CMAKE_MODULE_PATH – user3389943 Aug 04 '20 at 20:54
  • "... and pybind11Config.cmake is in `C:\Users\ben.wolfley\Anaconda3\Library\share\cmake\pybind11`" - So set `pybind11_DIR` variable to that **directory** as the error message suggests (not to the **file** in it, as you currently set). – Tsyvarev Aug 04 '20 at 21:09

4 Answers4

16

In case someone having the same issue without Anaconda, like directly with pip pybind11 or manual clone installation, both caused problems in my case. Manual installation of pybind11 with git didn't install the cmake config pybind11Config.cmake, although there is a tools/pybind11Config.cmake.in file, that I couldn't turn into a proper pybind11Config.cmake.

Installation pybind11 global with pip solved it for me, and automatically uninstalled the manual git installation:

pip install "pybind11[global]"

which installed both pybind11 and pybind11-global with proper cmake config like Anaconda does.

KeitelDOG
  • 4,750
  • 4
  • 18
  • 33
3

Add following to CMakeLists.txt:

```cmake
# set pybind11 dir
set(pybind11_DIR /Users/Caleb/Softwares/pybind11)
find_package(pybind11 REQUIRED)
```
Caleb
  • 341
  • 2
  • 7
  • Thanks, this really helped. For future help seekers: For me [using Ubuntu 22.04, Python 3.10], the pybind11 directory was located at: /usr/local/lib/python3.10/dist-packages/ You can find out your PyBind11 directory via CMD line: Type "python -v" to enter the Python console --> detailed information on all loaded packages will be given. Then, within the Python console, just use "import pybind11", and in the first output line you should see the storage location for pybind11. Hope that helps! – asti205 Mar 28 '23 at 17:33
  • For those who installed pybind11 with PyPI, you need to either set full directory all the way to '.cmake' files by `set(pybind11_DIR python_env/site-packages/pybind11/share/cmake/pybind11)` or find packages in config mode by `find_package(pybind11 REQUIRED CONFIG)`. – MrCrHaM May 08 '23 at 02:50
2

Thanks to Tsyvarev for pointing me in the right direction. The following command worked:

cmake .. -G "Visual Studio 15 2017" -A x64 `
      -Dpybind11_DIR=C:/Users/ben.wolfley/Anaconda3/Library/share/cmake/pybind11/

I was pointing to the .cmake file instead of the file's directory. I also had to specify the compiler in order for the code to work.

bwolf
  • 167
  • 1
  • 1
  • 6
0

I followed this tutorial to get started with pybind11: https://www.blopig.com/blog/2021/03/c-python-bindings-in-5-minutes/ That works VERY easily, and doesn't require having a CMake script all at.

But... I wanted to edit my code from within my IDE of choice (Qt Creator), and have that provide intellisense for me and whatnot. As such, I added CMake to the project for that benefit (Qt Creator can lean on it).

So, to get my IDE to simply not complain about missing includes (and to even compile the code if I want - though not actually build the whole pybind wrapper), all I needed to do was add a couple of include paths. In my environment, they were as follows:

include_directories(/usr/include/python3.6m)
include_directories(/home/johndoe/.local/lib/python3.6/site-packages/pybind11/include)

That, of course, is not portable. So to address that, I dropped in a macro which would allow me to implement a better solution using environmental variables:

macro(include_envvar_directory varname)
    if(DEFINED ENV{${varname}})
        set(dir_path $ENV{${varname}})
    else()
        message(FATAL_ERROR "ERROR: Env var ${varname} must be defined!")
    endif()
    if(NOT EXISTS ${dir_path})
        message(FATAL_ERROR
            "ERROR: Invalid path: ${dir_path} specified by env var: ${varname}")
    endif()
    message(STATUS "Adding to include path: ${dir_path}")
    include_directories(${dir_path})
endmacro()

include_envvar_directory(PY_INCLUDE_PATH)
include_envvar_directory(PYBIND_INCLUDE_PATH)

With that in place, I passed the PY_INCLUDE_PATH and PYBIND_INCLUDE_PATH env vars to the CMake via the IDE's feature to define such per project / workstation.

BuvinJ
  • 10,221
  • 5
  • 83
  • 96