首页 > 解决方案 > CMake将Qt运行时库复制到可执行输出路径

问题描述

这个问题是重复的。但是,在筛选其他帖子后,我仍然无法解决问题。我的问题:尝试打包我的应用程序时,所以在我运行 cpack 后,运行时依赖项Qt5Core.dll, Qt5Gui.dll, Qt5Network.dll, Qt5Svg.dll, Qt5Widgets.dll放在目录的根目录中,而可执行文件和其余依赖项位于 bin 文件夹中。

就像我知道的那样,我可以将 bin 文件夹的内容复制到根目录之外,它会起作用,但就像不一样。

它看起来像什么

我的怀疑不确定,但我相信这个问题来自使用include(GNUInstallDirs). 如果有人可以提供解释和解决方案。谢谢


尝试运行可执行文件时出现错误消息。

This application failed to start because no Qt platform plugin could be initialized! Reinstalling the application may fix this problem.

CMakeLists.txt

cmake_minimum_required(VERSION 3.11.0)

project(Translation_Verification VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt5 5.15 REQUIRED COMPONENTS Core Gui Network Widgets)

set(QONLINETRANSLATOR_VERSION 1.4.5)
include(FetchContent)
FetchContent_Declare(qonlinetranslator
    GIT_REPOSITORY https://github.com/crow-translate/QOnlineTranslator.git
    GIT_TAG ${QONLINETRANSLATOR_VERSION}
)

FetchContent_GetProperties(qonlinetranslator)
if(NOT qonlinetranslator_POPULATED)
    FetchContent_Populate(qonlinetranslator)
    add_subdirectory(${qonlinetranslator_SOURCE_DIR} ${qonlinetranslator_BINARY_DIR})
endif()

set(PROJECT_SOURCES
    src/main.cpp
    src/addlanguagedialog.cpp
    src/addlanguagedialog.ui
    src/verification.cpp
    src/widgetwindow.ui
    src/widgetwindow.cpp
)

include(GNUInstallDirs)
add_executable(Translation_Verification WIN32 ${PROJECT_SOURCES})
target_link_libraries(Translation_Verification QOnlineTranslator Qt5::Core Qt5::Gui Qt5::Network Qt5::Widgets)
install(TARGETS Translation_Verification RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

target_include_directories(Translation_Verification PUBLIC
    "${qonlinetranslator_BINARY_DIR}/src"
    "${qonlinetranslator_SOURCE_DIR}/src")
    
##CPack
get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION)
get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)

set(CPACK_PACKAGE_NAME "Translation_Verification")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Translation Verification Installation")
set(CPACK_PACKAGE_VERSION "1.0.0") # Version of installer

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    install(FILES ${CMAKE_SOURCE_DIR}/translationVerification.desktop DESTINATION share/applications/)
    set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-linux-${ARCHITECTURE}")
    set(CPACK_GENERATOR "TXZ")

elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")

    set(CPACK_PACKAGE_INSTALL_DIRECTORY "Translation_Verification")
    set(CPACK_NSIS_DISPLAY_NAME ${CMAKE_PACKAGE_NAME})
    set(CPACK_NSIS_COMPRESSOR lzma)
    set(CPACK_NSIS_INSTALLED_ICON_NAME Translation_Verification.exe)
    set(CPACK_NSIS_MENU_LINKS "Translation_Verification.exe" "Translation Verification")
    set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION ${CMAKE_INSTALL_BINDIR})
    set(CMAKE_INSTALL_UCRT_LIBRARIES TRUE)

    include(InstallRequiredSystemLibraries)
    find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS "${_qt_bin_dir}")

    add_custom_command(TARGET Translation_Verification POST_BUILD
                       COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/qtDeploy/
                       COMMAND ${WINDEPLOYQT_EXECUTABLE}
                               --release
                               --verbose 1
                               --no-compiler-runtime
                               --no-angle
                               --no-opengl
                               --no-opengl-sw
                               --no-webkit2
                               --no-quick-import
                               --no-translations
                               --dir ${CMAKE_BINARY_DIR}/qtDeploy $<TARGET_FILE:Translation_Verification>
    )
    install(
        DIRECTORY ${CMAKE_BINARY_DIR}/qtDeploy/
        DESTINATION .
        FILES_MATCHING PATTERN "*.*"
    )
    set(CPACK_GENERATOR "ZIP;NSIS")
endif()

set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-src")
set(CPACK_SOURCE_GENERATOR "ZIP;TGZ")
include(CPack)

标签: c++qtcmakeshared-librariescpack

解决方案


DESTINATION .您使用in明确要求您的结果

    install(
        DIRECTORY ${CMAKE_BINARY_DIR}/qtDeploy/
        DESTINATION .
        FILES_MATCHING PATTERN "*.*"
    )

这可能是DESTINATION ${CMAKE_INSTALL_BINDIR}指向您用于其他二进制文件的同一目录。


推荐阅读