Compilation Process

Understanding preprocessing, compilation, and linking

Interview Relevant: Important for understanding build systems

C++ Compilation Process

C++ source code goes through multiple stages before becoming an executable.

Stages of Compilation

  • Preprocessing: Handles #include, #define, macros. Output: .i file
  • Compilation: Converts C++ to assembly. Output: .s file
  • Assembly: Converts assembly to object code. Output: .o file
  • Linking: Combines object files and libraries into executable

Build Systems

  • Make: Classic build automation tool
  • CMake: Cross-platform build system generator
  • Ninja: Fast build system

Code Examples

Compile each stage separately to understand the process.

bash
1# See each stage separately
2g++ -E main.cpp -o main.i    # Preprocessing
3g++ -S main.i -o main.s      # Compilation to assembly
4g++ -c main.s -o main.o      # Assembly to object
5g++ main.o -o main           # Linking
6
7# Or compile everything at once
8g++ -o main main.cpp

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In