python - 在制作 boost.python helloword 演示时不安全地使用相对 rpath libboost.dylib?

标签 python c++ macos boost

最近在学习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/

相关文章:

Python 字符串格式 - 字符串替换后的百分比符号

c++ - 初始化列表中 const 引用成员的初始化

macos - 无法安装 tmux。错误 : dyld: Library not loaded:/usr/local/opt/openssl/lib/libcrypto. 1.0.0.dylib

python - 函数调用中明显差异的解释

Python 使用 subprocess.Popen 关闭所有线程或进程

c++ - qplaintextedit 行间距

objective-c - 没有名为 'NSTableViewDataSource' 的类型或协议(protocol)

ruby-on-rails - 如何根据环境配置回形针保存到不同目录

python - keras LSTM 以正确的形状输入输入

c++ - 将指向 T 的指针存储在 void* 中,但 T 可以是常量或非常量 - 使用 void const* 还是仅使用 void*?