不安全地使用相对路径libboost.dylib制作时boost.pythonhelloword演示?

2024-05-14 12:34:10 发布

您现在位置:Python中文网/ 问答频道 /正文

最近,我在学习Boost C++库。我想用Python调用存在C++项目。我用brew install boost在osx10.11下安装了boost。我的python版本2.7。在

我打个招呼。c:

char const* greet()
{
    return "hello, world";
}

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello)
{
    using namespace boost::python;
    def("greet", greet);
}

和Makefile:

^{pr2}$

但是,在我运行make并得到你好。所以. 运行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

Tags: 项目intestimporthelloso错误users
2条回答

以这个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不是指向真正存在的路径。在

所以使用以下命令:

^{pr2}$

然后再次运行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,得到结果。在

在MacOS上,建议更改Boost动态库本身,而不是更改链接到它们的可执行文件或其他动态库。在包含libboost_XXX.dylib库的目录中运行下面给出的bash脚本:

#!/bin/bash

# Modify the absolute dylib paths baked into the libraries
for i in *.dylib
do
    FULLPATH=`pwd`/$i
    install_name_tool -id $FULLPATH $i
    echo -change $i $FULLPATH
    done > changes
for i in *.dylib
do
    install_name_tool `cat changes` $i
done
rm changes

在构建Boost库之后,只需要执行一次。不需要在链接到它们的可执行文件上胡闹。在

相关问题 更多 >

    热门问题