2018-12-3日重要说明:
1、本文为官方编译静态库的翻译,经本人反复试验,除第二部分使用没有问题外,第一部分总是在 wingw32-make的时候出现错误,无法编译5.11.2版本。
2、故建议大家不要再依据本文前面的内容进行编译。而是直接使用编译好的静态库。
3、如果您已经编译好了5.10或者5.11.2,如果愿意的话请把编译细节给我反馈一下。
Building a static Qt for Windows using MinGW
再windows下使用Qt MinGW建立静态程序编译
Why a static Qt?
为什么要静态链接Qt?
On Windows, the pre-built environments from qt.io are dynamically linked. This means that your Qt application dynamically references the Qt DLL's which are installed with the pre-built environment.
在windows平台上的QT,缺省的编译环境是采用动态链接的。就是说你的程序采用动态引用QT动态库(DLL)的方式。
This is probably the preferred environment for developing and debugging since everything is there and ready to use. However, when it comes to deploying an application on systems without pre-installed Qt environments, it becomes complicated. There are several DLL’s to deploy with the application. Even using the classical DLL Dependency Walker tool, you cannot find all required DLL’s since some of them are dynamically loaded at execution. So, when the application is ready to deploy, it is easier to build a static version of it. But building a static version of the application requires a static version of the Qt environment in addition to the pre-built dynamic Qt environment.
这种方式对于开发和调试程序是适宜的。但是,当你分发(部署)程序到没有预安装QT环境的系统中,就会非常复杂(无法使用)。每个程序都有各自依赖的动态库。甚至是一些基本的库都在新环境无法找到。所以,当程序准备分发时,更方便的办法时建立静态库(库和程序打包到一起)。这就需要在QT环境中建立一个程序静态版本以便编译成静态链接程序。
This article describes an automated way of downloading, building and installing a static Qt development environment using a Windows PowerShell script.
这篇文章描述了使用windows powershell script方法来下载、编译和安装QT静态开发环境的内容。
Note that building and installing a static version of Qt on Windows was somewhat problematic up to Qt 4. But Qt 5 has now a much better support for static builds on Windows and this article takes advantage of that.
注意,在windows上之前的QT4及之前的版本有些问题。现在QT5已经能够更好的做到这一点了。
MinGW vs. MSVC
MinGW 和 MSVC
On Windows, two pre-built environments can be downloaded: one using MinGW and one using Microsoft Visual Studio. The two environments are incompatible and cannot be mixed. You have to choose one.
在windows平台上,有两种预设的开发环境:MinGW和MSVC。这两种环境不兼容,不可以交叉使用。
This article describes how to build a static version of Qt in a MinGW environment. If you need a static Qt for MSVC, you may probably find some ideas here but there is definitely some extra work.
本文描述如何在MinGW环境中构建Qt的静态版本。如果您需要MSVC的静态Qt,您可能会在这里找到一些想法,但肯定有一些额外的工作(译者:不会少)。
Target environment
目标环境
When you develop Qt applications on Windows and need to deploy them later on "general purpose" Windows machines, the recommended Qt environment contains two distinct builds of the same version of Qt:
当你在windows开发QT,并需要稍后再windows平台部署时,推荐QT环境包括两个不同的构建环境。
The pre-built dynamic environment shall be used for development and debugging. In addition to the Qt command line tools (qmake, moc, uic, lupdate, lrelease, etc.), it contains the high-level GUI tools such as Qt Creator, Assistant, Linguist, etc. The installation is straightforward, everything is here, well integrated, working like a charm.
预安装好的动态(编译)环境应该用于开发和调试。包括QT命令行工具(qmake, moc, uic, lupdate, lrelease)等,还包括高级GUI工具,例如Qt Creator, Assistant, Linguist等。所有的东西都在这里,很好的集成再一起工作。
The static environment shall be used to release the deployment version of the application, after it has been fully developed and debugged using the dynamic environment. The static environment contains only the Qt command line tools. There are at least two reasons for not building static versions of the GUI tools. First, releasing a new version of an application is typically a task that you automate using a script and there is no need for GUI tools at this stage. Second, it is probably impossible to build a static version of Qt Creator because of the lack of support for plugins in a static environment.
静态环境应该再程序开发编译成为release版本时使用。静态环境只包括QT命令行。这是因为首先发布应用程序通常使用脚本自动完成的,不需要高级GUI工具。其次,由于静态环境缺少对一些高级插件的支持。(译者:总之,开发调试用动态库,封包时用静态库封包。没有任何问题)
Restrictions in a static environment
静态环境的限制
Although building a static version of your application makes it easier to deploy, there are some restrictions in a static environment. Be sure to check that your application complies with these constraints before going further.
虽然编译一个静态程序可以简化程序部署,但是再静态环境中有一些限制。在继续之前,请确保您的程序符合以下条件。
Also note the following points: 另外还要注意以下几点:
Prerequisites
准备
Dynamic pre-built Qt environment
动态预编译Qt环境
Install the dynamic pre-built package from qt.io. Select the package named "Qt 5.5.0 for Windows 32-bit (MinGW 4.9.2, 959 MB)". The exact name and version may vary. You can also browse the tree of installation packages.
安装动态预编译安装包。
Preferably plan the installation of the same version of Qt for the dynamic and static builds. This is not a strict requirement but it is a safe configuration. At the time of writing of this article, the latest Qt version is 5.5.0.
最好事安装相同版本的动态和静态构建。这不是强制的,但是为了更安全的设置,推荐如此。本文Qt的最新版本事5.5(译者:偶是5.11)
During installation, select the default installation location (C:). Be sure to add the MinGW environment in the installation (selectable in the "Tools" branch of the installation options).
在安装过程中,缺省安装到C:盘。确认MinGW环境安装。
If you have a separated MinGW environment that you prefer to use and do not install the one from the Qt package installer, be sure to specify the option -MingwDir in the static build script (see below).
如果你有一个分离的MinGW环境,可以不在Qt安装(译者:不建议)。
Windows PowerShell
The script which builds the static Qt environment is a PowerShell script.
这个windows Powershell脚本就是建立静态Qt环境的脚本。(译者:启动方法:win10.打开放大镜,输入“powershell”即可看到。
Note: at least one user has experienced problems with this script. For a discussion, and workaround, see this forum topic.
The PowerShell version 3.0 or higher is required. PowerShell 3.0 comes with Windows 8 but it can be installed on Windows 7 as well from Microsoft download. It is probably impossible to install PowerShell 3.0 on Windows XP or Vista. This article consequently applies to Windows 7 and higher only.
powershell咬嘴最低版本,win7需要下载新版本。xp需要重新安装。相关下载见上述链接。
7-Zip
The build script also requires 7-Zip to uncompress the downloaded Qt source code. Be sure to install it first.
编译脚本要求7-zip解压缩支持,请确认已有。
Building the Qt static environment
1. 构建Qt静态环境
Download and run, all done
下载、运行,完成
Download the script named windows-build-qt-static.ps1 and execute it.
首先,下载脚本并在powershell中执行。
The above link fetches the script directly from the git repository of a small open-source project named QtlMovie. You may want to have a look at the various build scripts in this project for sample application builds and installations. 上面的链接直接从名为QtlMovie的小型开源项目的git存储库中获取脚本。您可能希望查看这个项目中用于示例应用程序构建和安装的各种构建脚本(点击链接查看)。
The windows-build-qt-static.ps1 script can be directly invoked from the Windows Explorer if you are ok with the default parameter values (see below). If you are not familiar with PowerShell, simply right-click on the file and select "Run with PowerShell".
The script performs the following steps:
这个脚本能直接执行,并执行以下步骤。如果你不熟悉powershell,可以右键点击文件,选择“run with powershell”
The execution of this script may take a couple of hours to complete, depending on your processing power. So you have to be patient. Qt is a complex framework with a very large amount of code.
这个过程,根据你计算机,可能需要几个小时。所以请您耐心。Qt是一个包括大量代码和复杂框架的系统(译者:虽自卖自夸,但也的确如此)。
Command line syntax
命令行语法
Alternatively, you can invoke the script from the PowerShell prompt using the following syntax:
除了上面的脚本,您也可以在powershell中输入下面命令来调用脚本。
windows-build-qt-static.ps1 [-QtSrcUrl url] [-QtStaticDir directory] [-QtVersion string] [-MingwDir directory] [-NoPause]
Command line parameters
命令行参数
-QtSrcUrl url
Specify the URL of the Qt source file archive. By default, the latest identified version is hardcoded ([1] at the time of this article was edited).
指定Qt源代码文件的URL。缺省是最新代码。(译者:可以考虑选择和动态库同版本的)
Instead of using the parameter on the command line, you may want to modify the default value in the script using another URL / version. Update the following line:
可以使用下面的参数,修改对应版本来下载。
$QtSrcUrl = "http://download.qt-project.org/official_releases/qt/5.5/5.5.0/single/qt-everywhere-opensource-src-5.5.0.7z", |
-QtStaticDir directory
Specify the root directory where the static versions of Qt are installed. By default: C:.
指定静态安装目录。缺省是C:。
-QtVersion string
Specify the Qt version. By default, the script tries to extract the version number from the Qt source file name. Usually, this is sufficient, the script will find "5.5.0" or "5.0.0-rc1" for instance. If the naming scheme of the Qt source file changes one day, you may need to specify an explicit value to this parameter.
指定Qt版本。缺省情况下,脚本会试图从源代码中解压出版本。同样,这就足够了。如果qt命名发生了变化,您就需要修改(译者:不要考虑)
-MingwDir directory
Specify the root directory of the MinGW environment. By default, use the version which was installed by the prebuilt Qt environment.
指定MinGW环境的根目录,缺省采用预安装的环境。
When the prebuilt environment for Qt 5.5.0 is installed for instance, the script locates MinGW in C:\5.5.0\Tools\mingw492_32. If you chose not to install MinGW as part of the prebuilt environment for Qt, you have to specify the root directory of your MinGW installation.
如果您以前没有安装MingW,您需要指定您自己的MinGW安装目录(译者:不建议)
-NoPause
Do not wait for the user to press enter at the end of execution. By default, the scripts executes a pause instruction at the end of execution, which is useful when the script was run from the Windows Explorer.
不打断自动执行脚本。
Using the Qt static environment
2. 使用Qt静态环境
Command line
2.1 命令行方式
This section describes how to use the static Qt environment from the command line, either the traditional Windows command shell (cmd.exe) or the Windows PowerShell.
这一节描述了如何从命令行使用静态Qt环境,包括传统命令行和powershell命令行。
Simply add the bin subdirectory of the static Qt environment in front of the Path environment variable. For Qt 5.5.0, this is C:\Qt\Static\5.5.0\bin.
添加静态文件路径
Do not forget to also include the MinGW environment in the path. With the dynamic pre-built Qt environment, you may want to add Qt Creator also.
不要忘记添加Mingw路径。在动态预编译Qt环境,您也需要添加Qt creator路径。
The following commands summarize the command line setups for the various environments.
Dynamic pre-built Qt with the traditional Windows command shell:
下面的命令总结了用于各种环境的命令行设置。
使用传统Windows命令行动态预构建Qt:
set Path=C:\Qt\Qt5.5.0\5.5.0\mingw492_32\bin;C:\Qt\Qt5.5.0\Tools\mingw492_32\bin;C:\Qt\Qt5.5.0\Tools\QtCreator\bin;C:32;C: |
Dynamic pre-built Qt with the Windows PowerShell:
powershell下面动态预编译Qt路径设置
$env:Path=C:\Qt\5.5.0\5.5.0\mingw492_32\bin;C:\Qt\Qt5.5.0\Tools\mingw492_32\bin;C:\Qt\5.5.0\Tools\QtCreator\bin;C:\32;C: |
Static Qt build with the traditional Windows command shell:
传统windows命令行构建静态Qt路径设置
set Path=C:\2.0\bin;C:\Qt\Qt5.5.0\Tools\mingw492_32\bin;C:\32;C: |
Static Qt build with the Windows PowerShell:
powershell命令行构建静态Qt路径设置
$env:Path=C:\Qt\.5.0\bin;C:\Qt\Qt5.5.0\Tools\mingw492_32\bin;C:\32;C: |
Using the Qt static environment with Qt Creator
2.2 使用Qt Creator静态环境编译
This section describes how to use the static Qt environment from Qt Creator. Note that Qt Creator itself is part of the dynamic pre-built environment. By default, it builds the applications for this dynamic environment. But it is possible to configure it to additionally use the static Qt environment.
本节描述如何使用来自Qt Creator的静态Qt环境。请注意,Qt创建者本身就是动态预构建环境的一部分。默认情况下,它为这个动态环境构建应用程序。但是可以将其配置为另外使用静态Qt环境。
The following description is based on a Qt 5.5.0 / Qt Creator 3.4.2 installation, without additional customization of Qt Creator. Change the version numbers to adapt to your configuration.
以下描述基于Qt 5.5.0 / Qt Creator 3.4.2安装,不需要额外定制Qt Creator。更改版本号以适应您的版本配置。
Qt Creator setup
Qt Creator设置
Open the Qt Creator options edit box (menu "Tools" then "Options…"). Select "Build & Run".
打开Qt Creator选项编辑栏。
Go to tab "Qt Versions". In the "qmake location" table, there must be an "Auto-detected" part and a "Manual" part. In the "Auto-detected" part, there should be one line named "Qt 5.5.0 MinGW 32bit C:\Qt\Qt5.5.0\5.5\mingw492_32\bin\qmake.exe". The "Manual" part is initially empty.
在”Qt Versions”分页中,修改如上。….。手动部分最初是空的(译者:下面要添加)
Click "Add", browse to C:\Qt\Qt5.5.0\bin and select "qmake.exe". The version name is automatically set to "Qt 5.5.0 (5.5.0)". You should set a more meaningful name such as "Qt 5.5.0 MinGW Static 32bit"
添加手动部分。版本您可以修改为更容易识别的”Qt 5.5.0 MinGW Static 32bit”
Then go to tab "Kits". Again, there must be an "Auto-detected" part and an initially empty "Manual" part. Click "Add". Set a meaningful name such as "Desktop Qt 5.5.0 MinGW Static 32bit". In the "Qt version" field, select your static environment, named "Qt 5.5.0 MinGW Static 32bit" if you followed the above advice.
Then click "OK" to terminate the options setup.
到”kits”栏目。设置如上。最后点击OK完成设置。
Project setup
3. 程序项目设置
In each project you want to deploy with a static build, edit the project setup (select "Projects" in the left vertical pane).
在每个你需要静态部署的项目中,修改项目设置(在项目左边竖直栏那里)。
Click "Add Kit" in the upper left corner and select your static kit, named "Desktop Qt 5.5.0 MinGW Static 32bit" if you followed the above advice.
点击“Add kit”在左上角,选择您的静态kit。
The new kit for your project is configured. Note that, by default, there are distinct build directories for the dynamic and static versions of your project.
这样一个新的kit(套件)就设置好了。注意那个,项目的动态和静态版本位于不同的目录当中。
Now you can build your application in any combination of static vs. dynamic and release vs. debug. In the bottom part of the left pane, the project selector (the computer icon with the name of your project and either "Release" or "Debug") lets you select the combination you want.
Note that it is recommended to build only release versions of the application with the static environment. Avoid building a debug version of the application with the static environment. There are several reasons for that:
现在,您可以在静态与动态、发布与调试的任何组合中构建应用程序。在左侧窗格的底部,project selector(带有项目名称和“Release”或“Debug”的计算机图标)允许您选择所需的组合。请注意,建议只使用静态环境构建应用程序的发布版本。避免使用静态环境构建应用程序的调试版本。有几个原因:
原文地址:https://wiki.qt.io/Building_a_static_Qt_for_Windows_using_MinGW
本文适用于:已经安装Qt Creator,需要部署您的程序时的静态编译方法。
Fred Jing Hubble 西安 2018-11-29 QQ群556869219