windeployqt不会为调试应用程序部署qwindowsd.dll

2022-02-27 00:00:00 qt5 qt c++

我尝试使用windeployqt.exe(Qt 5.13.2)为CMake3.16生成的调试应用程序部署dll。除平台插件DLL外,所有DLL都部署正确,它部署的是qwindows.dll而不是qwindowsd.dll,并在我尝试运行可执行文件时导致以下错误:

此应用程序无法启动,因为无法初始化任何Qt平台插件。

到目前为止,我已尝试:

  • windeployqt命令行上指定--debug。该操作失败,因为找不到Qt5Coredd.dll(请注意双%d)。
  • 正在验证是否未设置与Qt插件相关的环境变量。
  • 已检查PATH以确保它不包含具有platforms目录的任何文件夹。
如果手动复制qwindowsd.dll,则一切正常。但是,我真的想找出我在windeployqt中做错了什么。


解决方案

这显然是Qt迟迟未解决的一个已知问题,但我在CMake中想出了一个解决办法-它既适用于忍者生成器/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()

相关文章