问题描述
我正在测试 pybind11
在我的cpplcm.py
#include <pybind11/pybind11.h>
int greatest_common_divisor(int x,int y) {
int r,tmp;
if(x < y){
tmp = x;
x = y;
y = tmp;
}
r= x % y;
while(r != 0){
x = y;
y = r;
r = x % y;
}
return y;
}
int least_common_multiple(int x,int y) {
return x * y / greatest_common_divisor(x,y);
}
PYBIND11_MODULE(cpplcm,m) {
m.doc() = "pybind11 example";
m.def("greatest_common_divisor",&greatest_common_divisor,"A function which calculate greatest common divisor");
m.def("least_common_multiple",&least_common_multiple,"");
}
我编译并制作了 cpplcm.cpython-37m-darwin.so
$g++ -O3 -Wall -shared -std=c++11 -fPIC `python -m pybind11 --includes` cpplcm.cpp -o cpplcm`python3-config --extension-suffix` -lpython3.7m
然后在同目录下的test.py
import cpplcm
Fatal Python error: PyMUTEX_LOCK(_PyRuntime.ceval.gil.mutex) Failed
可能是内存问题???
任何解决的建议都有助于谢谢。
解决方法
您的操作系统必须是 MacOS,因此您可以尝试:
g++ -O3 -Wall **-Xlinker -undefined -Xlinker dynamic_lookup -flto=thin** -std=c++11 -fPIC `python -m pybind11 --includes cpplcm.cpp` -o cpplcm `python3-config --extension-suffix`