Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deploy all Qt dependencies when building

I've created a CMakeLists.txt for creating a simple Qt application (actually it has only one file main.cpp showing an empty main window):

cmake_minimum_required (VERSION 3.7.0)

project(guitest)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

find_package(Qt5Widgets REQUIRED)
include_directories (${CMAKE_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR})
file (GLOB_RECURSE WSIMGUI_SRC *.cpp)

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

Project builds and the add_custom_command copies the Qt5Widgets.dll file to output directory. But when I try to run the program I get the following error:

This application failed to start because it could not find or load the Qt platform plugin "windows" in "".

I've been looking for this problem and I've noticed that I must also copy the platform folder from my Qt installation.

How can I do this inside my CMakeLists.txt file?

like image 367
Jepessen Avatar asked Dec 16 '16 22:12

Jepessen


People also ask

How do you use Windeployqt?

0 The simplest way to use windeployqt is to add the bin directory of your Qt installation (e.g. <QT_DIR\bin>) to the PATH variable and then run: windeployqt <path-to-app-binary> If ICU, etc. are not in the bin directory, they need to be in the PATH variable.

Where is Windeployqt EXE?

The file windeployqt.exe would be located in C:\Qt\5.9. 9\msvc2015\bin. If you have chosen to install the MS VC 2015 x64 toolchain during the installation of Qt Creator, there will be a folder named msvc2015_64. The file windeployqt.exe would be located in C:\Qt\5.9.


1 Answers

Qt provides a utility to deploy dependencies on Windows: windeployqt.

Given your Qt installation is located in <qt_install_prefix>, you should use the following commands:

set PATH=%PATH%;<qt_install_prefix>/bin
windeployqt --dir /path/to/deployment/dir /path/to/qt/application.exe

A flexible way to use windeployqt with CMake is to first declare an imported target:

find_package(Qt5 ...)

if(Qt5_FOUND AND WIN32 AND TARGET Qt5::qmake AND NOT TARGET Qt5::windeployqt)
    get_target_property(_qt5_qmake_location Qt5::qmake IMPORTED_LOCATION)

    execute_process(
        COMMAND "${_qt5_qmake_location}" -query QT_INSTALL_PREFIX
        RESULT_VARIABLE return_code
        OUTPUT_VARIABLE qt5_install_prefix
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )

    set(imported_location "${qt5_install_prefix}/bin/windeployqt.exe")

    if(EXISTS ${imported_location})
        add_executable(Qt5::windeployqt IMPORTED)

        set_target_properties(Qt5::windeployqt PROPERTIES
            IMPORTED_LOCATION ${imported_location}
        )
    endif()
endif()

You can then use it as follows:

add_executable(foo ...)

if(TARGET Qt5::windeployqt)
    # execute windeployqt in a tmp directory after build
    add_custom_command(TARGET foo
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/windeployqt"
        COMMAND set PATH=%PATH%$<SEMICOLON>${qt5_install_prefix}/bin
        COMMAND Qt5::windeployqt --dir "${CMAKE_CURRENT_BINARY_DIR}/windeployqt" "$<TARGET_FILE_DIR:foo>/$<TARGET_FILE_NAME:foo>"
    )

    # copy deployment directory during installation
    install(
        DIRECTORY
        "${CMAKE_CURRENT_BINARY_DIR}/windeployqt/"
        DESTINATION ${FOO_INSTALL_RUNTIME_DESTINATION}
    )
endif()

This can (should?) be embedded in a cmake function.


Note that using GLOB to collect source files is discouraged in CMake's doc:

We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.


If you're using Visual C++, you may also want to install visual redistributable by adding the following code at the end of your CMakeLists.txt file:

set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP TRUE)

include(InstallRequiredSystemLibraries)

install(
    PROGRAMS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS}
    DESTINATION ${FOO_INSTALL_RUNTIME_DESTINATION}
)

You can take a look to one of my project, named Softbloks, for a complete and working integration of windeployqt and InstallRequiredSystemLibraries.

like image 139
rgmt Avatar answered Sep 20 '22 16:09

rgmt