Compilation vs.
Linking
Understand how C++ transforms your source code into a running executable: the two-stage build process every programmer must know.
Why Understanding the Build Process Matters
When you hit "Build" or "Run" in your IDE, a lot happens behind the scenes. Your C++ code doesn't magically become an application: it goes through distinct stages, each with its own job and its own kinds of errors.
The Big Picture: Source to Executable
What is Compilation?
Compilation is the stage where the compiler reads a single .cpp source file and translates it into machine code (an object file, typically .o or .obj). It checks your code for syntax errors, type mismatches, and other language rule violations.
What is Linking?
Linking is the stage where the linker takes all the object files produced by compilation and combines them into a single executable. It resolves references between files: if main.cpp calls a function defined in utils.cpp, the linker connects them.
The Translation Unit
A translation unit (or compilation unit) is the result of preprocessing one .cpp file. It includes the source file itself plus every header file pulled in by #include directives. The compiler only sees one translation unit at a time.
The Core Rule: Declare vs. Define
Declaration vs. Definition in Code
int add(int a, int b); // "this function exists"
#include "utils.h"
int add(int a, int b) {
return a + b; // actual implementation
}
#include "utils.h"
#include <iostream>
int main() {
std::cout << add(3, 4);
return 0;
}
Compiler Errors vs. Linker Errors
| Compiler Error | Linker Error | |
|---|---|---|
| When | During compilation | During linking |
| Scope | One .cpp file at a time | Across all object files |
| Cause | Syntax, type, missing declaration | Missing definition, duplicate symbol |
| Example | undeclared identifier 'x' | undefined reference to 'add' |
| Fix | Fix syntax, add #include | Add missing .cpp, link library |
How to Diagnose Build Errors
Example 1: A Simple Compiler Error
int main() {
std::cout << add(2, 3) // no declaration of add!
return 0;
}
Example 2: A Classic Linker Error
#include "utils.h" // declaration exists
#include <iostream>
int main() {
std::cout << add(2, 3);
return 0;
}
// Forgot to include utils.cpp!
Example 3: Duplicate Symbol Error
int MAX_SIZE = 100; // definition in header!
#include "constants.h" // gets a copy of MAX_SIZE
#include "constants.h" // gets ANOTHER copy of MAX_SIZE
Common Mistakes
Summary: Build Stages at a Glance
| Preprocessing | Compilation | Linking | |
|---|---|---|---|
| Job | Expand macros, includes | Translate to machine code | Combine object files |
| Input | .cpp + .h files | Translation unit | .o files + libraries |
| Output | Expanded source | .o / .obj file | Executable |
| Error type | Missing file, bad macro | Syntax, type mismatch | Undefined ref, dup symbol |
Question 1 of 5
You compile your C++ program and get the error: "undefined reference to 'calculateArea(double)'". At which stage did this error occur?
Question 2 of 5
What is the primary output of the compilation stage for a single .cpp file?
Question 3 of 5
You have main.cpp, which includes a header declaring square(). The function is defined in math.cpp. You run the command: g++ main.cpp -o program. What is the most likely outcome?
Question 4 of 5
Which of the following is a declaration (not a definition)?
Question 5 of 5
You define int MAX = 50; in a header file that is included by three different .cpp files. What linker error will you most likely see?