CMake Build Flow
The CMake build flow has several main steps, from creating the build directory to compiling, then cleaning and reconfiguring.
The whole process revolves around the CMakeLists.txt configuration file; CMake reads it and generates build files suitable for the current platform.
Build flow overview
A complete CMake build flow contains the following five key steps, each with its own purpose and command.
| Step | Operation | Command | Description |
|---|---|---|---|
| 1 | Create the build directory | mkdir build && cd build |
Use an out-of-source build to keep the source tree clean |
| 2 | Generate build files | cmake .. |
Read CMakeLists.txt and generate a Makefile, Ninja files, a .sln solution and so on |
| 3 | Compile and build | cmake --build . |
Perform the actual compilation and linking |
| 4 | Clean up build files | cmake --build . --target clean |
Delete intermediate and target files produced by compilation |
| 5 | Reconfigure and rebuild | cmake .. && cmake --build . |
Regenerate and compile after CMakeLists.txt changes |

Build flow explained
The following describes each step in detail with common options.
Create the build directory
CMake recommends out-of-source builds, that is, placing build files in a separate directory outside the source tree.
This keeps the source tree clean and also lets one source tree be configured in several different ways (for example Debug and Release side by side).
Always use out-of-source builds. Running cmake directly in the source directory pollutes the project structure, makes intermediate files hard to clean, and makes switching build types inconvenient.

Create the build directory:
# create build directory in project root
mkdir build
Enter the build directory:
# Switch to the build directory, all subsequent cmake commands will be executed here
cd build
Generate build files
Run CMake in the build directory, reading CMakeLists.txt from the source directory and generating build system files for the current platform.
CMake supports several build system generators, such as Unix Makefiles, Ninja, Visual Studio and Xcode.
Basic usage: generate the default build files:
# .. points to the source directory containing CMakeLists.txt
cmake ..
Specify a generator:
Use the -G option to choose the type of build system to generate.
# Using Ninja as the build system (faster than Make)
cmake -G "Ninja" ..
Specify the build type:
Use the -DCMAKE_BUILD_TYPE option to set the optimization level and debug information.
# Four common build types: Debug / Release / RelWithDebInfo / MinSizeRel
cmake -DCMAKE_BUILD_TYPE=Release ..
Check the configuration results:
CMake prints detailed configuration information, including detected compilers, libraries found and defined options.
If there are no errors, the build system files are generated in the current build directory.
If an error occurs during configuration (such as a specified library not being found), CMake stops and prints an error message. After fixing it, rerun the cmake command to continue.
Compile and build
Once the build files are generated, the actual compilation and linking can be performed.
Different build systems use different compile commands; you can also use the generic build command provided by CMake.
Recommended approach: use the generic CMake build command:
# This command will correctly compile regardless of the underlying build system
cmake --build .
Build a specific target:
# Only compile specified targets (e.g., an executable or library)
cmake --build . --target MyExecutable
With the Makefile build system:
# Compile all targets
make
# Use multi-core parallel compilation (-j specifies the number of parallel tasks)
make -j$(nproc)
# Compile specific target
make MyExecutable
With the Ninja build system:
# Ninja automatic parallel compilation, no need to manually specify -j
ninja
# Compile specific target
ninja MyExecutable
With Visual Studio:
If Visual Studio project files were generated, open the .sln solution file and choose Build Solution in the IDE.
# Can also use MSBuild command-line tool to compile
msbuild MyProject.sln /p:Configuration=Release
Clean up build files
Intermediate files (.o, .obj and so on) and target files produced during the build can be removed by cleaning, freeing disk space.
Use the generic CMake clean command:
cmake --build . --target clean
With Makefile:
make clean
With Ninja:
ninja clean
Manual cleanup:
# Delete all files in the build directory, keeping the source code directory unchanged
rm -rf build/*
The simplest and most thorough cleanup is to delete the whole build directory, recreate it and run cmake again. This is more reliable than running clean alone, because the clean target may not be defined correctly.
Reconfigure and rebuild
After you modify CMakeLists.txt or change project settings, you need to rerun CMake configuration and rebuild.
Reconfigure:
# CMake automatically detects changed files and only regenerates affected parts
cmake ..
Rebuild:
# The build system automatically detects which source files have changed and only recompiles the modified parts
cmake --build .
In most cases, after modifying CMakeLists.txt you can simply run cmake --build .; the build system detects the CMakeLists.txt change and reruns the cmake configure step automatically.
Build system comparison
CMake can generate files for many build systems; below is a comparison of the three most commonly used ones.
| Build system | Applicable platform | Parallel compilation | Feature |
|---|---|---|---|
| Unix Makefiles | Linux / macOS | Must be specified manually -j |
Widest compatibility; available by default on almost all Unix systems |
| Ninja | Cross-platform | Automatic parallelism | Faster, especially good for incremental builds and large projects |
| Visual Studio | Windows | Managed automatically by the IDE | Deeply integrated with the VS IDE, with graphical debugging and configuration |
Notes
Always use out-of-source builds.Running cmake in the source directory generates many intermediate files that pollute version control. Adding the build directory to .gitignore is best practice.
Prefer the cmake --build . command.This command is a cross-platform generic build entry point that works whether the underlying system is Make, Ninja or MSBuild. It is especially recommended in scripts and CI environments.
The build type affects performance and the debugging experience.Debug mode includes debug symbols and no optimization, suitable for development; Release mode enables optimization and strips debug information, suitable for production deployment. Do not test performance in Debug mode, and do not debug code in Release mode.
When you hit strange build errors, delete the build directory and start over.CMake's cache sometimes keeps stale configuration information and causes hard-to-diagnose errors. Deleting the whole build directory and rerunning cmake is the fastest way to troubleshoot.