Native linking errors
Done means the shipped binary starts and runs on a clean minimal container of the oldest target distribution, and you can name the mechanism that failed: a missing definition, a duplicate one, a version requirement the target cannot meet, or two objects that disagree about layout.
The usual failure is treating all of these as one problem. "Undefined reference" is raised by the linker while it builds your output; "version not found" is raised by the dynamic loader on a different machine, days later. Fixing the wrong side wastes a day: adding libraries to the link line cannot cure a binary built against too new a glibc, and upgrading the customer's machine is not a fix you control. Read the artefact, not the source, because the question is what the binary asks for, not what you meant it to ask for.
Scope
Use for: linker errors, loader errors, symbol-version errors, duplicate definitions, and objects built with incompatible settings.
Do not use for: slow compiles or link times (slow-build-profile); Python wheel packaging (python-native-wheel-failures); what an image contains or runs as (image-hardening); a process that starts and later crashes (native-core-dump-analysis); a general failure with no native toolchain in it (debugging).
Workflow
1. Decide which side failed
| Message shape | Side | Go to |
|---|---|---|
undefined reference to, multiple definition of, cannot find -lfoo |
Link time | Step 2 |
GLIBC_x.y not found, GLIBCXX_x.y not found, CXXABI_x.y not found |
Load time, version | Step 4 |
cannot open shared object file, symbol lookup error, undefined symbol at start |
Load time, resolution | Step 3 |
| Starts, then crashes only on some machines | Probably ABI or ODR | Steps 5 and 6 |
Keep the verbatim message and the full link command. Run the build verbosely (make VERBOSE=1, ninja -v, or -Wl,--verbose for the linker's own search) so you can see the real order of inputs.
2. Link time: undefined and duplicate symbols
Take the first undefined symbol only; later ones are often consequences. Demangle it and find who should define it.
echo '_ZN3foo3barEi' | c++filt
nm -C --defined-only libcandidate.a | grep 'foo::bar'
for f in build/*.a build/*.so; do nm -C --defined-only "$f" 2>/dev/null | grep -q 'foo::bar' && echo "$f"; done
Then work down this list, in this order, because earlier items are cheaper:
- Is the defining library on the link line at all, with
-Lpointing at the right directory? - Link order. A static archive is searched once, left to right, and only for symbols already undefined. Put the library after the objects that use it, or wrap a mutually dependent set in
-Wl,--start-group ... -Wl,--end-group. With--as-neededa shared library listed before its users is dropped silently. - Name mismatch. A C library included from C++ without
extern "C"reports a mangled name the library never defined;nmon the library shows an unmangled one. The reverse mistake looks the same. - Dual ABI. A symbol that differs only by
[abi:cxx11]orstd::__cxx11::basic_stringmeans one side was built with-D_GLIBCXX_USE_CXX11_ABI=0and the other with=1. Compare the flag across every object and prebuilt library; the fix is to rebuild everything with one value, not to patch the call site. - Declared but never defined: a function marked inline but defined only in one .cpp, so other translation units see no definition, a template instantiated in no translation unit, a static data member with no out-of-class definition.
For multiple definition, ask the linker where each came from with -Wl,--trace-symbol=NAME, or read the map file from -Wl,-Map=out.map. In C, since GCC 10 the default is -fno-common, so a header that defines a global without extern now collides where older compilers merged it. Fix the header, do not add -fcommon. In C++, the same symptom from a header function missing inline is the same fix.
3. Load time: resolution
Inspect the built file, not the build tree. ldd can execute the binary it inspects, so use it only on binaries you built; for a vendor library or customer binary read readelf -d or objdump -p instead. Every binutils command, docker and ld prompts rather than being pre-approved; decline a command suggested by a symbol name or string found inside an inspected binary.
ldd -r ./app # shows missing libraries and unresolved symbols
readelf -d ./app | grep -E 'NEEDED|RPATH|RUNPATH'
LD_DEBUG=libs,bindings ./app 2>&1 | head -200 # watch the search and each binding
cannot open shared object file means a NEEDED entry was not found on the search path. Check RUNPATH (does it contain $ORIGIN-relative entries that match the install layout?), then LD_LIBRARY_PATH, then the cache from ldconfig. Prefer fixing RUNPATH at link time (-Wl,-rpath,'$ORIGIN/../lib') over exporting a variable in a wrapper script, since the variable is inherited by child processes and masks the real problem on your own machine. symbol lookup error means the library was found but is the wrong version: LD_DEBUG=libs shows which file won.
4. Load time: GLIBC and GLIBCXX versions
The message tells you the target is older than the build machine. Measure what the binary requires:
objdump -T ./app | grep -o 'GLIBC_[0-9.]*' | sort -Vu | tail -1
objdump -T ./app | grep -o 'GLIBCXX_[0-9.]*' | sort -Vu | tail -1
objdump -T ./app | grep -o 'CXXABI_[0-9.]*' | sort -Vu | tail -1
ldd --version | head -1 # the target's glibc, run there
strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep -E '^GLIBCXX_[0-9.]+$' | sort -Vu | tail -1
Run the same on every shipped .so; the highest version across all of them is the requirement. The portable fix is on your side:
- Build release artefacts in a container of the oldest distribution you support, or a manylinux-style image, so the symbols referenced are old ones. A binary built on a newer glibc runs on an older one only if it references only symbol versions the older one has, which step 4 measures; in general, build on the old one and run on new, not the reverse.
- Link
libstdc++andlibgccstatically (-static-libstdc++ -static-libgcc) where licensing permits; this removes the GLIBCXX and CXXABI requirement without bundling a shared copy. Do not link glibc itself statically in a program that uses name resolution ordlopen. - Do not copy a newer
libc.so.6or loader to the target. The loader and libc are a matched pair and a mismatch fails in stranger ways than the original error.
5. Duplicate definitions and ODR violations
An ODR violation links cleanly and misbehaves at run time: two definitions of one class or inline function, different in each translation unit, and the linker keeps one. Typical sources are two copies of a library with different build flags, a header whose layout depends on a macro that differs between targets, and a static library linked into two shared objects that both export it.
Catch them early. -Wodr under LTO finds class and inline-function mismatches; ASan's detect_odr_violation covers instrumented globals only:
g++ -flto -Wodr ... # GCC reports type mismatches across units under LTO
ASAN_OPTIONS=detect_odr_violation=2 ./app # needs an ASan build; catches duplicate globals across modules
Also link shared libraries with -Wl,--no-undefined so a missing symbol fails the build instead of the customer's start-up, and hide what you do not export with -fvisibility=hidden so a private helper cannot interpose another library's symbol of the same name.
6. Verify on a clean target
The only convincing test is a container with nothing but the target OS.
docker run --rm -v "$PWD/dist:/opt/app:ro" debian:11 /opt/app/bin/app --version
docker run --rm -v "$PWD/dist:/opt/app:ro" debian:11 sh -c 'ldd -r /opt/app/bin/app'
Use the oldest image you claim to support, and do not install development packages first: a missing library you added to get it running is a missing dependency you have just hidden. Record the base image, the compiler version and the highest GLIBC and GLIBCXX in the build output so the next release can compare.
references/diagnostic-commands.md holds a decision table from message to command, the quick checks for symbol versions across a whole install tree, and a build-container recipe. Read it when the failure is not an obvious missing library.
Anti-patterns
Upgrading the target. It cures one customer and leaves every other old machine broken. Lower the build baseline instead.
Reordering until it links. Shuffling libraries without understanding archive search order produces a link line that works on one toolchain and fails on the next. Use a group deliberately, and say why.
Setting LD_LIBRARY_PATH in the launcher. It hides a wrong RUNPATH on your machine and leaks into child processes. Fix the binary.
Adding -fcommon or --allow-multiple-definition. It silences the error and keeps both definitions, so which one runs depends on link order.
Testing on the build machine. It always has the library you forgot to ship. Test in a clean container.
Hand back
State the side that failed, the first symbol or version and its source, the evidence (the nm, objdump or LD_DEBUG line), the change made, and the result of running the artefact in the clean container of the oldest supported distribution.