使用CMake构建/导入目标

这篇博客介绍了如何在CMake中使用Qt6模块。每个加载的Qt模块都会定义一个对应的库目标,如Qt6::Core、Qt6::Gui,这些目标可以根据配置(如debug或release)进行链接。通过设置target_link_libraries,可以将库目标应用到项目中。此外,还提到了Qt5和Qt6的兼容性,并指出了在自定义CMake配置下如何映射Qt导入目标的配置。

Imported targets

导入目标

Each Qt module that is loaded defines a CMake library target. The target names start with Qt6::, followed by the module name. For example: Qt6::CoreQt6::Gui. Pass the name of the library target to target_link_libraries to use the respective library.

加载的每个Qt模块定义一个CMake库目标。目标名称以Qt6::开头,后跟模块名称。例如:Qt6::Core、Qt6::Gui。将库目标的名称传递给target_link_libraries以使用相应的库。

Note: The targets are also available with a Qt:: prefix: Qt::CoreQt::Gui, and so on. See also Qt 5 and Qt 6 compatibility.

​注意:目标也可以使用Qt::prefix:Qt::Core、Qt::Gui等。另请参见Qt 5和Qt 6兼容性。

Imported targets are created with the same configurations as when Qt was configured. That is:

导入的目标使用与配置Qt时相同的配置创建。也就是说:

  • If Qt was configured with the -debug switch, an imported target with the DEBUG configuration is created.
  • 如果Qt配置了-debug开关,则会创建带有调试配置的导入目标。
  • If Qt was configured with the -release switch, an imported target with the RELEASE configuration is created.
  • 如果Qt配置了-release开关,则会创建具有release配置的导入目标。
  • If Qt was configured with the -debug-and-release switch, then imported targets are created with both RELEASE and DEBUG configurations.
  • 如果Qt配置了-debug-and-release开关,那么将使用release和debug配置创建导入的目标。

If your project has custom CMake build configurations, you have to map your custom configuration to either the debug or the release Qt configuration.

如果您的项目有定制的CMake构建配置,则必须将定制配置映射到debug配置或release配置。

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

CMake Error at build/external/glew/tmp/__glew-mkdirs.cmake:12 (file): file failed to create directory: C:/Program Files/Pangolin because: Permission denied Call Stack (most recent call first): D:/cmake/share/cmake-4.0/Modules/ExternalProject.cmake:1439 (include) D:/cmake/share/cmake-4.0/Modules/ExternalProject.cmake:3011 (_ep_set_directories) external/CMakeLists.txt:10 (ExternalProject_Add) CMake Error at build/external/zlib/tmp/__zlib-mkdirs.cmake:12 (file): file failed to create directory: C:/Program Files/Pangolin because: Permission denied Call Stack (most recent call first): D:/cmake/share/cmake-4.0/Modules/ExternalProject.cmake:1439 (include) D:/cmake/share/cmake-4.0/Modules/ExternalProject.cmake:3011 (_ep_set_directories) external/CMakeLists.txt:50 (ExternalProject_Add) CMake Error at build/external/libpng/tmp/__libpng-mkdirs.cmake:12 (file): file failed to create directory: C:/Program Files/Pangolin because: Permission denied Call Stack (most recent call first): D:/cmake/share/cmake-4.0/Modules/ExternalProject.cmake:1439 (include) D:/cmake/share/cmake-4.0/Modules/ExternalProject.cmake:3011 (_ep_set_directories) external/CMakeLists.txt:74 (ExternalProject_Add) CMake Error at build/external/libjpeg/tmp/__libjpeg-mkdirs.cmake:12 (file): file failed to create directory: C:/Program Files/Pangolin because: Permission denied Call Stack (most recent call first): D:/cmake/share/cmake-4.0/Modules/ExternalProject.cmake:1439 (include) D:/cmake/share/cmake-4.0/Modules/ExternalProject.cmake:3011 (_ep_set_directories) external/CMakeLists.txt:123 (ExternalProject_Add)
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值