Skip to content

/flags/cmake/cmake-g-generator

cmake -G: choose the generator (Ninja, Unix Makefiles, Visual Studio)

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
CMake
command
cmake
flag
-G <generator-name>
default
the CMAKE_GENERATOR environment variable, otherwise a platform default (Unix Makefiles on Linux and macOS, the newest Visual Studio found on Windows)

cmake -G Ninja -S . -B build tells CMake which build system to generate: Ninja files, Makefiles, a Visual Studio solution, an Xcode project. CMake does not compile anything itself; the generator decides which tool cmake --build runs later. Without -G, CMake uses the CMAKE_GENERATOR environment variable or the platform default, which is Unix Makefiles on Linux and macOS and the newest installed Visual Studio on Windows. The choice is stored in the build directory's cache and cannot be changed afterwards for that directory.

Generators side by side

The example uses CMake 4.4.4 and Ninja 1.13.0 from PyPI through uv, with a project that needs no compiler:

bash
cat > CMakeLists.txt <<'EOF'
cmake_minimum_required(VERSION 3.20)
project(demo NONE)
message(STATUS "Generator: ${CMAKE_GENERATOR}")
EOF
cmake() { uv run -q --no-project --python 3.13 --with cmake==4.4.4 --with ninja==1.13.0 cmake "$@"; }
echo "--- -G Ninja";               cmake -S . -B b1 -G Ninja | grep Generator;               ls b1 | grep ninja
echo "--- -G \"Ninja Multi-Config\""; cmake -S . -B b2 -G "Ninja Multi-Config" | grep Generator; ls b2 | grep ninja
echo "--- CMAKE_GENERATOR=Ninja";   CMAKE_GENERATOR=Ninja cmake -S . -B b3 | grep Generator

Output

--- -G Ninja
-- Generator: Ninja
build.ninja
--- -G "Ninja Multi-Config"
-- Generator: Ninja Multi-Config
build-Debug.ninja
build-RelWithDebInfo.ninja
build-Release.ninja
build.ninja
--- CMAKE_GENERATOR=Ninja
-- Generator: Ninja

Ninja writes one build.ninja; Ninja Multi-Config writes one file per configuration, which is why it ignores CMAKE_BUILD_TYPE (see -DCMAKE_BUILD_TYPE). The environment variable has the same effect as -G.

Common generators

-G valueBuilds withNotes
NinjaninjaFast, single-config; needs the ninja executable on PATH
Ninja Multi-ConfigninjaDebug, Release and RelWithDebInfo in one build tree; pick with --config
Unix MakefilesmakeDefault on Linux and macOS; single-config
Visual Studio 17 2022MSBuildMulti-config; choose the platform with -A x64, -A Win32 or -A ARM64
XcodexcodebuildmacOS only; multi-config
MinGW Makefilesmingw32-makeWindows with MinGW, from cmd.exe or PowerShell
MSYS Makefilesmake in MSYS2Windows, from an MSYS2 shell
NMake MakefilesnmakeWindows, from a Visual Studio developer prompt

The name after -G must be written exactly as cmake --help prints it, including spaces and capitals; quote it when it contains spaces.

Errors -G can cause

bash
printf 'cmake_minimum_required(VERSION 3.20)\nproject(demo NONE)\n' > CMakeLists.txt
cmake() { uv run -q --no-project --python 3.13 --with cmake==4.4.4 --with ninja==1.13.0 cmake "$@"; }
echo "--- wrong case";        cmake -S . -B x -G ninja 2>&1 | head -1
echo "--- tool not installed"; cmake -S . -B m -G "Unix Makefiles" 2>&1 | head -1
echo "--- switch generator"
cmake -S . -B build -G Ninja > /dev/null
cmake -S . -B build -G "Ninja Multi-Config" 2>&1 | head -3

Output

--- wrong case
CMake Error: Could not create named generator ninja
--- tool not installed
CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
--- switch generator
CMake Error: Error: generator : Ninja Multi-Config
Does not match the generator used previously: Ninja
Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.
  • Could not create named generator means the name is unknown: generator names are case-sensitive (ninja fails, Ninja works), and some exist only on one platform.
  • unable to find a build program corresponding to "Unix Makefiles" means the generator exists but its tool (make here) is not installed or not on PATH. Install it, or pick a generator whose tool you have.
  • Does not match the generator used previously appears when an existing build directory was configured with another generator. Use a new directory or cmake --fresh, as explained in clean a CMake build.
OptionPurpose
-A platformTarget architecture for Visual Studio (x64, Win32, ARM64)
-T toolsetToolset for Visual Studio and Xcode, such as -T ClangCL or -T v143
-DCMAKE_MAKE_PROGRAM=pathExact path of make or ninja when it is not on PATH
--freshDiscard the cache, allowing a new generator in the same directory (CMake 3.24+)
cmake --build DIRRun the generated build without calling make or ninja by name

Using cmake --build build in scripts and CI keeps them independent of the generator: the same command drives Ninja, make or MSBuild.

FAQ

How do I change the generator of an existing build directory?

You cannot reuse the directory: CMake stops with Does not match the generator used previously. Use a new build directory, delete CMakeCache.txt and CMakeFiles, or run cmake --fresh -G NAME (CMake 3.24 or later), which discards the cache.

How do I list the generators my CMake supports?

Run cmake --help; the Generators section at the end lists every generator available on this platform, with an asterisk next to the default one.

Can I set the generator without typing -G every time?

Yes. Set the CMAKE_GENERATOR environment variable (for example export CMAKE_GENERATOR=Ninja), or put generator in a configure preset in CMakePresets.json. -G on the command line overrides both.

Sources