Generators: Ninja, Makefiles & Multi-Config
A generator decides which native build files CMake writes. You pick it with -G the first time you configure a build directory, and it can't be changed afterwards.
The list
cmake --help ends with the generators available on the current platform; the default is marked with *:
cmake --help | sed -n '/^The following generators/,/^ Ninja Multi-Config/p'The following generators are available on this platform (* marks default):
Green Hills MULTI = Generates Green Hills MULTI files
(experimental, work-in-progress).
* Unix Makefiles = Generates standard UNIX makefiles.
Ninja = Generates build.ninja files.
Ninja Multi-Config = Generates build-<Config>.ninja files.
On Windows the list also contains the Visual Studio generators (Visual Studio 17 2022), and on macOS Xcode. The ones that matter in practice:
| Generator | Writes | Single or multi-config | Typical use |
|---|---|---|---|
Ninja |
build.ninja |
single | the default choice on every OS: fast, parallel by default, accurate rebuilds |
Ninja Multi-Config |
build-<Config>.ninja |
multi | Debug and Release in one build directory |
Unix Makefiles |
Makefile tree |
single | when Ninja isn't installed |
Visual Studio 17 2022 |
.sln + .vcxproj |
multi | Windows developers working in Visual Studio |
Xcode |
.xcodeproj |
multi | Apple platforms, code signing |
Tip: Set the environment variable
CMAKE_GENERATOR=Ninjaonce in your shell profile or CI, and everycmake -S -Buses it without-G.
The same project, two generators
The CMake code doesn't change; only the build output does:
mkdir -p gen && cd gen
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Gen LANGUAGES CXX)
add_executable(app main.cpp)
CM
echo 'int main() { return 0; }' > main.cpp
cmake -S . -B build-make -G "Unix Makefiles" --log-level=NOTICE > /dev/null
cmake --build build-make
cmake -S . -B build-ninja -G Ninja --log-level=NOTICE > /dev/null
cmake --build build-ninja
ls build-make | head -n 4
ls build-ninja | grep ninja[ 50%] Building CXX object CMakeFiles/app.dir/main.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
[1/2] Building CXX object CMakeFiles/app.dir/main.cpp.o
[2/2] Linking CXX executable app
CMakeCache.txt
CMakeFiles
Makefile
app
build.ninja
Ninja's single-line progress is easier to read in logs, it starts faster on large projects (it reads one file instead of a recursive Makefile tree) and it tracks command-line changes, so changing a flag rebuilds exactly what is affected.
A generator is fixed per build directory
Trying to reuse a directory with a different generator fails, because the cache remembers the first one:
mkdir -p gen && cd gen
printf 'cmake_minimum_required(VERSION 3.25)\nproject(Gen LANGUAGES NONE)\n' > CMakeLists.txt
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake -S . -B build -G "Unix Makefiles"
echo "exit code: $?"CMake Error: Error: generator : Unix Makefiles
Does not match the generator used previously: Ninja
Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.
exit code: 1
Use a new build directory (or --fresh) to switch.
Single-config vs multi-config
With a single-config generator (Ninja, Makefiles) you choose the build type when you configure (-DCMAKE_BUILD_TYPE=Release), and one build directory holds one configuration. With a multi-config generator (Ninja Multi-Config, Visual Studio, Xcode) the configuration is chosen when you build (--config Release), and one build directory holds all of them side by side:
mkdir -p multi && cd multi
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Multi LANGUAGES CXX)
add_executable(app main.cpp)
target_compile_definitions(app PRIVATE CONFIG_NAME="lt;CONFIG>")
CM
cat > main.cpp <<'SRC'
#include <cstdio>
int main() { std::printf("built as %s\n", CONFIG_NAME); }
SRC
cmake -S . -B build -G "Ninja Multi-Config" --log-level=NOTICE > /dev/null
ls build/*.ninja
cmake --build build --config Debug
cmake --build build --config Release
./build/Debug/app
./build/Release/appbuild/build-Debug.ninja
build/build-RelWithDebInfo.ninja
build/build-Release.ninja
build/build.ninja
[1/2] Building CXX object CMakeFiles/app.dir/Debug/main.cpp.o
[2/2] Linking CXX executable Debug/app
[1/2] Building CXX object CMakeFiles/app.dir/Release/main.cpp.o
[2/2] Linking CXX executable Release/app
built as Debug
built as Release
lt;CONFIG> is a generator expression: it is evaluated per configuration when the build files are generated, which is why the same line produces "Debug" in one build file and "Release" in the other. Multi-config builds are exactly why you should use generator expressions rather than if(CMAKE_BUILD_TYPE STREQUAL …): with a multi-config generator, CMAKE_BUILD_TYPE is empty at configure time. See Generator Expressions.
CMAKE_CONFIGURATION_TYPES lists the configurations a multi-config generator creates (Debug;Release;RelWithDebInfo by default for Ninja Multi-Config), and CMAKE_DEFAULT_BUILD_TYPE picks the one cmake --build uses without --config.
Test yourself
With the Ninja Multi-Config generator, when do you choose Debug or Release?
- At configure time with -DCMAKE_BUILD_TYPE
- At build time with
cmake --build build --config Release - In CMakeLists.txt only
- You can't; it's always Debug
You configured build/ with Ninja and now run cmake -S . -B build -G "Unix Makefiles". What happens?
- CMake switches generators
- An error: the generator does not match the one used previously
- Both generators are used
- The cache is silently deleted
Why is if(CMAKE_BUILD_TYPE STREQUAL "Debug") unreliable?
- CMAKE_BUILD_TYPE is read-only
- With multi-config generators it is empty at configure time; the configuration is chosen at build time
- It is case-insensitive
- It only works on Windows