Linker settings
I'm trying to link boost.python with my application in cmake build-system. The most important parts in my CMakeList.txt:
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost COMPONENTS filesystem system date_time python REQUIRED)
add_definitions(-Wall -g -msse2 -fPIC)
And, of course, including/linking boost's and python's libraries/headers.
Now this fails with:
/usr/bin/ld: libboost_python.a(from_python.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
libboost_python.a: could not read symbols: Bad value
I decided to go to the cmake t开发者_开发问答emporary build folder and find generated object file (*.o). I've applied command for it:
g++ -shared FILE.cpp.o -L/usr/lib -lboost_python -o MyModule.so
As a result I got workable MyModule module for python! So, lets look at cmake linker string and find the difference. I ran make with VERBOSE=1:
/usr/bin/c++ -fPIC -g -shared -Wl,-soname,libScript.so -o ../../lib/libScript.so CMakeFiles/LogManagerWrapper.cpp.o -Wl,-Bstatic -lboost_filesystem -lboost_system -lboost_date_time -lboost_python -Wl,-Bdynamic -lpython2.7
During excluding some parts of the linker flags I found that my problem is in -Wl,-Bstatic before boosts' libraries list.
I wonder from where that argument came because I noticed Boost_USE_STATIC_LIBS OFF in CMakeList.txt.
What's wrong?
You did not show the code that uses the Boost libraries, so I assume it's just:
target_link_libraries(foo ${Boost_LIBRARIES})
Could you please add
message("XXX ${Boost_LIBRARIES}")
around that point? I suspect that will print a list of libraries with .a extension, which CMake then converts into that -Wl,-Bstatic -lxxx
. Examine the path where .a libraries are located and check if there are .so counterparts. My guess is that you actually have only static libraries built.
精彩评论