gpt4 book ai didi

c++ - 将 cmake 项目与 Qt5 链接时部署 icu 库

转载 作者:行者123 更新时间:2023-12-03 00:11:58 32 4
gpt4 key购买 nike

我有以下CMakeLists.txt用于创建一个小应用程序:

cmake_minimum_required (VERSION 3.5.0)

project (sampleapp)

# Setting project flags
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/lib)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/lib)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/bin)
set (CMAKE_INCLUDE_CURRENT_DIR ON)
set (CMAKE_AUTOMOC ON)

# Finding needed packages
find_package (Qt5Widgets REQUIRED)
find_package (Qt5Core REQUIRED)
find_package (Qt5Gui REQUIRED)

# Setting project files
include_directories (${CMAKE_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR})
file (GLOB_RECURSE PROJECT_SRC *.cpp)

# Creating project
add_executable(${PROJECT_NAME} ${PROJECT_SRC})

# Linking dependencies
target_link_libraries (${PROJECT_NAME} somelibrary Qt5::Widgets Qt5::Gui Qt5::Core)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Widgets> $<TARGET_FILE_DIR:${PROJECT_NAME}>)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Gui> $<TARGET_FILE_DIR:${PROJECT_NAME}>)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Core> $<TARGET_FILE_DIR:${PROJECT_NAME}>)

当我运行它然后构建应用程序时,Qt dll 将被复制到目标文件夹中。我特别复制:

  • Qt5Core.dll
  • Qt5Gui.dll
  • Qt5Widgets.dll

当我运行该应用程序时,它无论如何都不会启动,因为 Qt 所需的 ICU 库未被复制。当我启动应用程序时,Windows 告诉我缺少以下库:

  • icuin53.dll
  • icuuc53.dll

如果我从 Qt 安装文件夹手动复制这些库,它就可以工作(好吧,我有一个关于“平台插件窗口”丢失的错误,但这是另一个故事)。

有没有一种方法可以以标准方式复制 icu 库,例如我用于复制库的构建后命令?或者如果可能的话,以透明的方式复制它们的最佳方法是什么(CMake 可以知道 Qt 的依赖项是什么,并将它们复制到需要 Qt 的地方)?

最佳答案

您可以使用 windeployqt(随 Qt 一起提供的用于在 Windows 上部署的工具)安装所有 Qt 依赖项,而无需手动复制每个 dll。

您可以首先将 windeployqt 声明为导入的可执行文件:

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

现在可以按如下方式使用导入的可执行文件:

add_executable(foo
# ...
)

if(TARGET Qt5::windeployqt)
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:foo>"
)

install(
DIRECTORY
"${CMAKE_CURRENT_BINARY_DIR}/windeployqt/"
DESTINATION ${FOO_INSTALL_RUNTIME_DESTINATION}
)
endif()

关于c++ - 将 cmake 项目与 Qt5 链接时部署 icu 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41912734/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com