构建 Pybind11 - 测试用例不会 运行?

Building Pybind11 - Test Case will not Run?

我目前正在尝试在我的 Mac 上设置 Pybind。我遵循这些说明:https://pybind11.readthedocs.io/en/stable/basics.html.

我已经在我的计算机上克隆了 pybind 存储库,在该存储库中创建了构建目录,并且 运行 测试用例 (make check -j 4)。

这是我的目录布局:

Home/
    ---example.cpp
    ---pybind11/

我有一个我写的 example.cpp 文件:

#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";

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

我使用以下命令和标志编译它(来源:https://pybind11.readthedocs.io/en/stable/compiling.html#building-manually):

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

我收到以下错误:

example.cpp:1:10: fatal error: 'pybind11/pybind11.h' file not found
#include <pybind11/pybind11.h>
         ^~~~~~~~~~~~~~~~~~~~~
1 error generated.

当我将文件移动到 pybind11 存储库时,我可以成功编译,但执行时出现以下错误:

zsh: exec format error: ./example.cpython-37m-darwin.so

非常感谢任何帮助。


跟进:我认为 Seth 让我更接近成功(多谢),但我仍然遇到这个问题:

    c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` example.cpp -o example`python3-config --extension-suffix` -I pybind11/include/ 
/Applications/Xcode.app/Contents/Developer/usr/bin/python3: No module named pybind11.__main__; 'pybind11' is a package and cannot be directly executed
In file included from example.cpp:1:
In file included from pybind11/include/pybind11/pybind11.h:44:
In file included from pybind11/include/pybind11/attr.h:13:
In file included from pybind11/include/pybind11/cast.h:13:
In file included from pybind11/include/pybind11/pytypes.h:12:
pybind11/include/pybind11/detail/common.h:122:10: fatal error: 'Python.h' file not found
#include <Python.h>
     ^~~~~~~~~~
1 error generated.

尝试听从这些人关于 github 的建议,但我得到同样的错误:https://github.com/pybind/pybind11/issues/1728#issuecomment-616619910

文件 不在您的包含路径中。您可以将它安装到您机器的默认包含路径中,或者将 -I path_to_directory_containing_pybind11 添加到您的编译命令中。

问题已解决:c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup PYTHONPATH=./pybind11 python -m pybind11 --includes example.cpp -o example`python3-config --extension-suffix

也可以使用 makefile。