6
votes

I tried to install a library but I have got the following error after running make. How to recompile with flag?

make Scanning dependencies of target pwrutils Linking CXX shared library libpwrutils.so /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/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 ,/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/libboost_system.a: error adding symbols: Bad value collect2: error: ld returned 1 exit status make[2]: * [libpwrutils/libpwrutils.so.1.0] Error 1 make[1]: * [libpwrutils/CMakeFiles/pwrutils.dir/all] Error 2 make: *** [all] Error 2

2
Are you using CMake? Do you do like cmake . or similar and then make or you just have a Makefile and you invoke make ? - fedepad
yes I'm using Cmake. I've tried to delete CMakeCache.txt and run cmake again but it didn't help of course. - Kasia
You are tried to link shared library libpwrutils.so against static library libboost_system.a. This doesn't work. You should either install shared Boost library, or build your library (libpwrutils.so) as STATIC. BTW, related question describes similar problem. - Tsyvarev

2 Answers

0
votes

Extend the compiler flags within the major CMakeLists.txt as required.

############################################################
# Compiler and linker flags
set(CMAKE_CXX_FLAGS                " ${CMAKE_CXX_FLAGS_INIT} -std=c++11 -fPIC")
set(CMAKE_CXX_FLAGS_DEBUG          "-g -DDEBUG")
set(CMAKE_CXX_FLAGS_MINSIZEREL     "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE        "-O4 -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g -DDEBUG"

Delete the cache again. Run cmake and check with ccmake or cmake-gui whether CMAKE_BUILD_TYPE is not empty but one of Debug, Release, etc.

0
votes

In case if someone runs into this, and adding the fPIC flag to your project doesn't help:

I had the same problem, and my library had the -fPIC flag set up correctly within my CMakeLists, and I could verify it when looking at the compilation logs, for example, I saw: /usr/bin/c++ -fPIC ... etc.

Then I realized I link against couple other libraries, so I had to recompile both of them with the -fPIC, and that's where the problem was.