01 / 20

Compilation vs.
Linking

Understand how C++ transforms your source code into a running executable: the two-stage build process every programmer must know.

C++

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

main.cpp utils.cpp Preprocessor #include, #define Preprocessor #include, #define Compiler Syntax check Compiler Syntax check main.o utils.o Libraries std, third-party Linker program.exe

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.

Input
One .cpp file (after preprocessing)
Output
One .o / .obj object file (machine code)
Errors
Syntax errors, type errors, undeclared identifiers

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.

Input
Multiple .o files + libraries
Output
One executable (program.exe, a.out)
Errors
Undefined references, duplicate symbols

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.

main.cpp utils.h <iostream> Preprocess Translation Unit main.cpp + utils.h + <iostream> (all merged into one stream) Macros expanded, comments removed

The Core Rule: Declare vs. Define

1
Declaration tells the compiler that something exists (its name and type). Found in header files (.h / .hpp).
2
Definition provides the actual implementation (the function body, the variable's storage). Found in source files (.cpp).
3
The compiler needs declarations. The linker needs definitions. Mismatch = errors.

Declaration vs. Definition in Code

// utils.h (declaration)
int add(int a, int b);  // "this function exists"
// utils.cpp (definition)
#include "utils.h"

int add(int a, int b) {
    return a + b;  // actual implementation
}
// main.cpp (uses the declaration)
#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

1
Read the error message carefully. It usually says which file and line caused the problem.
2
Identify the stage. Words like "syntax error" or "expected ;" mean compilation. Words like "undefined reference" or "unresolved external" mean linking.
3
For compiler errors: check the .cpp file and its included headers. Look for typos, missing semicolons, wrong types.
4
For linker errors: check that every declared function has a definition somewhere, and that all .cpp files are being compiled and linked together.
5
Check your build command. For example: g++ main.cpp utils.cpp -o program compiles and links both files.

Example 1: A Simple Compiler Error

#include <iostream>

int main() {
    std::cout << add(2, 3) // no declaration of add!
    return 0;
}

Example 2: A Classic Linker Error

// main.cpp
#include "utils.h"  // declaration exists
#include <iostream>

int main() {
    std::cout << add(2, 3);
    return 0;
}
// Build command: g++ main.cpp -o program
// Forgot to include utils.cpp!

Example 3: Duplicate Symbol Error

// constants.h
int MAX_SIZE = 100;  // definition in header!
// main.cpp
#include "constants.h"  // gets a copy of MAX_SIZE
// utils.cpp
#include "constants.h"  // gets ANOTHER copy of MAX_SIZE

Common Mistakes

✕ Forgetting #include
Not including the header with the function declaration causes a compiler error, even if the .cpp with the definition exists.
✕ Not linking all files
Compiling only main.cpp when functions are defined in other .cpp files causes linker errors (undefined reference).
✕ Defining in headers
Putting function or variable definitions (not just declarations) in .h files causes duplicate symbol linker errors.

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?