CMake Build system generator
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
cmake -S . -B build
cmake --build build
cmake --build build -j
ctest --test-dir build
cmake --install build --prefix <dir>
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake -B build -G Ninja
cmake --preset <name>
Concepts
- CMakeLists.txt project description
- Build tree cache + generated files
- Binaries via Make/Ninja
- Tests, install results, prefix
Configure
cmake -S <src> -B <build>
cmake -B build -G "<generator>"
cmake -B build -D<VAR>=<value>
cmake -B build -DCMAKE_BUILD_TYPE=<type>
cmake -B build --fresh
cmake -B build --toolchain <file>
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
Build
cmake --build <dir>
cmake --build <dir> -j <N>
cmake --build <dir> -t <target>
cmake --build <dir> --clean-first
cmake --build <dir> --config <cfg>
cmake --build <dir> -v
Install and test
cmake --install <dir>
cmake --install <dir> --prefix <dir>
ctest --test-dir <dir>
ctest --test-dir <dir> --output-on-failure
ctest -R <regex>
ctest -E <regex>
ctest -j <N>
ctest --rerun-failed
Presets
cmake --list-presets
cmake --preset <name>
cmake --workflow --preset <name>
Inspect and debug
cmake -L -N <dir>
cmake --trace-expand
cmake --debug-find
cmake --help-command <cmd>
cmake --versionScript and tools
cmake -P <script.cmake>
cmake -E copy <src> <dst>
cmake -E rm -rf <path>
CMakeLists.txt
cmake_minimum_required(VERSION <x.y>)
project(<name> VERSION <x.y> LANGUAGES
CXX)
add_executable(<tgt> <sources>)
add_library(<tgt> STATIC|SHARED
<sources>)
target_link_libraries(<tgt> PRIVATE
<lib>)
target_include_directories(<tgt> PUBLIC
<dir>)
target_compile_features(<tgt> PUBLIC
cxx_std_17)
target_compile_definitions(<tgt> PRIVATE
<NAME>=1)
find_package(<Pkg> REQUIRED)
add_subdirectory(<dir>)
option(<VAR> "<doc>" ON)
set(<VAR> <value>)
enable_testing()
add_test(NAME <t> COMMAND <exe>)
install(TARGETS <tgt> DESTINATION bin)
Recipes
cmake_minimum_required(VERSION 3.20)project(app LANGUAGES CXX)add_executable(app main.cpp)target_compile_features(app PRIVATE cxx_std_17)enable_testing()add_test(NAME smoke COMMAND app)
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Releasecmake --build build -jctest --test-dir build --output-on-failurecmake --install build --prefix ~/.local
find_package(fmt REQUIRED)add_library(core src/core.cpp)target_include_directories(core PUBLIC include)target_link_libraries(core PUBLIC fmt::fmt)add_executable(app src/main.cpp)target_link_libraries(app PRIVATE core)
{ "version": 6, "configurePresets": [{ "name": "dev", "generator": "Ninja", "binaryDir": "${sourceDir}/build", "cacheVariables": {"CMAKE_BUILD_TYPE": "Debug"} }]}



