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 buildClean start:
rm -rf build
cmake -S . -B build
cmake --build buildConfigure the project and generate build files:
cmake -S . -B buildUse 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=valueExamples:
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cmake -S . -B build -DBUILD_TESTING=ON
cmake -S . -B build -DMY_OPTION=ONSet install prefix:
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/usr/localUse a toolchain file:
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=path/to/toolchain.cmakeConfigure with warnings for unused variables:
cmake -S . -B build --warn-unused-varsOpen interactive cache UI:
ccmake buildOr GUI:
cmake-gui .
cmake-gui buildBuild the default target:
cmake --build buildBuild with parallel jobs:
cmake --build build -j
cmake --build build -j8Verbose build output:
cmake --build build --verbosePass native build-tool arguments after --:
cmake --build build -- -v
cmake --build build -- -k 0For Makefiles:
cmake --build build -- VERBOSE=1Single-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=MinSizeRelThen build normally:
cmake --build buildCommon 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/releaseMulti-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 MinSizeRelFor Ninja Multi-Config:
cmake -S . -B build -G "Ninja Multi-Config"
cmake --build build --config Debug
cmake --build build --config ReleaseBuild a specific target:
cmake --build build --target my_targetExamples:
cmake --build build --target app
cmake --build build --target library_name
cmake --build build --target testsBuild multiple targets:
cmake --build build --target target1 target2Build a target with a specific configuration:
cmake --build build --config Release --target my_targetList available targets:
cmake --build build --target helpFor Ninja directly:
ninja -C build -t targetsClean using CMake:
cmake --build build --target cleanClean and rebuild a target:
cmake --build build --target clean
cmake --build build --target my_targetFully remove generated files:
rm -rf build
cmake -S . -B build
cmake --build buildConfigure with an install prefix:
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/opt/myprojectBuild:
cmake --build buildInstall:
cmake --install buildInstall a specific configuration:
cmake --install build --config ReleaseOverride install prefix at install time:
cmake --install build --prefix /tmp/packageInstall only one component:
cmake --install build --component RuntimeConfigure with testing enabled if the project supports it:
cmake -S . -B build -DBUILD_TESTING=ON
cmake --build buildRun tests:
ctest --test-dir buildRun tests for a specific configuration:
ctest --test-dir build -C ReleaseVerbose test output:
ctest --test-dir build --output-on-failure
ctest --test-dir build -VRun tests matching a name:
ctest --test-dir build -R my_testRun tests in parallel:
ctest --test-dir build -j8List available presets:
cmake --list-presets
cmake --list-presets=allConfigure with a preset:
cmake --preset debugBuild with a preset:
cmake --build --preset debugTest with a preset:
ctest --preset debugCommon preset workflow:
cmake --preset debug
cmake --build --preset debug
ctest --preset debugDebug build with Ninja:
cmake -S . -B build/debug -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake --build build/debug -jRelease build with Ninja:
cmake -S . -B build/release -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build/release -jVisual Studio Release build:
cmake -S . -B build -G "Visual Studio 17 2022"
cmake --build build --config ReleaseBuild one target in Release mode:
cmake --build build --config Release --target my_appInstall Release configuration:
cmake --install build --config Release --prefix /usr/localEnable compile commands database:
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ONUse 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.cmakeShow cached variables:
cmake -L build
cmake -LA build
cmake -LAH buildEdit cache variable by reconfiguring:
cmake -S . -B build -DVAR=new_valueUndefine a cache variable:
cmake -S . -B build -UVARUndefine matching variables:
cmake -S . -B build -U "MYPROJECT_*"Use this for Makefiles or normal Ninja:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build buildUse this for Visual Studio, Xcode, or Ninja Multi-Config:
cmake -S . -B build
cmake --build build --config ReleaseUse this for specific targets:
cmake --build build --target target_nameUse this for installing:
cmake --install build --config Release --prefix /path/to/installBuild type is chosen at configure time for single-config generators.
Configuration is chosen at build time for multi-config generators.