Qt Creator 9-CMake预置

Qt Creator 9 - CMake预置

Qt Creator 9-CMake预置

October 25, 2022 by Cristian Adam | Comments

​2022年10月25日 克里斯蒂安·亚当|评论

CMake introduced with version 3.19 the concept of cmake-presets.

​CMake在3.19版中引入了CMake预设的概念。

The introduction in the CMake documentation reads as:

CMake文档中的介绍如下:

One problem that CMake users often face is sharing settings with other people for common ways to configure a project. This may be done to support CI builds, or for users who frequently use the same build. CMake supports two files, CMakePresets.json and CMakeUserPresets.json, that allow users to specify common configure options and share them with others.

CMake用户经常面临的一个问题是与其他人共享设置,以获得配置项目的常见方式。这可以用于支持CI构建,也可以用于经常使用同一构建的用户。CMake支持两个文件:CMakePresets.json和CMakeUserPresets.json,允许用户指定通用配置选项并与其他人共享。

The CMake presets come with a Version field, which gets incremented after every new feature added.

CMake预置带有一个Version字段,该字段在每添加一个新功能后递增。

Qt Creator supports presets up to version 3 (introduced in CMake 3.21), but the version checking is not enforced. All the fields from version 3 are read and used if present.

Qt Creator支持版本3之前的预设(CMake 3.21中引入),但不强制执行版本检查。如果存在,则读取并使用版本3中的所有字段。

The CMake presets support has been implemented ontop of Qt Creator’s Build import feature.

CMake预设支持已在Qt Creator的Build导入功能上实现。

This means that Qt Creator 9 offers the presets configuration only on the very first CMake project configuration (when no CMakeLists.txt.user file exists, or all the Kits have been disabled in the project).

这意味着Qt Creator 9仅在第一个CMake项目配置上提供预设配置(当不存在CMakeLists.txt.user文件或项目中所有Kit都已禁用时)。

Configure presets

配置预设

They are part of the CMake presets version 1. Below is one example that configures a Qt project with the:

​它们是CMake预设版本1的一部分。下面是一个配置Qt项目的示例:

  • LLVM-MinGW compiler
  • LLVM MinGW编译器
  • build directory – <sourceDir>/build-release
  • 构建目录–<sourceDir>/build-release
  • build type – CMAKE_BUILD_TYPE as Release
  • 构建类型–CMAKE_BUILD_TYPE作为Release
  • generator – MinGW Makefiles
  • 产生器–MinGW Makefiles
  • explicit CMake executable
  • 显式CMake可执行文件
  • path to Qt installation via CMAKE_PREFIX_PATH
  • 通过CMAKE_PREFIX_PATH进行Qt安装的路径
{
  "version": 1,
  "configurePresets": [
    {
      "name": "llvm-mingw",
      "displayName": "LLVM-MinGW 15.0.0",
      "generator": "MinGW Makefiles",
      "binaryDir": "${sourceDir}/build-release",
      "cmakeExecutable": "C:/Tools/cmake-3.24.2-windows-arm64/bin/cmake.exe",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Release",
        "CMAKE_PREFIX_PATH": "C:/Qt/6.4.0/llvm-mingw_arm64"
      },
      "environment": {
        "PATH": "C:/llvm-mingw/bin;$penv{PATH}"
      }
    }
  ]
}

I’ve tested a Qt Widgets example with Qt Creator 9 with a clean configuration:

我已经用Qt Creator 9测试了一个Qt Widgets示例,该示例具有干净的配置:

  • No CMake tool
  • No Kit
  • No Compilers
  • No Qt

As you could see above, Qt Creator has created a Kit named LLVM-MinGW 15.0.0 (CMake preset), configured the compilers, registered the Qt version, the CMake tool, and lastly the cacheVariables were added to the Initial Configuration project settings page.

如上所述,Qt Creator创建了一个名为LLVM MinGW 15.0.0(CMake预设)的工具包,配置了编译器,注册了Qt版本,CMake工具,最后将cacheVariables添加到初始配置项目设置页面。

Then the project was able to be built and run successfully.

然后,项目得以成功构建和运行。

Note that on this Windows 11 Arm64 laptop I have built Qt, Qt Creator natively for Arm64 myself, to simulate the case where Qt SDK cannot be used.

请注意,在这台Windows 11 Arm64笔记本电脑上,我自己为Arm64构建了Qt,Qt Creator,以模拟Qt SDK无法使用的情况。

Compiler detection

编译器检测

In the configure preset above there was no compiler specified via the CMAKE_C_COMPILER and CMAKE_CXX_COMPILER variables in the cacheVariables section.

在上面的配置预设中,没有通过cacheVariables部分中的CMAKE_C_COMPILER和CMAKE_CXX_COMPILE变量指定编译器。

In this case Qt Creator creates a CMake compiler probe project, configures it with the environment specified by the preset, and then extracts the compiler information.

在这种情况下,Qt Creator创建了一个CMake编译器探测项目,使用预设指定的环境对其进行配置,然后提取编译器信息。

Note that on Windows this operation is not insignificant, and adds up with the number of presets defined.

请注意,在Windows上,此操作并非无关紧要,它与定义的预设数量相加。

Build presets

构建预设

They are part of the CMake presets version 2. They allow configuration of the build steps.

​它们是CMake预设版本2的一部分。它们允许配置构建步骤。

Below you have an example that configures the same Qt project:

下面有一个配置相同Qt项目的示例:

  • generator Ninja Multi-Config
  • 生成Ninja多配置
  • defines two build steps Debug and Release
  • 定义了调试和发布两个构建步骤
  • specifies the path to ninja.exe as part of the CMAKE_MAKE_PROGRAM variable
  • 指定ninja.exe的路径作为CMAKE_MAKE_PROGRAM变量的一部分
{
  "version": 2,
  "configurePresets": [
    {
      "name": "ninja-nmc",
      "displayName": "Ninja Multi-Config LLVM-MinGW",
      "generator": "Ninja Multi-Config",
      "binaryDir": "${sourceDir}/build",
      "cacheVariables": {
        "CMAKE_CONFIGURATION_TYPES": "Debug;Release",
        "CMAKE_PREFIX_PATH": "C:/Qt/6.4.0/llvm-mingw_arm64",
        "CMAKE_MAKE_PROGRAM": "C:/Tools/ninja-1.10.2/ninja.exe"
      },
      "environment": {
        "PATH": "C:/llvm-mingw/bin;$penv{PATH}"
      }
    }
  ],
  "buildPresets": [
    {
      "name": "release",
      "displayName": "Ninja Release",
      "configurePreset": "ninja-nmc",
      "configuration": "Release"
    },
    {
      "name": "debug",
      "displayName": "Ninja Debug",
      "configurePreset": "ninja-nmc",
      "configuration": "Debug"
    }
  ]
}

As you could see above, Qt Creator 9 configured the project with the two build steps, and when I enabled the second build step, I could build the two configurations Debug and Release in one go.

如上所述,QtCreator 9用两个构建步骤配置了项目,当我启用第二个构建步骤时,我可以一口气构建调试和发布两个配置。

Visual C++ and the Ninja generator

Visual C++与Ninja生成器

CMake can use the Visual C++ compiler super easy with the Visual Studio 17 2022 generator:

CMake可以通过Visual Studio 17 2022生成器轻松使用Visual C++编译器:

      "generator": "Visual Studio 17 2022",
      "architecture": "ARM64"

But this will generate a MSBuild project.

但这将生成MSBuild项目。

The Visual C++ compiler in order to be used successfuly requires the proper environment to be present when invoked.

为了成功使用Visual C++编译器,需要在调用时提供适当的环境。

This is usually done via the vcvarsall.bat batch file.

这通常通过vcvarsall.bat批处理文件完成。

Since Qt Creator runs the CMake compiler probe with the system environment the Visual C++ compiler environment needs to be explicitly specified.

由于Qt Creator在系统环境中运行CMake编译器探测,因此需要显式指定Visual C++编译器环境。

Below you have such an example preset:

下面是这样一个预设示例:

{
  "version": 3,
  "configurePresets": [
    {
      "name": "visualc-ninja",
      "displayName": "Visual C++ 2022 arm64 Ninja",
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/build-${presetName}",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Release",
        "CMAKE_PREFIX_PATH": "C:/Qt/6.4.0/msvc2019_arm64"
      },
      "environment" : {
        "VCToolsVersion": "14.34.31931",
        "WindowsSDKVersion" : "10.0.22621.0",
        "VCArch": "arm64",
        "VCHostArch": "HostARM64",
        "VCToolsInstallDir": "$env{ProgramFiles}/Microsoft Visual Studio/2022/Preview/VC/Tools/MSVC/$env{VCToolsVersion}",
        "WindowsSdkDir" : "$env{ProgramFiles(x86)}/Windows Kits/10",
        "WindowsSdkIncVerDir": "$env{WindowsSdkDir}/Include/$env{WindowsSDKVersion}",
        "WindowsSdkLibVerDir": "$env{WindowsSdkDir}/Lib/$env{WindowsSDKVersion}",

        "INCLUDE": "$env{VCToolsInstallDir}/ATLMFC/include;$env{VCToolsInstallDir}/include;$env{WindowsSdkIncVerDir}/ucrt;$env{WindowsSdkIncVerDir}/shared;$env{WindowsSdkIncVerDir}/um;$env{WindowsSdkIncVerDir}/winrt;$env{WindowsSdkIncVerDir}/cppwinrt",
        "LIB": "$env{VCToolsInstallDir}/ATLMFC/lib/$env{VCArch};$env{VCToolsInstallDir}/lib/$env{VCArch};$env{WindowsSdkLibVerDir}/ucrt/$env{VCArch};$env{WindowsSdkLibVerDir}/um/$env{VCArch}",
        "PATH": "C:/Tools/ninja-1.10.2/;$env{VCToolsInstallDir}/bin/$env{VCHostArch}/$env{VCArch};$env{WindowsSdkDir}/bin/$env{WindowsSDKVersion}/$env{VCArch};$penv{PATH}"
      }
    }
  ],
  "buildPresets": [
    {
      "name": "visualc-ninja",
      "configurePreset": "visualc-ninja"
    }
  ]
}

And here you can see it in action:

在这里你可以看到它的实际作用:

The above environment variables can be also set in Qt Creator’s Environment -> System settings, and would act as the default compiler on Windows, in the same way as gcc and clang are present on other operating systems.

上述环境变量也可以在Qt Creator的“环境”->“系统”设置中设置,并将作为Windows上的默认编译器,与其他操作系统上的gcc和clang相同。

The above step is equivalent with calling Qt Creator from a vcvarsall.bat configured shell.

上述步骤相当于从vcvarsall.bat配置的外壳调用Qt Creator。

I gathered the version numbers from such a vcvarsall.bat configured shell by running:

我从这样一个vcvarsall.bat配置外壳中收集了版本号,通过运行以下命令配置shell:

$ set | findstr Ver
$ where cl

Conditions

条件

Come with CMake presets version 3Test presets are also part of this version but Qt Creator 9 does not support them.

​附带CMake预设版本3。测试预设也是此版本的一部分,但Qt Creator 9不支持它们。

Below you have an example preset that comes with a Linux and a Windows preset.

下面是Linux和Windows预置附带的示例预置。

{
  "version": 3,
  "configurePresets": [
    {
      "name": "linux",
      "displayName": "Linux GCC",
      "binaryDir": "${sourceDir}/build",
      "cacheVariables": {
        "CMAKE_PREFIX_PATH": "$env{HOME}/Qt/6.4.0/gcc_aarch64"
      },
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Linux"
      }
    },
    {
      "name": "windows",
      "displayName": "Windows MSVC",
      "binaryDir": "${sourceDir}/build",
      "cacheVariables": {
        "CMAKE_PREFIX_PATH": "$env{SYSTEMDRIVE}/Qt/6.4.0/msvc2019_arm64"
      },
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Windows"
      }
    }
  ]
}

On Windows only the Windows preset will be displayed:

在Windows上,仅显示Windows预设:

An on Linux (running with WSLg on WLS2 - Windows Subsystem for Linux) only the Linux preset will be displayed:

在Linux上(在WLS2上运行WSLg-Windows Subsystem for Linux),将仅显示Linux预设:

Note that I didn’t specify a generator field and Qt Creator picked the generator that CMake has selected as default for the platform.

请注意,我没有指定生成器字段,Qt Creator选择了CMake为平台默认选择的生成器。

Debugging

调试

Presets reloading can oly be done in Qt Creator 9 manually, by deleting the build directory and the CMakeLists.txt.user file.

通过删除构建目录和CMakeLists.txt.user文件,可以在Qt Creator 9中手动重新加载预设。

With Qt Creator 9 you can now remove folders in File system view (Alt+Y, Alt+F), which should help with this task.

使用Qt Creator 9,您现在可以在文件系统视图(Alt+Y,Alt+F)中删除文件夹,这将有助于完成此任务。

The CMake compiler probe invocation, and the location of the CMakeCache.txt file can be logged via the environment variable QT_LOGGING_RULES=qtc.cmake.import*=true before starting Qt Creator.

CMake编译器探测调用,以及CMakeCache.txt文件的位置,可以在启动Qt Creator前,通过环境变量QT_LOGGING_RULES=qtc.cmake.import*=true

Conclusion

结论

Qt Creator 9 is the first version if Qt Creator that supports CMake presets, and the implementation can and may contain bugs.

如果Qt Creator支持CMake预设,那么Qt Creator 9是第一个版本,并且实现可以并且可能包含错误。

I haven’t mentioned any Vendor specific settings, that’s because Qt Creator 9 doesn’t use any.

我没有提到任何特定于供应商的设置,这是因为Qt Creator 9不使用任何设置。

Which setting configuration would you want to be able to specify in a CMakePresets.json file?

您希望能够在CMakePresets.json文件中指定哪种设置配置?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值