CodeBlanks Logo Icon CodeBlanks

Ninja and Make

Learn Ninja and Make generators.

What are Ninja and Make?

CMake chooses a generator: the two most common are Make and Ninja. Make is universal and found on every Unix; Ninja offers faster incremental builds especially for large projects.

You pick the generator with the -G flag: cmake -G "Ninja" . or cmake -G "Unix Makefiles" .. To build the generated files you run make for Make or ninja for Ninja. Ninja does parallel builds by default; Make needs explicit parallelism like make -j4.

CMAKE_BUILD_TYPE

The CMAKE_BUILD_TYPE variable sets the build type: Debug (debug symbols, no optimization), Release (full optimization), RelWithDebInfo (optimization + symbols). It is set from the command line as cmake -DCMAKE_BUILD_TYPE=Release ..

Example: cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release . produces Ninja files for a release build. Running ninja then compiles the program optimized. Inside CMakeLists.txt you can write if(CMAKE_BUILD_TYPE STREQUAL "Debug") to add type-specific commands. This lets one build file serve both development and production builds.

Practice Write a release build configuration

Write a CMakeLists.txt. Add cmake_minimum_required(VERSION 3.20) and project(MyApp). Define add_executable(myapp main.cpp). Add a comment # cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release . at the end.

Starter code

main.cpp
# CMakeLists.txt
# Add cmake commands and a Ninja comment

Sample solution

main.cpp
cmake_minimum_required(VERSION 3.20)
project(MyApp)
add_executable(myapp main.cpp)
# cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release .

Expected output

Build ready

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the generator name

Question
cmake -G "____" .
Answer
cmake -G "Ninja" .

Explanation: cmake -G "Ninja" produces Ninja build files.

2 Match each piece to its role

Term Meaning
cmake -G "Ninja" Generates Ninja
cmake -G "Unix Makefiles" Generates Makefiles
ninja Builds with Ninja
make -j4 Parallel Make build

3 Why prefer Ninja?

  • It produces smaller files
  • It needs less code
  • It does faster incremental builds Correct answer
  • It is the default everywhere

Explanation: Ninja speeds up incremental builds, especially for large projects.

All lessons in Advanced