使用CMake将Qt DLL复制到Windows上的可执行目录 [英] Copying Qt DLLs to executable directory on Windows using CMake

查看:483
本文介绍了使用CMake将Qt DLL复制到Windows上的可执行目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CMake的新手,我很难理解如何使用生成器表达式。我试图使用 add_custom_command 创建一个后生成命令,将Qt DLL复制到可执行目录。

New to CMake, and I'm having a hard time understanding how to use generator expressions. I'm trying to use add_custom_command to create a post-build command to copy Qt DLLs to the executable directory.

Qt5WidgetsConfig.cmake 中,我可以看到它为Qt5 :: Widgets目标创建了不同的属性以引用DLL,具体取决于当前的活动配置。 IMPORTED_LOCATION_DEBUG IMPORTED_LOCATION_RELEASE 。我希望能够在 if() $< CONFIG:Debug> 生成器表达式作为条件

In Qt5WidgetsConfig.cmake I can see it creates different properties for the Qt5::Widgets target to refer to the DLL, depending on the currently active configuration. Either IMPORTED_LOCATION_DEBUG or IMPORTED_LOCATION_RELEASE. I expected to be able to use the $<CONFIG:Debug> generator expression as a condition in an if() but that doesn't work.

我的CMakeLists.txt:

My CMakeLists.txt:

# minimum version required for proper support of C++11 features in Qt
cmake_minimum_required(VERSION 3.1.0)

set(CMAKE_CONFIGURATION_TYPES Debug;Release)

# project name and version
project(TPBMon VERSION 0.0.0.1)

# Qt5 libs
find_package(Qt5Widgets REQUIRED)

# run Qt's MOC when needed
set(CMAKE_AUTOMOC ON)

add_executable(
    tpbmon
    src/main.cpp
    src/mainwindow.hpp
    src/mainwindow.cpp
)
target_link_libraries(tpbmon Qt5::Widgets)
set_target_properties(
    tpbmon
    PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin
)
if(WIN32)
    if($<CONFIG:Debug>)
        get_target_property(WIDGETDLL Qt5::Widgets IMPORTED_LOCATION_DEBUG)
    else($<CONFIG:Debug>)
        get_target_property(WIDGETDLL Qt5::Widgets IMPORTED_LOCATION_RELEASE)
    endif($<CONFIG:Debug>)
    add_custom_command(
        TARGET tpbmon POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy ${WIDGETDLL} $<TARGET_FILE_DIR:tpbmon>
    )
endif(WIN32)


推荐答案

通过修改对

Figured it out myself by modifying the add_custom_command call to

add_custom_command(
    TARGET tpbmon POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE:Qt5::Widgets>
        $<TARGET_FILE_DIR:tpbmon>
)

睡个好觉后,全新的视角令人惊奇。 ;)

It's amazing what a fresh perspective after a good night's sleep can do. ;)

这篇关于使用CMake将Qt DLL复制到Windows上的可执行目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆