Skip to content
Latchkey

Ninja "ninja: error" in CMake Builds

Ninja could not run the build graph. The build.ninja is missing, stale, or references an input that no longer exists - usually after switching CMake generators or reusing a dirty build directory.

What this error means

The build fails with a ninja: error - commonly "loading 'build.ninja': No such file or directory" or "'<file>', needed by '<target>', missing and no known rule to make it".

ninja output
ninja: error: '/src/generated/version.cpp', needed by 'CMakeFiles/app.dir/version.cpp.o', missing and no known rule to make it

Common causes

No build.ninja (CMake not configured with Ninja)

Calling ninja before cmake -G Ninja configured the directory means there is no build.ninja to load.

Stale build dir or missing generated input

A cached build directory from a different generator or commit references inputs that no longer exist, so Ninja cannot satisfy the graph.

How to fix it

Configure with the Ninja generator first

Terminal
cmake -S . -B build -G Ninja
ninja -C build

Wipe a stale build directory

When the graph references files that no longer exist, regenerate from a clean configure.

Terminal
rm -rf build
cmake -S . -B build -G Ninja
ninja -C build

How to prevent it

  • Always cmake -G Ninja before invoking ninja.
  • Do not cache the CMake build directory across generator or major-version changes.
  • Key any build-dir cache on the CMake version and generator.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →