最近在学习boost C++库。我想用 python 调用现有的 C++ 项目。我已经使用 brew install boost
在 OSX 10.11 下安装了 boost。我的 python 版本 2.7。
我做了一个 hello.c:
char const* greet()
{
return "hello, world";
}
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello)
{
using namespace boost::python;
def("greet", greet);
}
和生成文件:
PYTHON_VERSION = 2.7
PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)
# location of the Boost Python include files and library
#
BOOST_INC = /usr/local/include
BOOST_LIB = /usr/local/lib
#
# compile mesh classes
TARGET = hello
$(TARGET).so: $(TARGET).o
g++ -shared -Wl $(TARGET).o -L$(BOOST_LIB) -lboost_python -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) -o $(TARGET).so
$(TARGET).o: $(TARGET).c
g++ -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).c
但是,在我运行 make
并得到 hello.so 之后。我在运行 python 代码时遇到以下错误:
import hello
print hello.greet()
错误:
Traceback (most recent call last):
File "test.py", line 4, in <module>
import hello
ImportError: dlopen(/Users/einverne/boost_test/hello.so, 2): Library not loaded: libboost_python.dylib
Referenced from: /Users/einverne/boost_test/hello.so
Reason: unsafe use of relative rpath libboost_python.dylib in /Users/einverne/boost_test/hello.so with restricted binary
最佳答案
拿这个link作为引用。
对于我的问题,使用 otool -L hello.so
:
hello.so:
hello.so (compatibility version 0.0.0, current version 0.0.0)
libboost_python.dylib (compatibility version 0.0.0, current version 0.0.0)
/System/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.10)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.1.1)
可以看到libboost_python.dylib并没有指向真正存在的路径。
所以使用这个命令:
install_name_tool -change libboost_python.dylib /usr/local/lib/libboost_python.dylib hello.so
然后再次运行otool -L hello.so
:
hello.so:
hello.so (compatibility version 0.0.0, current version 0.0.0)
/usr/local/lib/libboost_python.dylib (compatibility version 0.0.0, current version 0.0.0)
/System/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.10)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.1.1)
最后运行python test.py
,我得到了结果。
关于python - 在制作 boost.python helloword 演示时不安全地使用相对 rpath libboost.dylib?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33281753/