thecodex.expert · The Codex Family of Knowledge
C++

Build Systems

CMake never compiles a single line of code itself — it generates the actual Makefiles, Ninja files, or Visual Studio project files that a different tool then uses to do the real work.

C++20 / C++23 Generates builds, doesn’t compile itself Last verified:
Canonical Definition

Unlike Java's Maven or Rust's Cargo, C++ has no single official build tool baked into the language ecosystem — CMake has become the de facto cross-platform standard by generating native build files for the platform's actual toolchain (Makefiles on Linux, Visual Studio projects on Windows, Ninja files for speed) from one shared CMakeLists.txt. This two-step process (CMake generates, then the real build tool builds) is what lets the same C++ project build natively across different operating systems and compilers without maintaining separate build configurations for each.

A minimal CMakeLists.txt

cmake_minimum_required states the oldest CMake version this file assumes; add_executable declares the target and which source files build it — the entire configuration for a simple project can be this short.

C++CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(MyApp)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(myapp main.cpp utils.cpp)

The generate-then-build two-step process

cmake -B build generates the platform-native build files inside the build/ directory; cmake --build build then invokes the actual underlying tool (make, ninja, or MSBuild, whichever CMake chose or was told to target) to compile.

C++terminal
$ cmake -B build              // generates native build files into build/
$ cmake --build build           // invokes the underlying tool to actually compile
$ ./build/myapp

Adding a dependency

find_package locates an already-installed library; target_link_libraries connects it to a specific target — this is the standard pattern for depending on any external C++ library through CMake.

C++CMakeLists.txt
find_package(fmt REQUIRED)

add_executable(myapp main.cpp)
target_link_libraries(myapp PRIVATE fmt::fmt)

Sources

1
Kitware. CMake Documentation, cmake.org/cmake/help/latest.