Skip to content
Latchkey

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 -fPIC

Common causes

How to fix it

Recompile with -fPIC

  1. Add -fPIC to the compile flags for every object going into the shared library.
  2. 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.so

How 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

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card