我目前正试图在我的Mac上设置Pybind。我遵循以下说明:https://pybind11.readthedocs.io/en/stable/basics.html。
我在我的计算机上克隆了pybind,在该repo中创建了构建目录,并运行了测试用例(请检查-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
发布于 2020-08-01 17:45:38
文件不在包含路径上。您可以将其安装到计算机中,默认包含路径,也可以将-I path_to_directory_containing_pybind11添加到编译命令中。
发布于 2020-08-03 15:53:40
问题解决: c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup PYTHONPATH=./pybind11 python -m pybind11 --includes example.cpp -o example3-config--扩展-后缀
使用makefile也有效。
https://stackoverflow.com/questions/63207598
复制相似问题