Skip to content

Build Systems

C++ code needs to be compiled and linked. While you can use the compiler directly, build systems automate this process.

The Compilation Process

  1. Preprocessing: Handles #include, #define, etc.
  2. Compilation: Translates C++ to Assembly/Machine Code (Object files .o).
  3. Linking: Combines object files and libraries into an executable.

Make

The classic build tool. Uses a Makefile.

# Simple Makefile
CXX = g++
CXXFLAGS = -std=c++20 -Wall

all: my_program

my_program: main.o utils.o
    $(CXX) $(CXXFLAGS) -o my_program main.o utils.o

main.o: main.cpp
    $(CXX) $(CXXFLAGS) -c main.cpp

clean:
    rm -f *.o my_program

CMake

The modern standard. It generates build files (like Makefiles) for you. It is cross-platform.

CMakeLists.txt

1
2
3
4
5
6
cmake_minimum_required(VERSION 3.10)
project(MyProject)

set(CMAKE_CXX_STANDARD 20)

add_executable(my_program main.cpp utils.cpp)

Building with CMake

1
2
3
4
mkdir build
cd build
cmake ..
make

Libraries

  • Static Library (.a / .lib): Code is copied into your executable. Larger size, no runtime dependency.
  • Dynamic/Shared Library (.so / .dll): Code is loaded at runtime. Smaller executable, requires library to be present.