The Toolchain: Compiler, Linker, Build Systems
কম্পাইলার, লিংকার ও CMake
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.
g++ command এই চারটিকেই একসাথে চালায়, কিন্তু প্রতিটি স্তর জানা থাকলে error বুঝতে অনেক সহজ হয়।
2. Installing a C++ Compiler
| OS | Recommended | Install command |
|---|---|---|
| Linux (Ubuntu) | g++ | sudo apt install g++ cmake |
| macOS | clang++ | xcode-select --install |
| Windows | MSYS2 + g++ | Install MSYS2, then pacman -S mingw-w64-x86_64-gcc |
g++ --version in a terminal. You should see GCC 13+ for full C++23 support.
3. Compiling From the Command Line
# 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:
| Flag | Meaning |
|---|---|
-std=c++20 | Use the C++20 standard. |
-Wall -Wextra | Enable most warnings — always use. |
-O2 / -O3 | Optimize for release builds. |
-g | Include debug symbols (for gdb). |
-fsanitize=address | Catch 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).
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:
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:
#include <iostream>
int main() {
std::cout << "C++ standard: " << __cplusplus << "\n";
std::cout << "GCC version: " << __GNUC__ << "." << __GNUC_MINOR__ << "\n";
return 0;
}
6. Practice Problems
-
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. -
What does the
-Wallflag do? Why use it?✨ Show Answer
Enables "all" common warnings (though not literally all — use
-Wall -Wextrafor more). Warnings catch real bugs early. Always compile with warnings turned on. -
Write a CMakeLists.txt for a project with files main.cpp, math.cpp, math.h, requiring C++20.
✨ Show Answer
CMakeLists.txtcmake_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) -
What is the difference between
-O0and-O3?✨ Show Answer
-O0— no optimization, fast compile, easy debug.-O3— aggressive optimization, slow compile, fast runtime, harder debug. Use-O0 -gfor development and-O2or-O3for 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.