The Toolchain: Compiler, Linker, Build Systems

কম্পাইলার, লিংকার ও CMake

Read: ~30 min Beginner 4 practice problems

1. The Big Picture

Compiling a C++ program is not one step — it's a pipeline of four programs working in sequence: preprocessor → compiler → assembler → linker. Most of the time you call g++ and it runs them all for you. But knowing each stage helps you read errors, optimize builds, and debug linker issues.

C++ কম্পাইলেশন আসলে একটি ধাপ নয়, চারটি প্রোগ্রামের একটি pipeline: preprocessor → compiler → assembler → linker। সাধারণত g++ command এই চারটিকেই একসাথে চালায়, কিন্তু প্রতিটি স্তর জানা থাকলে error বুঝতে অনেক সহজ হয়।
.cpp source+ headers Preprocessor.i file Compiler.s assembly Assembler.o object Linkerexecutable Figure 3.1 — C++ compilation pipeline।

2. Installing a C++ Compiler

OSRecommendedInstall command
Linux (Ubuntu)g++sudo apt install g++ cmake
macOSclang++xcode-select --install
WindowsMSYS2 + g++Install MSYS2, then pacman -S mingw-w64-x86_64-gcc
Verify install Run g++ --version in a terminal. You should see GCC 13+ for full C++23 support.

3. Compiling From the Command Line

terminal
# Simplest: compile and link in one step
g++ hello.cpp -o hello

# With modern standard + warnings + optimizations
g++ -std=c++20 -Wall -Wextra -O2 hello.cpp -o hello

# Run it
./hello

# See each stage:
g++ -E hello.cpp -o hello.i      # preprocess only
g++ -S hello.cpp -o hello.s      # compile to assembly
g++ -c hello.cpp -o hello.o      # assemble to object
g++ hello.o -o hello             # link

Important flags to remember:

FlagMeaning
-std=c++20Use the C++20 standard.
-Wall -WextraEnable most warnings — always use.
-O2 / -O3Optimize for release builds.
-gInclude debug symbols (for gdb).
-fsanitize=addressCatch memory bugs at runtime.

4. CMake — The Standard Build System

For projects with more than one file, use CMake. It generates platform-specific build files (Makefiles on Linux, Visual Studio on Windows, Xcode on macOS).

CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(MyApp LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(myapp main.cpp utils.cpp)
target_compile_options(myapp PRIVATE -Wall -Wextra)

Build with:

Standard CMake build flow
cmake -B build -S .
cmake --build build
./build/myapp

5. Try It Live

The runner below uses the same g++ compiler under the hood. Click Run:

version.cpp
#include <iostream>

int main() {
    std::cout << "C++ standard: " << __cplusplus << "\n";
    std::cout << "GCC version: " << __GNUC__ << "." << __GNUC_MINOR__ << "\n";
    return 0;
}

6. Practice Problems

  1. Name the four stages of the C++ compilation pipeline in order.
    ✨ Show Answer

    1. Preprocessor — handles #include, macros. 2. Compiler — translates to assembly. 3. Assembler — assembly to object code. 4. Linker — combines object files + libraries into an executable.

  2. What does the -Wall flag do? Why use it?
    ✨ Show Answer

    Enables "all" common warnings (though not literally all — use -Wall -Wextra for more). Warnings catch real bugs early. Always compile with warnings turned on.

  3. Write a CMakeLists.txt for a project with files main.cpp, math.cpp, math.h, requiring C++20.
    ✨ Show Answer
    CMakeLists.txt
    cmake_minimum_required(VERSION 3.20)
    project(MathApp LANGUAGES CXX)
    set(CMAKE_CXX_STANDARD 20)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    add_executable(mathapp main.cpp math.cpp)
  4. What is the difference between -O0 and -O3?
    ✨ Show Answer

    -O0 — no optimization, fast compile, easy debug. -O3 — aggressive optimization, slow compile, fast runtime, harder debug. Use -O0 -g for development and -O2 or -O3 for release.

Summary

C++ compilation goes through preprocess → compile → assemble → link. Use g++ -std=c++20 -Wall -Wextra for safe, modern builds. For multi-file projects, use CMake — it is the de-facto standard. Sanitizers (-fsanitize=address) catch real memory bugs and are free to use.

Next Module → Your First C++ Program — Anatomy of main().