cmake flags & options

Configure a project

Generate build files from CMakeLists.txt.

cmake -B build
cmake -S . -B build

-G

Specify the build system generator.

cmake -B build -G Ninja
cmake -B build -G "Unix Makefiles"

-D

Define a CMake variable.

cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake -B build -DCMAKE_INSTALL_PREFIX=/usr/local
cmake -B build -DBUILD_SHARED_LIBS=ON

--build

Build the project using the generated build system.

cmake --build build
cmake --build build -j$(nproc)
cmake --build build --target myapp

--install

Install the built project.

cmake --install build
cmake --install build --prefix /opt/myapp

--preset

Use a configure preset from CMakePresets.json.

cmake --preset release
cmake --build --preset release

-DCMAKE_BUILD_TYPE

Set the build type.

cmake -B build -DCMAKE_BUILD_TYPE=Debug
cmake -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo

-DCMAKE_C_COMPILER / -DCMAKE_CXX_COMPILER

Set the compiler.

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

--clean-first

Clean before building.

cmake --build build --clean-first

-L

List cached variables.

cmake -B build -LH