Skip to content
Latchkey

cmake Command: Configure C++ Builds in CI

cmake generates native build files (Make/Ninja) and drives the build.

CMake is the de facto build-system generator for C/C++. In CI it runs in two phases: configure (generate build files from CMakeLists.txt) and build (invoke the underlying generator). Presets standardize both across environments.

Common flags

  • -S DIR / -B DIR - source dir / build (binary) dir
  • -G "Ninja" - choose the generator (Ninja, Unix Makefiles, ...)
  • -D CMAKE_BUILD_TYPE=Release - set a cache variable
  • --build DIR - run the build phase in a configured dir
  • --target NAME - build a specific target
  • -j N (with --build) - parallel build jobs
  • --preset NAME - use a configure preset from CMakePresets.json

Example in CI

Configure with Ninja and a Release build type, then build in parallel:

shell
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build -j$(nproc)

Common errors in CI

  • CMake Error: could not find CMAKE_ROOT / CMakeLists.txt - wrong -S dir
  • No CMAKE_CXX_COMPILER could be found - install a C++ compiler on the runner
  • Could NOT find Package (missing: Package_DIR) - dependency not installed/discoverable
  • CMake Error: Generator ... does not match the generator used previously - stale build dir

Key takeaways

  • Configure then build are two separate cmake invocations (-B then --build).
  • Pick the generator with -G; Ninja is fastest on CI.
  • "No CMAKE_CXX_COMPILER" means the runner image lacks a C++ toolchain.

Related guides

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