Skip to content
Latchkey

ninja: Build a Specific Target

ninja runs the build described by build.ninja, building either the default targets or the ones you name.

CMake and Meson both emit build.ninja, so in CI you usually invoke ninja with a target rather than make. Knowing how to point it at the build dir and name a target keeps pipelines tidy.

What it does

ninja reads build.ninja in the current directory (or the directory given with -C) and brings the named targets up to date. With no target it builds the default targets declared in the manifest. Targets are output paths or named rules like "all", "install", or a binary name.

Common usage

Terminal
ninja                       # build default targets in ./build.ninja
ninja -C build              # build using build/build.ninja
ninja -C build my_app       # build just the my_app target
ninja -C build install      # run the install target

Options

FlagWhat it does
-C <dir>Change to <dir> before reading build.ninja
-f <file>Use a manifest other than build.ninja
<target>...One or more targets to build (default targets if omitted)
-nDry run: show what would build without running it
-k <N>Keep going until N jobs fail (0 means never stop)

In CI

Configure once into a build directory (cmake -G Ninja -B build or meson setup build) then call ninja -C build. Keeping the build dir stable across steps lets ninja do incremental work and lets a restored ccache hit on unchanged sources.

Common errors in CI

"ninja: error: unknown target 'X'" means X is not a declared output or alias; run ninja -C build -t targets to list them. "ninja: error: loading 'build.ninja': No such file or directory" means you did not configure first or are in the wrong directory; add -C build. "ninja: no work to do." is success, not an error.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →