Linux shared library and -fPIC error

I am trying to compile a shared library on Linux using the Makefile created with Cmake, but run make to get the following error:

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 

I provide the following command in CMakeLists.txt to say that I need a shared (.so) library:

add_library(cpp-lib SHARED ${CPP_FILES})

What else do I need to point out to avoid the -fPIC error shown above?

Thank you very much in advance

+5
source share
1 answer

Acceleration libraries must be compiled using -fPIC: See: How to compile a static library with -fPIC from boost.python

Try adding compiler flags to cmake in your project:

 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") 
+6
source

Source: https://habr.com/ru/post/1205440/


All Articles