100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Compilation Process in C++

Follow the full journey of a C++ program from source code to executable, covering preprocessing, compilation, assembly, and linking stages.

Introduction to C++Beginner6 min readJul 7, 2026
Analogies

1. Intro

Unlike interpreted languages, C++ source code must be translated into machine code before it can run. This translation happens through several distinct stages, and understanding them helps you diagnose errors like missing header, undefined reference, or linker error.

🏏

Cricket analogy: Just as raw net-practice footage must go through selection, fitness tests and final XI announcement before a player takes the field, C++ source code passes through several translation stages before it becomes a runnable program, and skipping steps causes errors like a 'linker error'.

2. Syntax

bash
# Typical g++ compilation pipeline for a single file
g++ -E hello.cpp -o hello.i      # 1. Preprocessing
g++ -S hello.i -o hello.s        # 2. Compilation to assembly
g++ -c hello.s -o hello.o        # 3. Assembly to object code
g++ hello.o -o hello             # 4. Linking to executable

# In practice, one command does all four stages:
g++ hello.cpp -o hello
./hello

3. Explanation

The compilation process has four broad stages. First, the preprocessor handles directives beginning with #, such as #include and #define, expanding headers and macros into a single translation unit. Second, the compiler translates this preprocessed source code into low-level assembly code specific to the target CPU architecture, also performing syntax and type checking — this is where most compile-time errors are caught. Third, the assembler converts the assembly code into object code (machine code in binary form, typically a .o or .obj file), which is not yet a runnable program. Finally, the linker combines one or more object files with the required library code (such as the C++ standard library) to resolve function calls like cout and produces a single executable file. If a function is declared but never defined anywhere the linker can find, you get a linker error (undefined reference), which is different from a compiler error.

🏏

Cricket analogy: The preprocessor is like collecting every player's registration form (#include) and abbreviations (#define) into one squad list; the compiler checks each player's eligibility and skill (syntax/type checking) like BCCI selectors; the assembler drafts the actual playing XI as an unsigned team sheet (object code); and the linker is the match referee who confirms every player, including guest imports like an overseas Sunil Narine (library functions), is actually present before play starts — a missing player causes a 'linker error'.

4. Example

cpp
#include <iostream>
using namespace std;

int main() {
    cout << "Compiling C++ step by step!" << endl;
    return 0;
}

5. Output

text
Compiling C++ step by step!

6. Key Takeaways

  • C++ compilation has four stages: preprocessing, compilation, assembly, and linking.
  • The preprocessor expands #include and #define directives before real compilation begins.
  • The compiler checks syntax/types and produces assembly code; the assembler turns it into object code.
  • The linker combines object files and libraries into the final executable, and reports linker errors for unresolved references.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#CompilationProcessInC#Compilation#Process#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep