我正在尝试使用通过Cmake创建的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错误,我还需要指定什么?

在此先多谢

最佳答案

需要使用-fPIC编译boost库:
请看一下:How to compile static library with -fPIC from boost.python

尝试在项目中按cmake添加编译器标志:

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

关于c++ - Linux和-fPIC上的共享库错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26549137/

10-12 02:58