makefilecmakefpic

Compiling library with make returns error - recompile with -fPIC (no ./configure file)


I'm trying to compile KArchive from KDE (here). Obviously I'm following their instructions in the install file. First thing goes well, I just have to make a directory called build and in there I use this:

cmake .. \
  -DCMAKE_BUILD_TYPE=debug \
  -DCMAKE_INSTALL_PREFIX=/usr/local

Which goes well. The problem appears when I have to use make. After running that I get the following error:

/usr/bin/ld: /usr/local/lib/libbz2.a(bzlib.o): relocation R_X86_64_32S against symbol BZ2_crc32Table can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib/libbz2.a(compress.o): relocation R_X86_64_32 against .rodata.str1.1 can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib/libbz2.a(decompress.o): relocation R_X86_64_32S against .rodata can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/lib/libbz2.a(blocksort.o): relocation R_X86_64_32 against .rodata.str1.1 can not be used when making a shared object; recompile with -fPIC

Now, this problem is not uncommon, but most solutions haven't worked for me.


Solution

  • As we have already found the solutions in the comments section, I take time to give you a proper answer to clarify the situation.

    First of all, CMake is a great tool to generate MakeFile. You can see a CMakeLists.txt file at the root of your project where you configure all what you need to compile your project.

    Then you create and go into your build directory, an type the command cmake .. -DCMAKE_BUILD_TYPE=debug -DCMAKE_INSTALL_PREFIX=/usr/local. The .. in the command line is the path where you have your main CMakeLists.txt of your project. Additional flags can be added with -DFLAG_NAME=value.

    In your case, when you compile using make, the problem was your linker was using a static lib instead of a dynamic lib to generate a dynamic lib (That's the error message).

    Your compiler suggests to recompile the bz2 library using -fPIC flag (Position Independent Code).

    Using the command locate libbz2, you told me you had the /usr/lib/libbz2.so.1 on your computer so the problem is only a library path issue.

    In order to add the path to this library, you have a CMake flag for that, as a result, the following command solve your problem:

    cmake .. -DCMAKE_BUILD_TYPE=debug -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_LIBRARY_PATH=/usr/lib/