Skip to content
Latchkey

g++ "collect2: error: ld returned 1 exit status"

This line is the summary, not the cause. collect2 is the GCC wrapper that invokes the linker; "ld returned 1 exit status" just means the linker failed. The actual reason - an undefined reference or a missing library - is printed immediately above.

What this error means

The build ends with "collect2: error: ld returned 1 exit status". Just above it sits the real linker diagnostic ("undefined reference to ..." or "cannot find -l...").

ld output
/usr/bin/ld: app.o: in function `run()':
app.cpp:(.text+0x5f): undefined reference to `zlibVersion'
collect2: error: ld returned 1 exit status

Common causes

An underlying linker error

collect2 reports a non-zero exit from ld. The cause is the specific linker error above - most often an undefined reference or a missing -l library.

Reading the wrapper line instead of the diagnostic

The collect2 line carries no detail by itself. Scrolling up to the /usr/bin/ld: line reveals the actual symbol or library problem.

How to fix it

Read the ld error above collect2

  1. Find the /usr/bin/ld: line(s) just above - that is the real failure.
  2. If it is "undefined reference", link the defining library and check link order.
  3. If it is "cannot find -lXXX", install the -dev package or add -L.

Reproduce the link step directly

Re-run the exact link command to iterate without rebuilding objects.

Terminal
g++ *.o -o app -lz   # add the missing library and re-link

How to prevent it

  • Treat collect2 as a pointer to the ld diagnostic above it.
  • Manage libraries and link order through CMake imported targets.
  • Install -dev libraries so the linker can find every -l.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →