einverne einverne - 1 month ago 9
C++ Question

unsafe use of relative rpath libboost.dylib when making boost.python helloword demo?

Recently, I am learning boost C++ library. I want to use python to call exist C++ project. I have install boost under OSX 10.11 using

brew install boost
. My python version 2.7.

I make a hello.c:

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

#include <boost/python.hpp>

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


and Makefile:

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


However, after I run
make
and got hello.so. I met following error when I run python code:

import hello
print hello.greet()


error:

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

Answer Source

Take this link as a reference.

To my problem, use 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)

you can see that libboost_python.dylib is not point to the really exist path.

so use this command:

install_name_tool -change libboost_python.dylib /usr/local/lib/libboost_python.dylib hello.so 

and run otool -L hello.so again:

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)

and finally run python test.py, I get the result.