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...").
/usr/bin/ld: app.o: in function `run()':
app.cpp:(.text+0x5f): undefined reference to `zlibVersion'
collect2: error: ld returned 1 exit statusCommon 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
- Find the
/usr/bin/ld:line(s) just above - that is the real failure. - If it is "undefined reference", link the defining library and check link order.
- If it is "cannot find -lXXX", install the
-devpackage or add-L.
Reproduce the link step directly
Re-run the exact link command to iterate without rebuilding objects.
g++ *.o -o app -lz # add the missing library and re-linkHow to prevent it
- Treat collect2 as a pointer to the ld diagnostic above it.
- Manage libraries and link order through CMake imported targets.
- Install
-devlibraries so the linker can find every-l.