DevTools

CMake

CMake commands for C/C++ build system generation.

30 commands
Windows MacOS Linux
#build-system #cpp

Project Generation

Configure project into build directory
cmake -S . -B build
Generate Ninja build files
cmake -S . -B build -G Ninja
Generate Makefiles
cmake -S . -B build -G "Unix Makefiles"
Configure Release build
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
Set install prefix
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/usr/local
Set compilers
cmake -S . -B build -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++

Build

Build the project
cmake --build build
Build with 8 parallel jobs
cmake --build build -j 8
Build a specific target
cmake --build build --target mylib
cmake --build build --config Release # Build specific configuration
Clean before building
cmake --build build --clean-first
Show build commands
cmake --build build --verbose

Install

Install built project
cmake --install build
Install to custom prefix
cmake --install build --prefix /opt
Install specific component
cmake --install build --component dev
Strip binaries during install
cmake --install build --strip

Testing

Run all tests
ctest --test-dir build
Run tests in parallel
ctest --test-dir build -j 8
Run tests matching pattern
ctest --test-dir build -R "unit_*"
Show output on test failure
ctest --test-dir build --output-on-failure
Run tests with verbose output
ctest --test-dir build -V
Rerun only failed tests
ctest --test-dir build --rerun-failed

Presets

List available configure presets
cmake --list-presets
Configure using a preset
cmake --preset release
Build using a preset
cmake --build --preset release
Test using a preset
ctest --preset release
Generate compile_commands.json
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

Quick Commands

Configure project into a build directory
cmake -S . -B build
Build project with 8 parallel jobs
cmake --build build -j 8
Run all project tests
ctest --test-dir build