Linux上的共享库和-fPIC错误

我正在尝试使用Cmake创build的Makefile来编译Linux中的共享库,但是运行make让我得到以下错误:

Linking CXX shared library libcpp-lib.so /usr/bin/ld: /home/davide/Desktop/boost_1_55_0/stage/lib/libboost_system.a(error_code.o): relocation R_X86_64_32 against .rodata.str1.1 can not be used when making a shared object; recompile with -fPIC /home/davide/Desktop/boost_1_55_0/stage/lib/libboost_system.a: could not read symbols: Bad value collect2: ld returned 1 exit status make[2]: *** [libcpp-lib.so] Error 1 make[1]: *** [CMakeFiles/cpp-lib.dir/all] Error 2 make: *** [all] Error 2 

我在CMakeLists.txt中提供了以下命令来说明我需要共享(.so)库:

add_library(cpp-lib SHARED ${CPP_FILES})

我还需要指定什么以避免上面显示的-fPIC错误?

非常感谢

boost库需要使用-fPIC进行编译:请看看: 如何用boost.python编译静态库和-fPIC

尝试通过cmake在你的项目中添加编译器标志:

 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")