Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying Qt DLLs to executable directory on Windows using CMake

Tags:

windows

cmake

qt5

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.

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.

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)
like image 932
Jehjoa Avatar asked Nov 12 '16 15:11

Jehjoa


1 Answers

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. ;)

like image 134
Jehjoa Avatar answered Sep 20 '22 00:09

Jehjoa