13

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

Kasia
  • 166
  • 1
  • 1
  • 7
  • 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 Jan 07 '17 at 23:34
  • yes I'm using Cmake. I've tried to delete CMakeCache.txt and run cmake again but it didn't help of course. – Kasia Jan 08 '17 at 10:46
  • 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](http://stackoverflow.com/questions/26549137/shared-library-on-linux-and-fpic-error) describes similar problem. – Tsyvarev Jan 09 '17 at 05:05
  • Possible duplicate of [Shared library on Linux and -fPIC error](http://stackoverflow.com/questions/26549137/shared-library-on-linux-and-fpic-error) – Tsyvarev Jan 09 '17 at 05:06

2 Answers2

7

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.

vicrucann
  • 1,703
  • 1
  • 24
  • 34
0

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.

Th. Thielemann
  • 2,592
  • 1
  • 23
  • 38