Table of Contents

CMake Basics

CMake is a build system generator: it reads CMakeLists.txt and generates build files for your platform (Makefiles, Ninja, Visual Studio, etc.). Run cmake to configure, then use the generated build system to compile.

$ mkdir build && cd build
$ cmake ..                      # configure (reads CMakeLists.txt from parent)
$ cmake --build .               # build using generated files
$ cmake --build . -v            # verbose: show compiler commands
$ cmake --build . --target test # run tests
$ cmake --install .             # install (if configured)

The CMakeLists.txt file describes your project: what to build (executables, libraries), where source files are, dependencies, compiler flags, installation rules, etc. CMake handles finding compilers, libraries, and adjusting for your OS.

Key directories:

Use out-of-source builds: create a separate build/ directory for generated files. This keeps sources clean and allows multiple build configurations (debug, release, different compilers) from the same source tree.

mkdir build-debug && cd build-debug && cmake -DCMAKE_BUILD_TYPE=Debug ..
mkdir build-release && cd build-release && cmake -DCMAKE_BUILD_TYPE=Release ..

CMake version: check with cmake --version. Projects often specify minimum version in CMakeLists.txt: cmake_minimum_required(VERSION 3.20).