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

我目前正在尝试在我的 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


当年话下
浏览 352回答 2
2回答

qq_花开花谢_0

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

不负相思意

问题解决:c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup&nbsp;PYTHONPATH=./pybind11 python -m pybind11 --includesexample.cpp -o example`python3-config --extension-suffix使用 makefile 也可以。
打开App,查看更多内容
随时随地看视频慕课网APP