导入的目标
每个被加载的 Qt 模块都会定义一个 CMake 库目标。目标名称以Qt6:: 开头,后跟模块名称。例如:Qt6::Core 、Qt6::Gui 。将库目标的名称传递给target_link_libraries 以使用相应的库。
注意:这些 目标也可使用Qt:: 前缀:Qt::Core 、Qt::Gui 等。另请参阅Qt 5 与 Qt 6 的兼容性。
导入的目标将采用与配置 Qt 时相同的配置创建。即:
- 如果 Qt 是通过
-debug选项配置的,则会生成一个具有 DEBUG 配置的导入目标。 - 如果 Qt 是使用
-release选项配置的,则会创建一个具有 RELEASE 配置的导入目标。 - 如果 Qt 是使用 `
-debug-and-release` 开关配置的,则会创建同时具有 RELEASE 和 DEBUG 配置的导入目标。
如果您的项目具有自定义的CMake 构建配置,则必须将自定义配置映射到 Qt 的调试或发布配置之一。
find_package(Qt6 REQUIRED COMPONENTS Core)
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_RELEASE} -fprofile-arcs -ftest-coverage")
# set up a mapping so that the Release configuration for the Qt imported target is
# used in the COVERAGE CMake configuration.
set_target_properties(Qt6::Core PROPERTIES MAP_IMPORTED_CONFIG_COVERAGE "RELEASE")© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.