Qt Creator 14 - CMake Update

Here are the new CMake features and fixes in Qt Creator 14:

Per Project CMake Settings

Qt Creator 14 allows specifying the CMake settings per project.

This can be done in the GUI as seen below:

qtcreator14-cmake-project-settings

They can be specified in a CMakePresets.json file via the vendor field:

{
  "version": 4,
  "configurePresets": [
    {
      "name": "default",
      "displayName": "Default Config",
      "description": "Default build using Ninja generator",
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/build/default"
    }
  ],
  "vendor": {
    "qt.io/QtCreator/1.0": {
       "AskBeforePresetsReload": false,
       "AskReConfigureInitialParams": false,
       "AutorunCMake": false,
       "PackageManagerAutoSetup": false,
       "ShowAdvancedOptionsByDefault": true,
       "ShowSourceSubFolders": false,
       "UseJunctionsForSourceAndBuildDirectories": true
    }
  }
}

If a project doesn’t use CMakePresets.json, then a CMakeLists.txt.shared file can be used instead:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<qtcreator>
  <data>
  <variable>ProjectExplorer.Project.PluginSettings</variable>
   <valuemap type="QVariantMap">
    <valuemap type="QVariantMap" key="CMakeSpecificSettings">
      <value type="bool" key="AskBeforePresetsReload">false</value>
      <value type="bool" key="AskReConfigureInitialParams">false</value>
      <value type="bool" key="AutorunCMake">false</value>
      <value type="bool" key="PackageManagerAutoSetup">false</value>
      <value type="bool" key="ShowAdvancedOptionsByDefault">true</value>
      <value type="bool" key="ShowSourceSubFolders">false</value>
      <value type="bool" key="UseGlobalSettings">false</value>
      <value type="bool" key="UseJunctionsForSourceAndBuildDirectories">true</value>
    </valuemap>
  </valuemap>
 </data>
 <data>
  <variable>Version</variable>
  <value type="int">22</value>
 </data>
</qtcreator>

Debugger registration for CMake Presets

The CMake Presets specification doesn’t mention anything about a Debugger entry.

Qt Creator 14 allows specifying a debugger in a configure preset via de vendor field:

   "vendor": {
     "qt.io/QtCreator/1.0": {
       "debugger": "C:/Qt/Tools/mingw1120_64/bin/gdb.exe"
     }
   }

Or with all the debugger details as:

  "vendor": {
    "qt.io/QtCreator/1.0": {
      "debugger": {
        "Id": "debugger.gdb.11.2.0.win64",
        "DisplayName": "GNU gdb 11.2.0 for MinGW 11.2.0 64-bit",
        "Abis": ["x86-windows-msys-pe-64bit"],
        "Binary": "C:/Qt/Tools/mingw1120_64/bin/gdb.exe",
        "EngineType": 1,
        "Version": "11.2.0"
      }
    }
  }

If the Id is not specified, then a GUID will be automatically generated.

qtcreator14-cmake-presets-debugger

The CMakePresets.json used above:

{
  "version": 4,
  "configurePresets":
      [
        {
          "name": "llvm-mingw",
          "displayName": "Qt LLVM MinGW 6.7.2 x64",
          "generator": "Ninja",
          "binaryDir": "${sourceDir}/build",
          "cacheVariables": {
            "CMAKE_BUILD_TYPE": "Debug",
            "CMAKE_PREFIX_PATH": "C:/Qt/6.7.2/llvm-mingw_64",
            "CMAKE_C_COMPILER": "C:/Qt/Tools/llvm-mingw1706_64/bin/clang.exe",
            "CMAKE_CXX_COMPILER": "C:/Qt/Tools/llvm-mingw1706_64/bin/clang++.exe"
          },
          "vendor":
              {
                "qt.io/QtCreator/1.0": {
                  "debugger": {
                    "DisplayName": "LLDB 17.0.6 Debugger",
                    "Abis": [
                      "x86-windows-msys-pe-64bit"
                    ],
                    "Binary": "C:/Qt/Tools/llvm-mingw1706_64/bin/lldb.exe",
                    "EngineType": 256,
                    "Version": "17.0.6"
                  }
                }
              }
        }
      ]
}

In order to find the right values for the debugger, you can have a look at %appdata%\QtProject\qtcreator\debuggers.xml file. This is where Qt Creator stores the debuggers settings information.

The EngineType can have the following values:

  • 1 for GDB
  • 4 for CDB
  • 8 for PDB
  • 256 for LLDB
  • 512 for GDB DAP
  • 1024 for LLDB DAP
  • 4096 for µVision Debugger

Cross-compiler emulator

This feature was contributed by Ralf Habacker and was tracked by QTCREATORBUG-29880.

Below, I show how you can cross-compile for Windows x64 from a Linux host using the LLVM-MinGW 17.0.6 toolchain, which I extracted locally under $HOME/llvm-mingw.

The CMakePresets.json looks like this:

{
  "version": 4,
  "configurePresets":
      [
        {
          "name": "llvm-mingw",
          "displayName": "LLVM MinGW 17.0.6 x64",
          "generator": "Ninja",
          "binaryDir": "${sourceDir}/build",
          "toolchainFile": "llvm-mingw.cmake",
          "cacheVariables": {
            "CMAKE_BUILD_TYPE": "Debug",
            "CMAKE_CROSSCOMPILING_EMULATOR": "/usr/bin/wine64"
          }
        }
      ]
}

The llvm-mingw.cmake toolchain file is simplified version taken from wrappers: add CMake toolchain files from upstream LLVM-MinGW.

The CMakeLists.txt comes with a workaround for having the LLVM-MinGW C++ Runtime accessible to the C++ executable:

cmake_minimum_required(VERSION 3.16)
project(Hello LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable
(Hello main.cpp)
add_custom_command
( TARGET Hello POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_SYSROOT}/bin/libc++.dll" ${CMAKE_BINARY_DIR} COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_SYSROOT}/bin/libunwind.dll" ${CMAKE_BINARY_DIR} )

And lastly, a C++ example which has some Windows specific bits:

#include <iostream>
#include <windows.h>
#if !defined(_M_X64) #error "not a x86_64 compiler" #endif
int main() { OSVERSIONINFO info{.dwOSVersionInfoSize = sizeof(OSVERSIONINFO)}; ::GetVersionEx(&info); std::cout << "Hello Windows " << info.dwMajorVersion << "." << info.dwMinorVersion << "." << info.dwBuildNumber << " " << info.szCSDVersion << "\n"; }

qtcreator14-cmake-presets-crosscompiling-emulator

Note that I haven’t registered a debugger since the LLDB debugger from LLVM-MinGW would require some patching as seen in the following blog post Debugging Wine with LLDB and VSCode.

File operations on globbed CMake projects

Qt Creator 14 will no longer modify the CMakeLists.txt project file for CMake projects that use file(GLOB|_RECOURSE) to quickly create an executable or library by using *.cpp *.h globbing parterns.

cmake_minimum_required(VERSION 3.18)
project(HelloWidgetsGlob)
set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package
(Qt6 REQUIRED COMPONENTS Widgets)
file
(GLOB_RECURSE sources RELATIVE ${CMAKE_CURRENT_LIST_DIR} CONFIGURE_DEPENDS "*.c" "*.h" "*.cpp" "*.hpp" "*.cxx" "*.hxx")
file
(RELATIVE_PATH buildDir ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_BINARY_DIR}) list(FILTER sources EXCLUDE REGEX "${buildDir}/*")
add_executable
(HelloWidgetsGlob ${sources}) target_link_libraries(HelloWidgetsGlob PRIVATE Qt6::Widgets)

qtcreator14-cmake-file-glob-operations

 


Blog Topics:

Comments