为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

当年话下 2023-04-18 15:58:43
我目前正在尝试在我的 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 executedIn 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
查看完整描述

2 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超6个赞

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



查看完整回答
反对 回复 2023-04-18
?
不负相思意

TA贡献1777条经验 获得超10个赞

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

使用 makefile 也可以。


查看完整回答
反对 回复 2023-04-18
  • 2 回答
  • 0 关注
  • 215 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信