C++: relocation R_X86_64_32S can not be used when making a shared object
A shared library must be loadable at any address, so every object in it needs position independent code. The object triggering this is nearly always from a static library built without -fPIC and then linked into a .so, which is why the advice to recompile applies to that dependency rather than to the file being built.
Quick fix
Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.
# Which object is at fault: the linker names it just before the advice
make 2>&1 | grep -B3 'recompile with -fPIC'
# Confirm a static library lacks PIC
readelf --relocs libfoo.a | grep -c R_X86_64_32S
# Rebuild the dependency with PIC and relink
make CFLAGS='-fPIC -O2' CXXFLAGS='-fPIC -O2'
./configure --with-pic && make
# CMake: set it on the target, or globally for the build
set_property(TARGET foo PROPERTY POSITION_INDEPENDENT_CODE ON)
cmake -S . -B build -DCMAKE_POSITION_INDEPENDENT_CODE=ON
# Distributions ship a PIC variant for exactly this reason: prefer it
apt-file search libfoo_pic.a
# Building an executable instead of a library also removes the constraint
How to diagnose C++ errors
C++ errors fall into two very different worlds. Compile and link errors (undefined reference, multiple definition, template substitution failure) are deterministic and are almost always about declarations, the One Definition Rule, or link order. Runtime memory errors (segfaults, double free, use-after-free) are non-deterministic and should never be debugged by reading code alone; a sanitizer will find in seconds what code review misses for days.
If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.
- Rebuild with sanitizers before anything else:
-fsanitize=address,undefined -fno-omit-frame-pointer -g. AddressSanitizer reports the exact allocation and free sites for use-after-free and double-free. - For undefined-reference errors, check link order, remembering that with GNU ld libraries must come after the objects that use them, and check for a C/C++ linkage mismatch that needs
extern "C". - Use
nm -C libfoo.a | grep symbolto confirm the symbol is actually present and to see the demangled signature. A signature that differs by aconstis a different symbol. - Enable core dumps (
ulimit -c unlimited) and open them in gdb withgdb ./binary core, thenbt full. A stack trace beats speculation. - For template errors, read the message from the bottom up. The final line is usually the real constraint that failed; everything above is instantiation context.
Tools worth reaching for
-fsanitize=address,undefinedvalgrindgdb / lldbnm -Cldd
Authoritative references
Primary documentation for this error, worth reading before applying any fix in production.
Related C++ errors
- C++: runtime error: signed integer overflow (UBSan)Signed integer overflow is undefined behaviour in C and C++. The program may appear to work…
- C++: Segmentation fault (core dumped)Program attempted to access memory it doesn't own (e.g., dereferencing null pointer, buffer…
- C++: undefined reference to `vtable for X'This is not a missing call to the named function. The compiler emits a class's vtable…
- CMake Error: Could not find a package configuration file provided by Xfind_package looked for a config file the library installs, XConfig.cmake or x-config.cmake…
- double free or corruptionHeap memory was freed twice, corrupting the memory allocator. Often caused by manual delete…
- heap-use-after-freeAccessing memory after it has been freed. Detected by AddressSanitizer. Causes undefined…
- multiple definition of 'symbol'The same symbol is defined in multiple translation units. Usually caused by defining…
- stack-buffer-overflowA write exceeded the bounds of a stack-allocated buffer. Detected by AddressSanitizer or…
Browse other categories
- HTTP 494xx client errors, 5xx server errors, redirects, headers and protocol problems.
- JavaScript 42npm resolution, async pitfalls, hydration, memory limits and runtime type…
- Database 41Connections, deadlocks, constraints, replication and memory limits.
- AI 35Rate limits, context windows, GPU memory and model-serving failures.
- Network 35Refused connections, timeouts, resets, MTU problems and port exhaustion.
- Python 35Imports, virtual environments, encoding, concurrency and dependency conflicts.
- Kubernetes 34CrashLoopBackOff, ImagePullBackOff, OOMKilled, RBAC, scheduling and storage.
- Docker 27Daemon connectivity, disk space, image pulls, ports and architecture mismatches.
- System 26Disk space, systemd units, file descriptors, OOM killer and scheduled jobs.
- Cloud 25IAM permissions, quotas, service limits and credential failures.
- Security 25JWT validation, CSRF, OAuth grants, SELinux, SSH host keys and CSP.
- TLS 24Untrusted authorities, expiry, hostname mismatch, chains and cipher negotiation.
Something missing or wrong?
This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.