building cpp with pybind11

Viewed 4081

I'm new to pybind (and also C++). I'm trying to follow the tutorial on pybind https://pybind11.readthedocs.io/en/stable/basics.html, but I'm having trouble building it. I have a folder on the Desktop, with inside this structure:

.
├── file.txt
├── include
│   └── pybind11
│       ├── attr.h
│       ├── buffer_info.h
│       ├── cast.h
│       ├── chrono.h
│       ├── common.h
│       ├── complex.h
│       ├── detail
│       │   ├── class.h   
│       │   ├── common.h
│       │   ├── descr.h
│       │   ├── init.h
│       │   ├── internals.h
│       │   └── typeid.h
│       ├── eigen.h
│       ├── embed.h
│       ├── eval.h
│       ├── functional.h
│       ├── iostream.h
│       ├── numpy.h
│       ├── operators.h
│       ├── options.h
│       ├── pybind11.h
│       ├── pytypes.h
│       ├── stl_bind.h
│       └── stl.h
├── prova.cpp
└── share
    └── cmake
        └── pybind11
            ├── FindPythonLibsNew.cmake
            ├── pybind11Config.cmake
            ├── pybind11ConfigVersion.cmake
            ├── pybind11Targets.cmake
            └── pybind11Tools.cmake

6 directories, 31 files

My file, prova.cpp is as follow:

#include <pybind11/pybind11.h>

namespace py = pybind11;

int add(int i, int j) {
    return i+j;
}

PYBIND11_MODULE(prova, m) {
    m.def("add", &add, "A function which add two numbers");
}

Now, on the tutorial they say to build the file using this command:

c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix`

But i got this output:

c++: error: python3 -m pybind11 --includes: No such file or directory

Now, I have python install through conda, and i have an environment with all package needed. If i run on the command prompt the command python3 -m pybind11 --includes i got this output:

-I/home/luca/Programs/miniconda3/envs/fcg/include/python3.7m -I/home/luca/Programs/miniconda3/envs/fcg/lib/python3.7/site-packages/pybind11/include

Obviously, I must be inside the right conda environment. So I'm wordering if c++ is unable to use the right conda evironment. However, even if i substitute the output i got directly in the c++ command, so running:

c++ -O3 -Wall -shared -std=c++11 -fPIC '-I/home/luca/Programs/miniconda3/envs/fcg/include/python3.7m -I/home/luca/Programs/miniconda3/envs/fcg/lib/python3.7/site-packages/pybind11/include' prova.cpp -o prova'python3-config --extension-suffix'

I get this other output:

prova.cpp:1:10: fatal error: pybind11/pybind11.h: No such file or directory
#include <pybind11/pybind11.h>
      ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.

I really dunno how to fix it. Thanks to those who can help :)

2 Answers

You need to have -I"path to the folder pybind11 that has the pybind11.h file" included near the front of the c++ line.

The -I"path" tells the compiler where to find the header.

Step 1: Find the location of pybuild11 include file:

$ python3.8 -c "import pybind11;print(pybind11.get_include())"
/usr/local/lib/python3.8/site-packages/pybind11/include

Step 2: Add path to includes to compile command:

$ c++ ...[bunch of stuff]... -I '/usr/local/lib/python3.8/site-packages/pybind11/include'  ...[bunch of stuff]...
Related