A short note on how you can switch Cmake options in QtCreator just by one click.
Who is frequently working on Cmake C++ projects in QtCreator knows the problem. Cmake options (build-in or defined) appear as check boxes or editable lines in ‘Projects’ tab of QtCreator – that’s fine. But if you like to switch some of those options, you will need to push ‘Apply Configuration Changes’ button multiple times and waiting a second for Cmake run. Sometimes a dialog comes up with the list of changed options and buttons ‘Apply’ or ‘Overwrite’. Depending QtCreator version, this options switch may be even unsuccessful. A couple of bugs were reported and fixed about that problem. So, doing that process often is annoying.
But there is a simple way to setup two independent Cmake configurations and switch between them by one click. QtCreator defines so called ‘Kits’. A Kit is a description of a Qt version with the path to it, compiler, etc. If you click on ‘Manage Kits…’ button in ‘Projects’ tab of QtCreator, you will see the available Kits. Some of them might be auto-detected from the system PATH, other you can add manually using ‘Add’ button and selecting a valid path to a Qt installation.
Proceed with the following steps:
- Be sure to have at least 2 Kits defined. Otherwise, install an additional version of Qt from qt.io, or copy an installed one and add a Kit for it using a different name. For each Kit, you can create a separated configuration with different Cmake options for your project.
- Load your project and switch to ‘Projects’ tab. The Kits appear under ‘Build & Run’ section, where the only one Kit is active and selected, other Kits are gray.
- Click on another Kit to activate it and select a different build directory for the project.
- Now you can change Cmake optoins to create the second Cmake configuration different from the first one.Yes, this time, but only once, you will need to go through multiple clicking on ‘Apply Configuration Changes’.
- Clicking back on the first Kit you will turn back to the first Cmake configuration, while the second remains under the second Kit and can be reached by clicking on this Kit.
This simple procedure works, because QtCreator creates separated projects targets for each Kit in CMakeLists.txt.user file.
Example
The sample project on the pictures below has two Cmake options: myoption1 and myoption2. Under the first Kit ‘Desktop’, I set both options to ‘ON’. The second Kit ‘Desktop Qt 5.12.0 GCC 64bit’ has both options ‘OFF’. Selecting a Kit by one click, I will change the corresponding Cmake configuration. The procedure is much more valuable, if a project has many Cmake options.
Save your time and honor it!