Problem
When I try to link a static library to a shared library, I receive the following error.
/usr/bin/ld: ../../../libraries/log4cplus/liblog4cplus.a(fileappender.o): relocation R_X86_64_32S against `a local symbol' can not be used when making a shared object; recompile with -fPIC
../../../libraries/log4cplus/liblog4cplus.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
However, on a 32-bit system, this worked without a hitch. I tried manually adding the -fPIC parameters to the Makefile, but that didn’t help.
I tried the -whole-archive flag as recommended here, but it didn’t work.
/usr/bin/ld: ../../../libraries/log4cplus/liblog4cplus.a(appenderattachableimpl.o): relocation R_X86_64_32S against `vtable for log4cplus::spi::AppenderAttachable' can not be used when making a shared object; recompile with -fPIC
../../../libraries/log4cplus/liblog4cplus.a(appenderattachableimpl.o): could not read symbols: Bad value
collect2: ld returned 1 exit status
Asked by Thiyagarajan
Solution #1
If you’re making a shared library, the most likely cause is that the version of liblog4cplus.a you’re using wasn’t built with -fPIC. You may check this in Linux by removing the object files from the static library and looking at their relocations:
ar -x liblog4cplus.a
readelf --relocs fileappender.o | egrep '(GOT|PLT|JU?MP_SLOT)'
If the output is empty, the static library is not position-independent and hence cannot be used to create a shared object.
The -fPIC flag will not assist because the static library contains object code that has already been built.
You must obtain a copy of liblog4cplus.a that has been constructed with -fPIC and use it instead.
Answered by fons
Solution #2
After CMAKE CXX FLAGS and CMAKE C FLAG, add -fPIC.
Example:
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall --std=c++11 -O3 -fPIC" )
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -fPIC" )
This was the solution to my problem.
Answered by user13777568
Solution #3
When installing FCL that requires CCD lib(libccd), I get an error like this:
I discovered that there are two files titled “libccd.a”:
I was able to address the issue by deleting the first file.
Answered by Saeed Mohtasham
Solution #4
R X86 64 PC32 R X86 64 PC32 R X86 64 PC32 R_ When LDFLAGS are set with hardening and CFLAGS are not, this results in an undefined symbol. Maybe it’s just a case of user error: You must additionally use -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 if you use -specs=/usr/lib/rpm/redhat/redhat-hardened-ld at link time. You need either both or drop the -specs=/usr/lib/rpm/redhat/redhat-hardened-ld at compile time because you are compiling and linking at the same time. Fixes that are commonly used: https://bugzilla.redhat.com/show bug.cgi?id=1304277#c3 https://github.com/rpmfusion/lxdream/blob/master/lxdream-0.9.1-implicit.patch
Answered by Sérgio
Solution #5
When I tried to link static generated fontconfig and expat into a linux shared object, I ran across identical issues:
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /3rdparty/fontconfig/lib/linux-x86_64/libfontconfig.a(fccfg.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /3rdparty/expat/lib/linux-x86_64/libexpat.a(xmlparse.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
[...]
This is in contrast to the fact that I was already sending -fPIC flags through the CFLAGS variable, and that other compilers/linkers (clang/lld) worked fine with the identical build configuration. It turns out that these dependencies use special autoconf scripts to manage position-independent code settings, and that they require the —with-pic switch during build setup on linux gcc/ld, and that its absence presumably overrides the CFLAGS value. The dependencies will be successfully compiled with -fPIC if you pass the switch to configure script.
Answered by ceztko
Post is based on https://stackoverflow.com/questions/19768267/relocation-r-x86-64-32s-against-linking-error