When configuring projects with CMake, you're not limited to the default build backends like Unix Makefile or MSBuild. Another popular option is Ninja, a small, fast build system designed for speed and simplicity. Pairing CMake with Ninja can significantly cut down build times, especially on large projects. This tutorial explains how to use Ninja build system with CMake.
Let's start with a very basic CMakeLists.txt
file:
cmake_minimum_required(VERSION 3.27)
project(myapp)
set(CMAKE_CXX_STANDARD 17)
add_executable(${PROJECT_NAME} main.cpp)
To instruct CMake to use Ninja as the build system, you can specify the generator explicitly when creating the build directory using -G
option:
cmake -S . -B build -G Ninja
Alternatively, you can set the generator to Ninja through the environment variable CMAKE_GENERATOR
:
CMAKE_GENERATOR=Ninja cmake -S . -B build
Once configured, the build directory will contain files specific to Ninja:
| build/
|-- CMakeFiles/
|-- build.ninja
|-- cmake_install.cmake
|-- CMakeCache.txt
The build.ninja
file is the core build script generated by CMake, which Ninja reads to perform fast incremental builds.
Leave a Comment
Cancel reply