01 / 20

Compilation vs.
Linker Errors

Learn to interpret and distinguish the two main categories of build errors in C++: errors caught by the compiler and errors caught by the linker.

C++

What Happens When You Build?

When you press "Build" or run g++ main.cpp, your code passes through several stages before becoming an executable. Each stage can produce its own type of error.

Source Code Compiler Object Files Linker Compilation Errors Linker Errors

What Is a Compilation Error?

A compilation error (also called a compiler error) occurs when the compiler cannot translate your source code into machine instructions. The compiler checks each .cpp file independently.

Syntax Errors
Missing semicolons, mismatched braces, misspelled keywords
Type Errors
Assigning a string to an int, wrong argument types in function calls
Undeclared Names
Using a variable or function name the compiler has never seen declared

What Is a Linker Error?

A linker error occurs after all files compile successfully. The linker tries to combine object files into one executable, and fails when it cannot find or reconcile symbol definitions.

Undefined Reference
A function is declared but its definition (body) is missing from all object files
Multiple Definitions
The same function or variable is defined in two or more translation units
Missing Library
You forgot to link against a required library (e.g., -lm for math)

The Core Rule

COMPILER (per-file analysis) Checks: ✓ Syntax valid? ✓ Types match? ✓ Names declared? ✓ Scope rules? Error examples: expected ';' before '}' 'x' was not declared LINKER (cross-file resolution) Checks: ✓ All symbols defined? ✓ No duplicate defs? ✓ Libraries found? ✓ main() exists? Error examples: undefined reference to 'foo' multiple definition of 'bar'

Declaration vs. Definition

Understanding the difference between a declaration and a definition is the key to understanding linker errors. The compiler only needs declarations; the linker needs definitions.

Declaration
Tells the compiler a name exists and its type. No memory is allocated. Can appear many times.
Definition
Provides the actual body or storage. Memory is allocated. Must appear exactly once.
// Declaration (in header) int add(int a, int b); // Definition (in .cpp file) int add(int a, int b) { return a + b; }

Key Distinction: Same Symptom, Different Cause

Both error types can involve the same function name, but the root cause is completely different. Knowing which tool produced the message tells you where to look.

Compilation Error Linker Error
When During compile step After all files compile
Cause Broken syntax or types Missing or duplicate definition
Scope Single .cpp file Across all .cpp files
Typical keyword error: expected, undeclared undefined reference to
Fix involves Editing the flagged line Adding a missing file or library

How to Diagnose Build Errors

1
Read the first error message. Later errors often cascade from the first one. Fix the first problem, then rebuild.
2
Check who is reporting. Does the message say the compiler (file:line) or the linker (undefined reference, ld)?
3
If compiler error: go to the exact file and line number. Check syntax, types, and spelling of names.
4
If linker error: verify that every declared function has a matching definition, and that all .cpp files are included in the build.
5
Rebuild after each fix. One fix often resolves multiple errors at once.

Example 1: Compilation Error

#include <iostream> int main() { int x = 10 std::cout << x << std::endl; return 0; }
main.cpp:4:19: error: expected ';' before 'std' int x = 10 ^ ;

Example 2: Undeclared Identifier

#include <iostream> int main() { std::cout << greet() << std::endl; return 0; }
main.cpp:4:20: error: use of undeclared identifier 'greet' std::cout << greet() << std::endl; ^~~~~

Example 3: Linker Error

// main.cpp #include <iostream> std::string greet(); // declaration int main() { std::cout << greet() << std::endl; return 0; }
ld: undefined reference to `greet()' collect2: error: ld returned 1 exit status

Example 4: Multiple Definition

// helpers.h (included in two .cpp files) int square(int n) { // definition in header! return n * n; }
ld: multiple definition of `square(int)' a.o: first defined here b.o: also defined here

Anatomy of an Error Message

Compiler Error: main.cpp : 4 : 19 : error: expected ';' before 'std' file line col description Linker Error: ld : undefined reference to `greet()' linker (ld) missing symbol

Common Mistakes

✕ Confusing the Two
Seeing "undefined reference" and trying to fix a typo in your code, when the real problem is a missing .cpp file in the build command.
✕ Defining in Headers
Putting function bodies in .h files without inline, causing "multiple definition" linker errors when two files include it.
✕ Ignoring Cascading Errors
Trying to fix error #15 when error #1 (a missing brace) is causing all the rest. Always fix the first error first.

Summary Comparison

Compilation Error Linker Error
Tool g++ (compiler) ld (linker)
Phase Translation (per file) Linking (all files)
Reports File, line, column Symbol name only
Common causes Typos, missing ; , type mismatch Missing definition, missing library
Fix strategy Edit the flagged line Add missing .cpp or -l flag
Needs declaration? Yes, to compile Yes, plus a definition

Question 1 of 5

You see the error message: "undefined reference to `calculateArea()'". What type of error is this?

Question 2 of 5

Which of the following would most likely cause a compilation error rather than a linker error?

Question 3 of 5

A student writes a function declaration void printScore(int s); in a header file, but never writes the function body in any .cpp file. What happens?

Question 4 of 5

You see an error that includes a file name, line number, and column number. This error was most likely produced by the:

Question 5 of 5

A program has 15 error messages. After fixing only the first error (a missing closing brace), the program recompiles with zero errors. What does this demonstrate?