Qt Creator 14-CMake更新

Qt Creator 14 - CMake Update

Qt Creator 14-CMake更新

July 15, 2024 by Cristian Adam | Comments

​2024年7月15日:克里斯蒂安·亚当|评论

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

以下是Qt Creator 14中CMake的新功能和修复:

Per Project CMake Settings

根据项目CMake设置

Qt Creator 14 allows specifying the CMake settings per project.

Qt Creator 14允许为每个项目指定CMake设置。

This can be done in the GUI as seen below:

这可以在GUI中完成,如下所示:

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

它们可以通过vendor字段在CMakePrestes.json文件中指定:

{
  "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:

​如果项目不使用CMakePrestes.json,则可以使用CMakeLists.txt.shared文件:

<?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

CMake预设的调试器注册

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

​CMake预设规范没有提到任何关于调试器条目的内容。

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

Qt Creator 14允许通过供应商字段在配置预设中指定调试器:

   "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.

如果未指定Id,则将自动生成GUID。

The CMakePresets.json used above:

CMakePresets.json使用如下:

{
  "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.

为了找到调试器的正确值,您可以查看%appdata%\QtProject\qtcreator\debuggers.xml文件。这是Qt Creator存储调试器设置信息的地方。

The EngineType can have the following values:

EngineType可以具有以下值:

  • 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.

​此功能由Ralf Habacker贡献,并由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.

​下面,我将展示如何使用LLVM MinGW 17.0.6工具链从Linux主机对Windows x64进行交叉编译,我在$HOME/LLVM-MinGW下本地提取了该工具链。

The CMakePresets.json looks like this:

CMakePresets.json看起来像这样:

{
  "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.

​llvm-mingw.make工具链文件是从包装器中提取的简化版本:从上游LLVM-MinGW添加cmake工具链文件。

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

CMakeLists.txt附带了一个解决方法,可以让C++可执行文件访问LLVM MinGW C++运行时:

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:

最后,一个C++示例,其中包含一些Windows特定的位:

#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";
}

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.

​请注意,我还没有注册调试器,因为LLVM MinGW的LLDB调试器需要一些补丁,如下面的博客文章“使用LLDB和VSCode调试Wine”所示。

File operations on globbed CMake projects

全局CMake项目上的文件操作

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 patterns.

Qt Creator 14将不再为使用文件(GLOB|_RECOURSE)通过使用*.cpp*.h globbing模式快速创建可执行文件或库的CMake项目修改CMakeLists.txt项目文件。

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值