collect2: ld returned 1 exit status
The collect2: ld returned 1 exit status error means the linker failed. It is always accompanied by specific linker errors that explain the actual problem.
What It Means
collect2 is a wrapper that invokes the linker. When the linker returns a non-zero exit status (1), it means the linking step failed. This error is a summary — the real error is the linker diagnostic printed just before it.
Why It Happens
- Undefined references to functions or variables (most common)
- Multiple definitions of the same symbol
- Missing library files (
.a,.so) or incorrect library paths - A previous compilation step failed and the object file is missing
- Incorrect link order causing symbols to be discarded
How to Fix It
Step 1: Read the actual error above
The real error always appears before the ld returned 1 message:
/tmp/ccXxxxx.o: In function `main':
main.cpp:(.text+0x14): undefined reference to `calculateSum(int, int)'
collect2: error: ld returned 1 exit statusThe real error is undefined reference to 'calculateSum(int, int)'.
Step 2: Link all required object files
// math.cpp
int calculateSum(int a, int b) {
return a + b;
}// main.cpp
int calculateSum(int, int);
int main() {
int result = calculateSum(3, 4);
return 0;
}# Compile both to object files
g++ -c math.cpp -o math.o
g++ -c main.cpp -o main.o
# Link both together
g++ main.o math.o -o programForgetting math.o produces a linker error.
Step 3: Link external libraries
// needs the math library for sqrt()
#include <cmath>
#include <iostream>
int main() {
std::cout << sqrt(25.0) << std::endl;
return 0;
}g++ main.cpp -o main -lm # -lm links the math libraryStep 4: Fix multiple definition errors
// utils.h
int doubleValue(int x) {
return x * 2;
}If utils.h is included in multiple .cpp files, the linker sees multiple definitions. Fix by marking functions inline:
// utils.h
inline int doubleValue(int x) {
return x * 2;
}Or move the implementation to a single .cpp file.
Step 5: Check for missing compilation steps
# If helper.cpp fails to compile but you still try to link:
g++ -c helper.cpp -o helper.o # fails!
g++ main.o helper.o -o program # ld returned 1 exit statusAlways fix compilation errors first. The linker error is a consequence.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro