Skip to content

Instantly share code, notes, and snippets.

@0xcafed00d
Last active July 8, 2026 16:16
Show Gist options
  • Select an option

  • Save 0xcafed00d/50b487b3713aad636de81a141abe5036 to your computer and use it in GitHub Desktop.

Select an option

Save 0xcafed00d/50b487b3713aad636de81a141abe5036 to your computer and use it in GitHub Desktop.
CMake command line cheatsheet

CMake Command-Line Cheatsheet

Core workflow

cmake -S . -B build
cmake --build build

-S selects the source directory.
-B selects the build directory.

Common out-of-source build:

cmake -S . -B build
cmake --build build

Clean start:

rm -rf build
cmake -S . -B build
cmake --build build

Configure step

Configure the project and generate build files:

cmake -S . -B build

Use a specific generator:

cmake -S . -B build -G "Ninja"
cmake -S . -B build -G "Unix Makefiles"
cmake -S . -B build -G "Visual Studio 17 2022"

Set cache variables:

cmake -S . -B build -DVAR=value

Examples:

cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cmake -S . -B build -DBUILD_TESTING=ON
cmake -S . -B build -DMY_OPTION=ON

Set install prefix:

cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/usr/local

Use a toolchain file:

cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=path/to/toolchain.cmake

Configure with warnings for unused variables:

cmake -S . -B build --warn-unused-vars

Open interactive cache UI:

ccmake build

Or GUI:

cmake-gui .
cmake-gui build

Build step

Build the default target:

cmake --build build

Build with parallel jobs:

cmake --build build -j
cmake --build build -j8

Verbose build output:

cmake --build build --verbose

Pass native build-tool arguments after --:

cmake --build build -- -v
cmake --build build -- -k 0

For Makefiles:

cmake --build build -- VERBOSE=1

Build types: single-config generators

Single-config generators include:

Ninja
Unix Makefiles
NMake Makefiles

For these, choose the build type at configure time:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake -S . -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo
cmake -S . -B build -DCMAKE_BUILD_TYPE=MinSizeRel

Then build normally:

cmake --build build

Common build types:

Debug          No optimization, debug symbols
Release        Optimized
RelWithDebInfo Optimized with debug symbols
MinSizeRel     Optimized for size

Recommended directory layout:

cmake -S . -B build/debug   -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake -S . -B build/release -G Ninja -DCMAKE_BUILD_TYPE=Release

cmake --build build/debug
cmake --build build/release

Configurations: multi-config generators

Multi-config generators include:

Visual Studio
Xcode
Ninja Multi-Config

For these, you usually do not set CMAKE_BUILD_TYPE.

Configure once:

cmake -S . -B build -G "Visual Studio 17 2022"

Build a specific configuration:

cmake --build build --config Debug
cmake --build build --config Release
cmake --build build --config RelWithDebInfo
cmake --build build --config MinSizeRel

For Ninja Multi-Config:

cmake -S . -B build -G "Ninja Multi-Config"
cmake --build build --config Debug
cmake --build build --config Release

Selecting targets

Build a specific target:

cmake --build build --target my_target

Examples:

cmake --build build --target app
cmake --build build --target library_name
cmake --build build --target tests

Build multiple targets:

cmake --build build --target target1 target2

Build a target with a specific configuration:

cmake --build build --config Release --target my_target

List available targets:

cmake --build build --target help

For Ninja directly:

ninja -C build -t targets

Clean builds

Clean using CMake:

cmake --build build --target clean

Clean and rebuild a target:

cmake --build build --target clean
cmake --build build --target my_target

Fully remove generated files:

rm -rf build
cmake -S . -B build
cmake --build build

Installing

Configure with an install prefix:

cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/opt/myproject

Build:

cmake --build build

Install:

cmake --install build

Install a specific configuration:

cmake --install build --config Release

Override install prefix at install time:

cmake --install build --prefix /tmp/package

Install only one component:

cmake --install build --component Runtime

Running tests

Configure with testing enabled if the project supports it:

cmake -S . -B build -DBUILD_TESTING=ON
cmake --build build

Run tests:

ctest --test-dir build

Run tests for a specific configuration:

ctest --test-dir build -C Release

Verbose test output:

ctest --test-dir build --output-on-failure
ctest --test-dir build -V

Run tests matching a name:

ctest --test-dir build -R my_test

Run tests in parallel:

ctest --test-dir build -j8

Presets

List available presets:

cmake --list-presets
cmake --list-presets=all

Configure with a preset:

cmake --preset debug

Build with a preset:

cmake --build --preset debug

Test with a preset:

ctest --preset debug

Common preset workflow:

cmake --preset debug
cmake --build --preset debug
ctest --preset debug

Common examples

Debug build with Ninja:

cmake -S . -B build/debug -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake --build build/debug -j

Release build with Ninja:

cmake -S . -B build/release -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build/release -j

Visual Studio Release build:

cmake -S . -B build -G "Visual Studio 17 2022"
cmake --build build --config Release

Build one target in Release mode:

cmake --build build --config Release --target my_app

Install Release configuration:

cmake --install build --config Release --prefix /usr/local

Enable compile commands database:

cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

Use Clang:

cmake -S . -B build -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++

Use a cross-compilation toolchain:

cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=cmake/toolchains/arm-none-eabi.cmake

Useful cache commands

Show cached variables:

cmake -L build
cmake -LA build
cmake -LAH build

Edit cache variable by reconfiguring:

cmake -S . -B build -DVAR=new_value

Undefine a cache variable:

cmake -S . -B build -UVAR

Undefine matching variables:

cmake -S . -B build -U "MYPROJECT_*"

Rule of thumb

Use this for Makefiles or normal Ninja:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build

Use this for Visual Studio, Xcode, or Ninja Multi-Config:

cmake -S . -B build
cmake --build build --config Release

Use this for specific targets:

cmake --build build --target target_name

Use this for installing:

cmake --install build --config Release --prefix /path/to/install

Key distinction

Build type is chosen at configure time for single-config generators.

Configuration is chosen at build time for multi-config generators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment