CMakeToolchain: Building your project using CMakePresets

In this example we are going to see how to use CMakeToolchain, predefined layouts like cmake_layout and the CMakePresets CMake feature.

Let’s create a basic project based on the template cmake_exe as an example of a C++ project:

$ conan new -d name=foo -d version=1.0 cmake_exe

Generating the toolchain

The recipe from our project declares the generator “CMakeToolchain”.

We can call conan install to install both Release and Debug configurations. Conan will generate a conan_toolchain.cmake at the corresponding generators folder:

$ conan install .
$ conan install . -s build_type=Debug

Building the project using CMakePresets

A CMakeUserPresets.json file is generated in the same folder of your CMakeLists.txt file, so you can use the --preset argument from cmake >= 3.23 or use an IDE that supports it.

The CMakeUserPresets.json is including the CMakePresets.json files located at the corresponding generators folder.

The CMakePresets.json contain information about the conan_toolchain.cmake location and even the binaryDir set with the output directory.

Note

We use CMake presets in this example. This requires CMake >= 3.23 because the “include” from CMakeUserPresets.json to CMakePresets.json is only supported since that version. If you prefer not to use presets you can use something like:

cmake <path> -G <CMake generator> -DCMAKE_TOOLCHAIN_FILE=<path to
conan_toolchain.cmake> -DCMAKE_BUILD_TYPE=Release

Conan will show the exact CMake command everytime you run conan install in case you can’t use the presets feature.

If you are using a multi-configuration generator:

$ cmake --preset conan-default
$ cmake --build --preset conan-debug
$ build\Debug\foo.exe
foo/1.0: Hello World Release!

$ cmake --build --preset conan-release
$ build\Release\foo.exe
foo/1.0: Hello World Release!

If you are using a single-configuration generator:

$ cmake --preset conan-debug
$ cmake --build --preset conan-debug
$ ./build/Debug/foo
foo/1.0: Hello World Debug!


$ cmake --preset conan-release
$ cmake --build --preset conan-release
$ ./build/Release/foo
foo/1.0: Hello World Release!

Note that we didn’t need to create the build/Release or build/Debug folders, as we did in the tutorial. The output directory is declared by the cmake_layout() and automatically managed by the CMake Presets feature.

This behavior is also managed automatically by Conan (with CMake >= 3.15) when you build a package in the Conan cache (with conan create command). The CMake >= 3.23 is not required.

Read More: