Using CMakeToolchain with different generators: Ninja example¶
This guide demonstrates how to use CMakeToolchain with predefined generators like Ninja and how to configure it to use different generators.
Note
We assume you have already installed Ninja in your system. In case you do not have Ninja installed in your system, you can use the Ninja Conan package in your profile (default or custom) by adding tool-requires.
Understanding CMake generators¶
The CMake client offers a variety of generators to create build system files.
If you want to use a generator other than the default chosen by CMake, you can configure tools.cmake.cmaketoolchain:generator
.
To see which generators are available on your system, run:
$ cmake --help
You can set this configuration in your profile, directly in the command line, or even in your global configuration.
Using the Ninja generator by default in a profile¶
First, let’s create a profile file name my_custom_profile
,
so we can set the Ninja generator as the default for all Conan packages built with this profile.
$ conan profile detect --name=my_custom_profile
To set the Ninja generator as the default in my_custom_profile
profile,
add the entry [conf]
with the generator value in the file:
[settings]
os=Linux
arch=x86_64
compiler=gcc
compiler.version=13
compiler.libcxx=libstdc++11
compiler.cppstd=20
build_type=Release
[conf]
tools.cmake.cmaketoolchain:generator=Ninja
Now, we will create a basic project based on the cmake_exe
template as an example of a C++ project:
$ conan new cmake_exe -d name=foo -d version=0.1.0
Then, we can build your project using the profile we just created:
$ conan create . -pr=my_custom_profile
This configuration will be passed to the conan_toolchain.cmake
file, generated by CMakeToolchain
, then the Ninja generator will be used.
You should see the following output snippet indicating the Ninja generator is being used:
Profile host:
[settings]
...
[conf]
tools.cmake.cmaketoolchain:generator=Ninja
...
foo/0.1.0: Calling build()
foo/0.1.0: Running CMake.configure()
foo/0.1.0: RUN: cmake -G "Ninja" ...
Note that same configuration can be passed to the default profile, and used for all Conan packages built with that profile.
In case passing the generator configuration by command line, the same will override the profile configuration.