Как скомпилировать pybind11?

Всем привет. Учусь использовать pybind11. Нужно скомпилировать код. Код программы:

#include <pybind11/pybind11.h>


namespace py = pybind11;

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

PYBIND11_MODULE(example, m) {
    m.doc() = "pybind11 example plugin"; // optional module docstring

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

Устанавливала на linux с помощью этих команд:

apt-get install cmake
pip3 install pytest

git clone https://github.com/pybind/pybind11.git 
cd pybind11 
mkdir build 
cd build 
cmake .. 
make install

Пытаюсь компилировать командой: c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix)

Но появляется ошибка:

/usr/local/include/pybind11/detail/common.h:124:20: fatal error: Python.h: No such file or directory
compilation terminated.

Сможете объяснять куда и какие пути прописывать в команде компиляции


Ответы (1 шт):

Автор решения: Андрей

Если открыть документацию по pybind11, можно найти замечание:

If you used Include as a submodule to get the pybind11 source, then use $(python3-config --includes) -Iextern/pybind11/include instead of $(python3 -m pybind11 --includes) in the above compilation, as explained in Building manually.

У Вас есть два способа:

  1. Сделать клон исходного кода pybind11 с Гитхаба, собрать его на своей машине с помощью cmake, положить cpp-файл в директорию pybind11 (заметьте, не pybind11/build/) и скомпилировать с помощью

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

  1. Установить pybind11 из репозитория с помощью apt install pybind11-dev и скомпилировать cpp-файл в любой директории командой

c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3-config --includes) -Iextern/pybind11/include example.cpp -o example$(python3-config --extension-suffix)

→ Ссылка