Skip to content
collect2: ld returned 1 exit status

collect2: ld returned 1 exit status

DodaTech 3 min read

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 status

The 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 program

Forgetting 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 library

Step 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 status

Always fix compilation errors first. The linker error is a consequence.

What does 'collect2' mean?
collect2 is an internal GCC tool that orchestrates the final linking step. It collects all the object files and invokes the system linker (ld). When you see collect2: ld returned 1, it means this wrapper received a failure from the linker.
How do I see detailed linker output?
Pass -v (verbose) to the compiler: g++ -v main.cpp -o main. This shows every command the compiler and linker execute. You can also use -Wl,--verbose to pass verbosity flags directly to the linker: g++ main.cpp -o main -Wl,--verbose.
What is a 'multiply defined symbol' and how do I fix it?
A symbol is multiply defined when two object files define the same function or variable. Solutions: move the definition to a single source file (put only declarations in headers), mark functions as inline, use static to give them internal linkage, or wrap in an anonymous namespace.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro