pybind / pybind11

Seamless operability between C++11 and Python

Home Page:https://pybind11.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pybind11/pybind11.h cannot be resolved in CMakeLists with Clion

zhengcharlie8 opened this issue · comments

Issue description

I am trying to resolve this header in Clion with the following in my CMakeLists.txt

add_subdirectory(pybind11)

I have cloned the pybind11 in my project root folder and checkout the v2.2 branch.

However, Clion(2019.1.4) has no way to resolve this header and the main.cpp cannot run now.

/home/charlie/pokemon/pokeai/src/life_cycle/main.cpp:2:10: fatal error: pybind11/pybind11.h: No such file or directory
 #include <pybind11/pybind11.h>
          ^~~~~~~~~~~~~~~~~~~~~

When I build the shared object file and run it using Python 3.6, it has segmentation fault.

charlie:cmake-build-debug$ python3.6 main.py 
Segmentation fault (core dumped)

what I have tried

I tried https://github.com/pybind/cmake_example and it worked. But after I move the files from my project to this working template with my CMakeLists(attached in the link below), the <pybind11/pybind11.h> fails to resolve again.

Reproducible example code

#include <pybind11/pybind11.h>
#include "CommonHeader.h"
#include "Team.h"
#include "battle/Battle.h"

https://bitbucket.org/pokeai/pokeai/src/reproducible-pybind-include-issue/src/life_cycle/main.cpp
I have also made the whole code in this branch public and comment out the irrelevant part. You can checkout the reproducible-pybind-include-issue and reproduce the problem locally.

Not sure if this solves the issue, but you're template doesn't follow the guides. Your CMakeLists.txt has to include this at least

find_package(PythonLibs 3 REQUIRED)

add_subdirectory(pybind11)
pybind11_add_module(${PROJECT_NAME} src/${PROJECT_NAME}.cpp src/bindings.cpp # more files...)
target_include_directories(${PROJECT_NAME} PRIVATE ${PYTHON_INCLUDE_DIRS} include)

But, more importantly I think, looking at your project, it doesn't seem like you've added pybind11 as a submodule. Do that with

git submodule add https://github.com/pybind/pybind11
git submodule init
git submodule update

and then commit

@juliangaal Thank you for sharing! I also have the exact same problem. The main.cpp shown in the above link reflects the issue:

  1. "add_executable" and "pybind11_add_module" cannot work at the same time. I git clone the branch above.

(1) If "add_executable" is commented out in CMakeLists.txt, then I can build the .so file in CLion (using shortcut ctrl+F9).

(2) If "add_executable" is not commented out, then the pybind11.h cannot be resolved and the binary from main.cpp cannot be generated.

  1. Also, I find that in this example cannot be included to build the .so file, be it from boost or c++17 std::experimental::filesystem

Any suggestion?

You should only use add_executable for .cpp files with a main function, that will actually run. I would create bindings in a separate .cpp. You can then compile that and all library files like so

find_package(PythonLibs 3 REQUIRED)

add_subdirectory(pybind11)
pybind11_add_module(${PROJECT_NAME} src/${PROJECT_NAME}.cpp src/bindings.cpp # more files...)
target_include_directories(${PROJECT_NAME} PRIVATE ${PYTHON_INCLUDE_DIRS} include)

Have a look at this repo to see how you would normally structure it.

Here's another more complex example with pybind_bindings. I hope the cmake file here can help you out

The code is not accessible anymore, and there was no further reaction to @juliangaal's answers (thanks for answering, @juliangaal!). Closing. Reopen if necessary.