C++ 在 Windows 上部署 Qt 5 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22185974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:58:55  来源:igfitidea点击:

Deploying Qt 5 App on Windows

c++windowsdeploymentqt

提问by Alex Spataru

I've written a couple of applications in QML (part of Qt 5). In a question that I've made before (https://softwareengineering.stackexchange.com/questions/213698/deploying-qt-based-app-on-mac-os-x), I found the solution for deploying my app on OS X (using the macdeployqt tool).

我已经用 QML(Qt 5 的一部分)编写了几个应用程序。在我之前提出的一个问题(https://softwareengineering.stackexchange.com/questions/213698/deploying-qt-based-app-on-mac-os-x)中,我找到了将我的应用程序部署到OS X(使用 macdeployqt 工具)。

Deploying Qt4 apps on Windows was easy:

在 Windows 上部署 Qt4 应用程序很容易:

  1. You compiled it in release mode.
  2. You copied the necessary libraries (DLLs).
  3. You tested and it worked.
  1. 你在发布模式下编译它。
  2. 您复制了必要的库 (DLL)。
  3. 你测试过,它奏效了。

Unfortunately, this approach did not work in Qt5 (I even included the platforms folder with the qwindows.dll file and it did not work). After some days of trying, I gave up and compiled a static version of Qt5.

不幸的是,这种方法在 Qt5 中不起作用(我什至在 qwindows.dll 文件中包含了平台文件夹,但它不起作用)。经过几天的尝试,我放弃并编译了Qt5的静态版本。

Again, it did not work. The app works on a PC with Qt installed, but it crashes on "clean" PCs. As a side note, Windows 8/8.1 systems don't give a warning or a message notifying me about the app's crash. But in Windows 7 a message notifies me that the application crashed.

再次,它没有用。该应用程序可在安装了 Qt 的 PC 上运行,但在“干净”的 PC 上会崩溃。作为旁注,Windows 8/8.1 系统不会发出警告或消息通知我有关应用程序崩溃的信息。但是在 Windows 7 中,一条消息通知我应用程序崩溃了。

I've tried running Dependency Walker (depends.exe) and all libraries in the static build of my application seemed fine.

我试过运行 Dependency Walker (depends.exe) 并且我的应用程序的静态构建中的所有库看起来都很好。

In Windows 8, I don't get any error. But after profiling the app in depends.exe, I get an access violation originating from QtGui.dll. The exact error is

在 Windows 8 中,我没有收到任何错误。但是在depends.exe 中分析应用程序后,我得到了一个源自QtGui.dll 的访问冲突。确切的错误是

Second chance exception 0xC0000005 (Access Violation) occurred in "QT5GUI.DLL" at address 0x61C2C000.

在地址 0x61C2C000 的“QT5GUI.DLL”中发生第二次机会异常 0xC0000005(访问冲突)。

Is there something that I am missing (say an extra DLL or config file)?

有什么我遗漏的吗(比如一个额外的 DLL 或配置文件)?

Application information:

应用信息:

  • Written and compiled with Qt 5.2.1
  • Uses Quick/QML.
  • Uses the network module.
  • Uses the webkit module.
  • Uses the bluetooth module.
  • The QML files are written in Quick 2.2
  • 使用 Qt 5.2.1 编写和编译
  • 使用快速/QML。
  • 使用网络模块。
  • 使用 webkit 模块。
  • 使用蓝牙模块。
  • QML 文件是用 Quick 2.2 编写的

采纳答案by Alex Spataru

After some hours digging in the Qt Forums, I found out that I need to copy the "qml" folder (normally located in C:/Qt/5.2.1/qml) to the application's root directory. After doing so, both the dynamic and static versions of my application worked on vanilla systems.

在 Qt 论坛中挖掘了几个小时后,我发现我需要将“qml”文件夹(通常位于 C:/Qt/5.2.1/qml)复制到应用程序的根目录。这样做之后,我的应用程序的动态和静态版本都可以在普通系统上运行。



Program directory (MinGW 4.8 32-bit, dynamic):

程序目录(MinGW 4.8 32 位,动态):

As hydesaid, use the windeployqttool (<qt path>\<version>\bin\windeployqt.exe) to copy the necessary files to your application's folder. After that, copy the required QML components from <qt path>\<version>\qml\to your application's folder. The resulting folder should look similar to:

正如海德所说,使用windeployqt工具 ( <qt path>\<version>\bin\windeployqt.exe) 将必要的文件复制到应用程序的文件夹中。之后,将所需的 QML 组件复制<qt path>\<version>\qml\到应用程序的文件夹中。生成的文件夹应类似于:

  • platforms (folder)
  • QtQuick (folder)
  • QtQuick.2 (folder)
  • Any other QML components that you need
  • app.exe
  • icudt51.dll
  • icuin51.dll
  • icuuc51.dll
  • libgcc_s_dw2-1.dll
  • libstdc++-6.dll
  • libwindthread-1.dll
  • Qt5Core.dll
  • Qt5Gui.dll
  • Qt5Qml.dll
  • Qt5Quick.dll
  • Qt5Network.dll
  • Qt5Widgets.dll
  • 平台(文件夹)
  • QtQuick(文件夹)
  • QtQuick.2(文件夹)
  • 您需要的任何其他 QML 组件
  • 应用程序
  • icudt51.dll
  • icuin51.dll
  • icuuc51.dll
  • libgcc_s_dw2-1.dll
  • libstdc++-6.dll
  • libwindthread-1.dll
  • Qt5Core.dll
  • Qt5Gui.dll
  • Qt5Qml.dll
  • Qt5Quick.dll
  • Qt5Network.dll
  • Qt5Widgets.dll


Program directory (static)

程序目录(静态)

Compile the application statically, then copy the required QML components from <qt path>\<version>\qml\to your application's folder. The resulting folder should look similar to:

静态编译应用程序,然后将所需的 QML 组件复制<qt path>\<version>\qml\到应用程序的文件夹中。生成的文件夹应类似于:

  • QtQuick (folder)
  • QtQuick.2 (folder)
  • Any other QML components that you need
  • app.exe
  • QtQuick(文件夹)
  • QtQuick.2(文件夹)
  • 您需要的任何其他 QML 组件
  • 应用程序


I think the cause for the crash was that the Qt5Gui.dll(dynamic and static) "tried" to load the QtQuick* folders during run time, but could not find them (thus crashing the application during load).

我认为崩溃的原因是Qt5Gui.dll(动态和静态)“尝试”在运行时加载 QtQuick* 文件夹,但找不到它们(因此在加载过程中使应用程序崩溃)。

回答by hyde

Starting from Qt 5.2, there is windeployqttool you can use. Just run it from command line to get help. But basic usage is, give it the .exe file, it will copy Qt dependencies to go with it.

从 Qt 5.2 开始,windeployqt您可以使用一些工具。只需从命令行运行它即可获得帮助。但基本用法是,给它.exe文件,它会复制Qt依赖项来配合它。

You will want to use --qmldiroption to let the tool know where your QML files are, so it can figure out the needed QML dependencies.

您将需要使用--qmldir选项让工具知道您的 QML 文件在哪里,以便它可以找出所需的 QML 依赖项。

Note about testing: to make sure you have everything, test in computer with no Qt SDK, or temporarily renamethe Qt directory. Otherwise the application might find missing files from there...

关于测试的注意事项:为确保您拥有一切,请在没有 Qt SDK 的计算机上进行测试,或者暂时重命名Qt 目录。否则应用程序可能会从那里找到丢失的文件......