ld "relocation R_X86_64_PC32 against symbol (recompile with -fPIC)" in CI
Shared libraries require position-independent code. An object compiled without -fPIC contains absolute relocations the linker cannot place in a .so.
What this error means
Building a shared library or linking a static lib into one fails with "relocation R_X86_64_PC32 ... can not be used when making a shared object; recompile with -fPIC".
ld
/usr/bin/ld: util.o: relocation R_X86_64_PC32 against symbol 'data' can not be used
when making a shared object; recompile with -fPICCommon causes
How to fix it
Recompile with -fPIC
- Add -fPIC to the compile flags for every object going into the shared library.
- In CMake, set POSITION_INDEPENDENT_CODE ON on the target.
ld
gcc -fPIC -c util.c -o util.o
gcc -shared util.o -o libutil.soHow to prevent it
- Compile every object destined for a shared library with -fPIC, or set CMAKE_POSITION_INDEPENDENT_CODE for the project.
Frequently asked questions
What causes ""recompile with -fPIC""?
Building a shared library or linking a static lib into one fails with "relocation R_X86_64_PC32 ... can not be used when making a shared object; recompile with -fPIC".
How do I fix "recompile with -fPIC"?
Recompile with -fPIC
Related guides
ld "DSO missing from command line" in CIFix ld "DSO missing from command line" in CI - a symbol resolves through a shared library that was relied on…
ld "multiple definition of 'X'" in CIFix ld "multiple definition of 'X'" in CI - the same symbol is defined in more than one object file, breaking…
ld "relocation R_X86_64_... can not be used" - Recompile with -fPICFix ld "relocation R_X86_64_32 against ... can not be used when making a shared object; recompile with -fPIC"…