在构build时部署所有Qt依赖项

我创build了一个CMakeLists.txt来创build一个简单的Qt应用程序(实际上它只有一个文件main.cpp显示一个空的主窗口):

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

项目构build和add_custom_commandQt5Widgets.dll文件复制到输出目录。 但是当我尝试运行该程序时,出现以下错误:

此应用程序无法启动,因为它无法find或加载“”中的Qt平台插件“窗口”。

我一直在寻找这个问题,我注意到我也必须从我的Qt安装中复制platform文件夹。

我怎样才能在我的CMakeLists.txt文件中做到这一点?

Qt提供了一个在Windows上部署依赖项的工具: windeployqt

鉴于您的Qt安装位于<qt_install_prefix> ,您应该使用以下命令:

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

与CMake一起使用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) # 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() 

这可以(应该?)嵌入到cmake函数中。


请注意,在CMake的文档中不鼓励使用GLOB来收集源文件:

我们不建议使用GLOB从源代码树中收集源文件列表。 如果在添加或删除源时没有更改CMakeLists.txt文件,则生成的生成系统无法知道何时要求CMake重新生成。


如果您使用的是Visual C ++,则可能还需要在CMakeLists.txt文件的末尾添加以下代码,以安装可视可再发行组件:

 set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP TRUE) include(InstallRequiredSystemLibraries) install( PROGRAMS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} DESTINATION ${FOO_INSTALL_RUNTIME_DESTINATION} ) 

您可以看看我的一个名为Softbloks的项目,以获得 windeployqtInstallRequiredSystemLibraries的完整工作集成。