Skip to main content

Qt Creator 21 - CMake Changes

Comments

Here are the new CMake changes in Qt Creator 21:

Editor

Qt Creator 21 comes with a new CMake language parser. It uses qlalr, the same parser generator used by QML. This allows better CMake editing in the IDE.

Indenter / Formatter

This paved the way for a CMake indenter and formatter as well.

Quick Fixes

The CMake editor now supports quick fixes. The following quick fixes are implemented:

  • Creating a new source file if not present
  • Creating a CMakeLists.txt for an add_subdirectory call
  • Installing a Qt component for a find_package(Qt6 COMPONENTS) call

Documentation

Qt Creator 21 parses the reStructuredText (.rst) documentation of CMake functions / macros if provided. This way you can document the CMake API your project is using.

Function Arguments

CMake functions and macros get their arguments detected from the cmake_parse_arguments calls inside them.

The arguments are highlighted with a different color in the editor.

Different Platform Sources

QMake had the ability to display source files for operating systems other than the host operating system.

CMake projects now support this feature too. All the source files that are referenced via add_executable, add_library, or target_sources and are not found in the current configuration will be displayed grayed out in the project tree.

Find Usages / Rename

Find Usages via Ctrl+Shift+U and Rename via Ctrl+Shift+R now work for CMake targets.

Install

For CMake projects, Qt Creator previously had only QtCreatorDeployment.txt as a way to define the files that get deployed to a device.

Now the install information from the CMake File API is used. Note that this will not work with CMake code run at install time via install(CODE) or install(SCRIPT).

Automatic Header Detection

CMake doesn’t require header files to be specified as source files in order to build a project.

Qt Creator, however, needs the header files in order to know where to show them in the project tree.

With the new setting Find header files by asking the compiler (off by default), you can get Qt Creator to issue compiler calls that list the dependencies of a source file, and save them in a database (.qtcreator/header-deps), similar to how build tools like Ninja operate.

The advantage is that you don’t have to specify the header files in CMake; the downside is the overhead of the extra compiler calls.

Vendor Extensions for CMake Presets

runDevice

Let’s take the challenge of cross-compiling, on a Windows host, a Linux application that prints the equivalent of uname -a.

The Linux machine is a virtual machine:

  • Ubuntu 24.04.5 LTS installed via Windows Subsystem for Linux (WSL2)

  • openssh-server is installed on the WSL virtual machine

  • Qt Creator has a Remote Linux device set up for it

The compiler is the official Windows LLVM package installed via winget install llvm.llvm.

In order to get clang to cross-compile for Linux, I created a linux-sysroot from the WSL terminal:

$ mkdir -p /mnt/c/linux-sysroot/usr
$ cp -r /usr/include /mnt/c/linux-sysroot/usr
$ cp -r /usr/lib /mnt/c/linux-sysroot/usr
$ cd /mnt/c/linux-sysroot
$ ln -s usr/lib lib

Then I used the following linux-wsl.cmake toolchain file:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)

set(triple aarch64-linux-gnu)
set(gcc_version 13)

set(CMAKE_C_COMPILER "$ENV{ProgramFiles}/LLVM/bin/clang.exe")
set(CMAKE_C_COMPILER_TARGET ${triple})
set(CMAKE_CXX_COMPILER "$ENV{ProgramFiles}/LLVM/bin/clang++.exe")
set(CMAKE_CXX_COMPILER_TARGET ${triple})

set(CMAKE_SYSROOT c:/linux-sysroot)

set(CMAKE_C_STANDARD_INCLUDE_DIRECTORIES
    ${CMAKE_SYSROOT}/usr/include/${triple}
    ${CMAKE_SYSROOT}/usr/include
)

set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES
    ${CMAKE_SYSROOT}/usr/include/${triple}/c++/${gcc_version}
    ${CMAKE_SYSROOT}/usr/include/c++/${gcc_version}
    ${CMAKE_C_STANDARD_INCLUDE_DIRECTORIES}
)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

And finally, a CMake Preset using the runDevice vendor extension:

{
    "version": 10,
    "cmakeMinimumRequired": {
        "major": 4,
        "minor": 0,
        "patch": 0
    },
    "configurePresets": [
        {
            "name": "crosscompile-linux-wsl",
            "generator": "Ninja",
            "toolchainFile": "linux-wsl.cmake",
            "cacheVariables": {
                "CMAKE_C_COMPILER": "$env{ProgramFiles}/LLVM/bin/clang.exe",
                "CMAKE_CXX_COMPILER": "$env{ProgramFiles}/LLVM/bin/clang++.exe",
                "CMAKE_SYSROOT": "c:/linux-sysroot",
                "CMAKE_INSTALL_PREFIX": "/tmp",
                "CMAKE_BUILD_TYPE": "Debug"
            },
            "vendor": {
                "qt.io/QtCreator/1.0": {
                    "runDevice": {
                        "type": "GenericLinuxOsType",
                        "name": "WSL"
                    }
                }
            }
        }
    ]
}

 

runSettings

The last piece of the CMake Presets puzzle is now in place.

You can create Run Configurations from CMake Presets for:

  • existing targets, with Working directory and Command line arguments
  • custom CMake targets

Below is the Same Game example built with a CMake Preset for Visual C++ Arm64, with a run configuration that disables Qt Creator’s automatic PATH environment modification and sets PATH manually instead.

"vendor": {
    "qt.io/QtCreator/1.0": {
        "runSettings": [
            {
                "target": "samegame",
                "displayName": "samegame (manual PATH)",
                "useLibraryPaths": false,
                "environment": {
                    "PATH": "C:/Qt/6.12.0/msvc2022_arm64/bin;${PATH}"
                },
                "active": true
            }
        ]
    }
}

 

Locator

With the “cm” locator filter you can issue cmake commands with custom parameters like:

  • cm --install . --prefix myinstall
  • cm -LAH
  • cm --trace-expand --trace-redirect=cmake.log .

With the “ct” locator filter you can run ctest in the build directory with custom parameters like:

  • ct -R timeline

This allows you to run the tests that match the regular expression specified after the -R parameter.

Profiler

The CMake profiler in Qt Creator 21 now jumps to the function’s location when you click on it in the profiler timeline!

Offline Help

CMake also ships its documentation as a Qt Help file (doc/cmake/CMake.qch). Starting with CMake 4.5.0-rc1 you will get a Qt Help file with a stylesheet that matches the official CMake documentation. It also contains a dark theme stylesheet, which is automatically selected by Qt Creator or Qt Assistant when the application uses a dark theme.

Comments

Subscribe to our blog

Try Qt 6.12 Now!

Download the latest release here: www.qt.io/download

Qt 6.12 LTS is now available, with new features and improvements for application developers and device creators.

We're Hiring

Check out all our open positions here and follow us on Instagram to see what it's like to be #QtPeople.