如何在 Windows 上构建 64 位 MinGW Qt 应用程序(使用 Qt Creator)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25402225/
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-09-08 18:39:16  来源:igfitidea点击:

How to build 64-bit MinGW Qt application on Windows (with Qt Creator)

windowsqt64-bitmingw

提问by Chris

I'm looking (and sure I am not the only one) for the most straight forward way to build MinGW based 64-bit Qt programs on Windows. Straight forward means minimal effort and time (e.g. for building and setting up). Currently I run the normal, official development environment of the qt-project.org website for Windows 32-bit with MinGW/OpenGL and QtCreator. Of course I have read the (unfinished...) 64 bit wiki page of qt-project.org and I also read through different stackoverflow questions, but still I'm confused. I know that the topic of how to build 64-bit Qt is treated in other posts, but this is not my question, what I need is an overview and the most favorable 64-bit MinGW port.

我正在寻找(当然我不是唯一一个)在 Windows 上构建基于 MinGW 的 64 位 Qt 程序的最直接的方法。直接意味着最少的努力和时间(例如用于构建和设置)。目前,我使用 MinGW/OpenGL 和 QtCreator 为 Windows 32 位运行 qt-project.org 网站的正常官方开发环境。当然,我已经阅读了 qt-project.org 的(未完成的...)64 位 wiki 页面,我也阅读了不同的 stackoverflow 问题,但我仍然很困惑。我知道在其他帖子中讨论了如何构建 64 位 Qt 的主题,但这不是我的问题,我需要的是概述和最有利的 64 位 MinGW 端口。

One thing that confused me for example was that the DLLs in "x64-Qt-5.2.1+QTCreator-3.0.1-(gcc-4.8.2-seh).7z" package from mingwbuilds sourceforge project seem to be 32-bit.

例如,让我感到困惑的一件事是来自 mingwbuilds sourceforge 项目的“x64-Qt-5.2.1+QTCreator-3.0.1-(gcc-4.8.2-seh).7z”包中的 DLL 似乎是 32 位.

To clarify my question a bit let me also ask some related questions:

为了澄清我的问题,让我也问一些相关的问题:

  • Do I need to build a 64-bit development environment first to produce 64-bit executables or can I keep my normal default environment and only exchange the MinGW toolchain with a 64-bit toolchain?
  • Where can I get 64-bit Qt libraries for the deployment, or do I need to build them myself anyway? Or where can I get 64-bit development environment including the 64-bit libraries?
  • If I have to build myself, which MinGW 64-bit port is the most favorable?
  • 我是否需要先构建一个 64 位开发环境来生成 64 位可执行文件,还是我可以保持我的正常默认环境并且只用 64 位工具链交换 MinGW 工具链?
  • 在哪里可以获得用于部署的 64 位 Qt 库,或者我是否需要自己构建它们?或者我在哪里可以获得包括 64 位库在内的 64 位开发环境?
  • 如果必须自己搭建,哪个MinGW 64位端口最有利?

Thanks a lot for your help.

非常感谢你的帮助。

回答by Andrew Dolby

Update 3/11/2019:Starting with Qt 5.12 the Qt Project officially supports MinGW-w64 in 64-bit mode. You can install it from the Qt installer or build it yourself from Qt's sources (they've ironed out issues that were preventing successful builds).

2019 年 3 月 11 日更新:从 Qt 5.12 开始,Qt 项目正式支持 64 位模式下的 MinGW-w64。您可以从 Qt 安装程序安装它或从 Qt 的源代码中自己构建它(他们已经解决了阻止成功构建的问题)。

Prior update:I think that rubenvb's answeris now the best answer to this question: MSYS2 contains an up-to-date Qt built with MinGW-w64, which is what you'll need to build 64-bit Qt apps. I recommend using the Qt wiki link in rubenvb's answer for deployment; it's greatly improved.

之前的更新:我认为rubenvb 的答案现在是这个问题的最佳答案:MSYS2 包含一个用 MinGW-w64 构建的最新 Qt,这是构建 64 位 Qt 应用程序所需要的。我建议在 rubenvb 的部署答案中使用 Qt wiki 链接;它大大改善了。

Qt64-NG

Qt64-NG

I recommend installing your Qt build from the Qt64-NG project.

我建议从Qt64-NG 项目安装 Qt 构建。

The project provides an up-to-date, x64 Qt build with MinGW-w64. The MinGW-w64 project's GCC compiler is also included in the installer versions (but not in the 7z). You'll want the same version of GCC in MinGW-w64 that was used to build Qt for building your own apps, so I'd recommend choosing the installer.

该项目使用 MinGW-w64 提供最新的 x64 Qt 构建。MinGW-w64 项目的 GCC 编译器也包含在安装程序版本中(但不在 7z 中)。您将需要 MinGW-w64 中用于构建 Qt 以构建您自己的应用程序的相同版本的 GCC,因此我建议选择安装程序。

You will need to choose a build with the exception stack frame unwinding method you want. The question What is difference between sjlj vs dwarf vs seh?describes the differences between the types of exception handlers that are present in different builds of Qt. SJLJ was the only choice before GCC 4.8 was released, but later GCC versions support SEH. Either choice will work, though SEH is more efficient.

您将需要选择具有您想要的异常堆栈帧展开方法的构建。问题sjlj vs dwarf vs seh 有什么区别?描述了不同 Qt 版本中存在的异常处理程序类型之间的差异。在 GCC 4.8 发布之前,SJLJ 是唯一的选择,但后来的 GCC 版本支持 SEH。任一种选择都行,尽管 SEH 效率更高。

Post-install

安装后

After you install Qt, you could add the Qt/qt-ver/qt-ver/bin and Qt/qt-ver/mingw64/bin directories to your User or System Path variable in your Environment Variables, which will allow you to use MinGW-w64's GCC for building from your preferred build process. Qt Creator will auto-detect MinGW-w64 as an installed compiler after you do this.

安装 Qt 后,您可以将 Qt/qt-ver/qt-ver/bin 和 Qt/qt-ver/mingw64/bin 目录添加到环境变量中的用户或系统路径变量中,这将允许您使用 MinGW -w64 的 GCC,用于从您首选的构建过程进行构建。执行此操作后,Qt Creator 将自动检测 MinGW-w64 作为已安装的编译器。

If you would rather not add to your path, you can specify the compiler for the version you just set up with Qt Creator, manually. You will likely need to add the Qt version by selecting qmake.exe under Options > Build & Run. You'll also want to select gdb.exe as your debugger under the Build & Run > Debuggers tab. It will be located at Qt/qt-ver/qt-ver/mingw64/bin.

如果您不想添加到您的路径中,您可以手动指定您刚刚使用 Qt Creator 设置的版本的编译器。您可能需要通过在 Options > Build & Run 下选择 qmake.exe 来添加 Qt 版本。您还需要在 Build & Run > Debuggers 选项卡下选择 gdb.exe 作为调试器。它将位于 Qt/qt-ver/qt-ver/mingw64/bin。

You can exchange the MinGW-w64 toolchain with other toolchains. To do this in Qt Creator you can create and swap between kits.

您可以将 MinGW-w64 工具链与其他工具链交换。要在 Qt Creator 中执行此操作,您可以创建套件并在套件之间进行交换。

Deployment

部署

You can use the Qt64-NG build for development and deployment. To deploy, you'll want to package the files your application will need to run with your application, so your end-user won't have to do any extra installation steps.

您可以使用 Qt64-NG 构建进行开发和部署。要进行部署,您需要将应用程序与应用程序一起运行所需的文件打包,这样您的最终用户就不必执行任何额外的安装步骤。

First, create a deployment folder (likely with your application name) and copy your application exe file to it. If you're using Qt Creator, you can find the path to your application exe either in the Projects tab on the side bar or in the application's .pro file if it is specified there.

首先,创建一个部署文件夹(可能包含您的应用程序名称)并将您的应用程序 exe 文件复制到其中。如果您使用的是 Qt Creator,则可以在侧栏的“项目”选项卡中或应用程序的 .pro 文件(如果在此处指定)中找到应用程序 exe 的路径。

There are two ways that you can copy the dll dependencies you'll need to run your application. You can either copy the needed files manually or you can use windeployqt, a program provided with your Qt installation that will automatically copy (most) needed dependencies for deployment to the application deploy folder you just created.

有两种方法可以复制运行应用程序所需的 dll 依赖项。您可以手动复制所需的文件,也可以使用 Windeployqt,这是一个随 Qt 安装提供的程序,它会自动将部署所需的(大多数)依赖项复制到您刚刚创建的应用程序部署文件夹中。

Copy files manually

手动复制文件

Copy the following dll files from your Qt/qt-ver/qt-ver/bin and Qt/qt-ver/mingw64/bin folders to your application deploy folder. Some dll files will be located in both folders, and either location is acceptable for use:

将以下 dll 文件从 Qt/qt-ver/qt-ver/bin 和 Qt/qt-ver/mingw64/bin 文件夹复制到应用程序部署文件夹。一些 dll 文件将位于这两个文件夹中,并且任一位置都可以使用:

icudt54.dll
icuin54.dll
icuuc54.dll
libgcc_s_sjlj.dll or libgcc_s_seh.dll (corresponds with chosen stack unwinding method)
libssp-0.dll
libstdc++-6.dll
libwinpthread-1.dll
Qt5Core.dll
Qt5Gui.dll
Qt5Widgets.dll

You'll also need to include any Qt5*.dll files that correspond with Qt modules you are using in your project.

您还需要包含与您在项目中使用的 Qt 模块对应的任何 Qt5*.dll 文件。

Next, you'll need to copy any plugin dll files that Qt requires. Qt will look for plugins by default at application deploy folder/plugins. You can find the plugin folders you will want to copy at Qt/qt-ver/qt-ver/plugins. The exception to this is the platforms folder, which it will try to find at your application deploy directory/platforms. The following directory structure indicates where to copy files for a minimal project. Copy any other needed plugins including the containing folder to your deployment/plugins folder.

接下来,您需要复制 Qt 所需的任何插件 dll 文件。默认情况下,Qt 将在应用程序部署文件夹/插件中查找插件。您可以在 Qt/qt-ver/qt-ver/plugins 中找到要复制的插件文件夹。例外是平台文件夹,它将尝试在您的应用程序部署目录/平台中找到它。以下目录结构指示了为最小项目复制文件的位置。将任何其他需要的插件(包括包含文件夹)复制到您的部署/插件文件夹。

/deploy

/部署

/platforms
/plugins
icudt54.dll
icuin54.dll
icuuc54.dll
app.exe
libgcc_s_seh-1.dll
libssp-0.dll
libstdc++-6.dll
libwinpthread-1.dll
Qt5Core.dll
Qt5Gui.dll
Qt5Widgets.dll

/deploy/platforms

/部署/平台

qwindows.dll

/deploy/plugins/imageformats

/部署/插件/图像格式

qjpeg.dll

Windeployqt

Windeployqt

You can find windeployqt.exe in your Qt/qt-ver/qt-ver/bin folder. Windeployqt requires that your Qt/qt-ver/qt-ver/bin and mingw64/bin folder be in your User or System Path variable to work properly.

您可以在 Qt/qt-ver/qt-ver/bin 文件夹中找到 windeployqt.exe。Windeployqt 要求您的 Qt/qt-ver/qt-ver/bin 和 mingw64/bin 文件夹位于您的用户或系统路径变量中才能正常工作。

To use windeployqt, open a command window and change to your application deploy directory. The syntax for running it can be found at Qt Deployment. You'll probably want to run it as follows:

要使用 windeployqt,请打开命令窗口并切换到您的应用程序部署目录。运行它的语法可以在Qt Deployment 中找到。您可能希望按如下方式运行它:

windeployqt.exe app.exe

Windeployqt (Qt 5.4.1) will copy your imageformats and iconengines plugin folders to your application deploy directory level instead of inside a plugins folder. This is the correct location, as it alters where Qt looks for these folders.

Windeployqt (Qt 5.4.1) 会将您的 imageformats 和 iconengines 插件文件夹复制到您的应用程序部署目录级别,而不是在插件文件夹中。这是正确的位置,因为它会改变 Qt 查找这些文件夹的位置。

Troubleshooting

故障排除

You can test to see if you have the correct files by running your application on a computer that doesn't have the Qt development library installed and in the path. If you get no errors on your application startup, you'll know that Qt found the dependencies it needed, and you can package your application for distribution.

您可以通过在未安装 Qt 开发库且路径中的计算机上运行您的应用程序来测试您是否拥有正确的文件。如果您的应用程序启动时没有出现错误,您就会知道 Qt 找到了它需要的依赖项,并且您可以打包您的应用程序以进行分发。

If you are getting errors, refer to the Tripleboot deployment guide. You can also try using Process Explorerto see what dependencies your application attempts to load when it gives a dependency-missing error.

如果出现错误,请参阅Tripleboot 部署指南。您还可以尝试使用Process Explorer查看您的应用程序在出现依赖项缺失错误时尝试加载哪些依赖项。

Packaging

包装

Frequently applications are distributed with an archive and/or an installer. The Qt Installer Frameworkprovides a convenient way to package your Qt application for distribution as an installer executable.

应用程序经常随存档和/或安装程序一起分发。在Qt的安装程序框架提供了一个便捷的方式打包Qt应用程序分发作为安装程序可执行文件。

回答by rubenvb

You can install MSYS2, which includes a package manager and prebuilt versions of load of packages including Qt which you can also use outside of the MSYS2 environment with e.g. Qt Creator (which is also in the repositories by the way).

您可以安装 MSYS2,它包括一个包管理器和包含 Qt 的包加载的预构建版本,您也可以在 MSYS2 环境之外使用例如 Qt Creator(顺便说一下,它也在存储库中)。

Installation is easy:

安装很简单:

  1. Download the installer from the web page.
  2. Open the MSYS2 command prompt and install Qt:

    pacman -Sy mingw-w64-{i686,x86_64}-qt5

  3. Qt's qmake is installed in <msys2>/mingw{64,32}/binand accessible from the respective command prompts.

  1. 网页下载安装程序。
  2. 打开 MSYS2 命令提示符并安装 Qt:

    pacman -Sy mingw-w64-{i686,x86_64}-qt5

  3. Qt 的 qmake 安装在<msys2>/mingw{64,32}/bin相应的命令提示符中并可从其访问。

In the same way you can install hundreds of other libraries and tools you might need for your development.

以同样的方式,您可以安装开发所需的数百个其他库和工具。

As for the rest of application deployment, I refer you to the Qt project wiki page on Windows deployment.

至于应用程序部署的其余部分,我建议您参阅有关 Windows 部署的 Qt 项目 wiki 页面