cmake(1)
CMake is a cross-platform build system generator that creates platform-specific build files from CMakeLists.txt configuration files.
Synopsis
cmake [<options>] <path-to-source>Description
CMake reads CMakeLists.txt files to generate native build systems (Makefiles, Ninja files, Visual Studio projects, etc.) for your project. It handles dependency management, compiler detection, and platform-specific configuration automatically.
Unlike traditional build systems, CMake separates the configuration step (generating build files) from the actual compilation step, allowing the same CMakeLists.txt to work across Linux, macOS, Windows, and other platforms.
Common options
| Flag | What it does |
|---|---|
-S <path> | Specify the source directory containing CMakeLists.txt (defaults to current directory) |
-B <path> | Specify the build directory where generated files and compiled objects are placed |
-DCMAKE_BUILD_TYPE=<type> | Set build type: Debug, Release, RelWithDebInfo, or MinSizeRel |
-DCMAKE_INSTALL_PREFIX=<path> | Set the installation directory prefix (where 'make install' places files) |
-G <generator> | Specify build system generator (e.g., 'Unix Makefiles', 'Ninja', 'Visual Studio 16') |
-DBUILD_SHARED_LIBS=ON|OFF | Enable or disable building shared libraries |
-DCMAKE_CXX_COMPILER=<path> | Specify the C++ compiler executable to use |
-DCMAKE_C_COMPILER=<path> | Specify the C compiler executable to use |
--build <dir> | Invoke the native build tool on the build directory (alternative to make) |
--install <dir> | Install built artifacts (alternative to make install) |
-Wno-dev | Suppress warnings for developers (useful for warnings in CMakeLists.txt) |
--debug-output | Print extra debug information during configuration |
Examples
Configure project from current directory, place build files in ./build subdirectory
cmake -S . -B buildConfigure for optimized Release build
cmake -S . -B build -DCMAKE_BUILD_TYPE=ReleaseGenerate Ninja build files instead of Makefiles
cmake -S . -B build -G NinjaCompile the project using 4 parallel jobs
cmake --build build --config Release --parallel 4Configure with custom installation prefix to user home directory
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=$HOME/.localInstall built artifacts to the configured prefix
cmake --build build --target installConfigure project to use clang++ compiler
cmake -S . -B build -DCMAKE_CXX_COMPILER=clang++Traditional workflow: create build dir, configure, and compile
cd build && cmake .. && make