Table of Contents

CMake Generators

A generator is the backend build system CMake produces. CMake reads CMakeLists.txt and generates build files for your chosen generator: Makefiles, Ninja, Visual Studio, Xcode, etc. Use -G to select:

cmake -G "Unix Makefiles" ..
cmake -G "Ninja" ..
cmake -G "Visual Studio 16 2019" ..

List available generators:

cmake --help       # shows all available generators

Common generators:

Single-config generators (one per build tree):

mkdir build-debug && cd build-debug
cmake -DCMAKE_BUILD_TYPE=Debug ..
 
mkdir build-release && cd build-release
cmake -DCMAKE_BUILD_TYPE=Release ..

Multi-config generators (all in one tree):

mkdir build && cd build
cmake -G "Ninja Multi-Config" ..
cmake --build . --config Debug
cmake --build . --config Release

Switching generators:

CMake caches the generator choice. To switch, delete CMakeCache.txt and CMakeFiles/:

rm -rf CMakeCache.txt CMakeFiles/
cmake -G "Ninja" ..

Or use a separate build directory for each generator.

Building with a specific generator:

cmake --build . -j 4        # use generator's parallel build
cmake --build . --verbose   # show compiler commands
cmake --build . --target myapp  # build specific target

Ninja is popular for speed (supports parallel builds by default). Visual Studio generators integrate with the IDE. Unix Makefiles are traditional and portable.

Choose a generator based on your platform and preferences. Most projects default to Unix Makefiles on Linux/macOS, Visual Studio on Windows. Ninja is preferred by many developers for faster iteration.