我正在尝试使用windeployqt.exe
(Qt 5.13.2)为 CMake 3.16 生成的调试应用程序部署 dll。除了平台插件 dll 之外,所有 dll 均已正确部署,当我尝试运行可执行文件时,平台插件 dll 会部署qwindows.dll
并qwindowsd.dll
导致以下错误:
该应用程序无法启动,因为无法初始化 Qt 平台插件。
到目前为止,我已经尝试过:
--debug
在命令行上指定windeployqt
。失败是因为Qt5Coredd.dll
找不到(注意双 d)。PATH
以确保它不包含任何带有platforms
目录的文件夹。如果我qwindowsd.dll
手动复制,一切正常。但是我真的很想弄清楚我做错了什么windeployqt
。
这显然是一个已知问题,Qt 在修复方面拖了后腿,但我在 CMake 中找到了一个解决方法 - 这适用于 Ninja 生成器/Visual Studio 的内置 CMake 支持以及常规 Visual Studio 解决方案生成器
# Split windeployqt into 2 parts to fix issue with deploying debug plugins
add_custom_command(TARGET MyApp POST_BUILD
COMMAND ${QT_PATH}/bin/windeployqt --compiler-runtime --no-plugins ${MY_APP_EXE})
if (CMAKE_GENERATOR STREQUAL "Ninja")
# Ninja is a single-config generator so we can use CMAKE_BUILD_TYPE to generate different commands
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_custom_command(TARGET MyApp POST_BUILD
COMMAND ${QT_PATH}/bin/windeployqt --debug --no-compiler-runtime --no-translations --no-libraries ${MY_APP_EXE})
else()
add_custom_command(TARGET MyApp POST_BUILD
COMMAND ${QT_PATH}/bin/windeployqt --release --no-compiler-runtime --no-translations --no-libraries ${MY_APP_EXE})
endif()
else()
# if in MSVC we have to check the configuration at runtime instead of generating different commands
add_custom_command(TARGET MyApp POST_BUILD
COMMAND cmd.exe /c if "$(Configuration)" == "Debug" ${QT_PATH}/bin/windeployqt --debug --no-compiler-runtime --no-translations --no-libraries ${MY_APP_EXE})
add_custom_command(TARGET MyApp POST_BUILD
COMMAND cmd.exe /c if not "$(Configuration)" == "Debug" ${QT_PATH}/bin/windeployqt --release --no-compiler-runtime --no-translations --no-libraries ${MY_APP_EXE})
endif()
Run Code Online (Sandbox Code Playgroud)