Hello,
There is a number of requests and answers in many forums about the way to compile a static version of Qt. When I was looking for a method to do so on Windows, I found many clues but no complete off-the-shelf script to do this. Since I prefer automated processes for administrative tasks, I wrote a script to do so and I share it using this post.
Why a static Qt?
On Windows, the pre-built environments from qt-project.org are dynamically linked. IMHO, this remains 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 install. Even using the classical “DLL Dependency Walker”, 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.
Building script
I had this requirement in a small personal open-source project named QtlMovie [qtlmovie.sourceforge.net] and, as part of this project, I wrote a script which downloads, builds and installs a static version of 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 the script takes advantage of that.
The script is available in the git repository of QtlMovie at https://sourceforge.net/p/qtlmovie/code/ci/master/tree/build/windows-build-qt-static.ps1 [sourceforge.net]
Note that I use MinGW for both the pre-built and static environment. I have not used MS Visual Studio and I have not written a corresponding version of the script for this environment.
If you have similar needs as mine, you may want to try it or review it. Any feedback is welcome.
Requirements
First, a pre-built Qt environment for MinGW must be installed. Preferably use the same version for both dynamic and static versions of Qt.
This script uses Windows PowerShell. The 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 [microsoft.com]
This script also requires 7-Zip [7-zip.org] to uncompress the downloaded Qt source code.
Invocation
The PowerShell script can be directly invoked from the Windows Explorer if you are ok with the default parameter values (see below).
Alternatively, you can invoke it from the PowerShell prompt using the following syntax;
windows-build-qt-static.ps1 [[-QtSrcUrl] <Object>] [[-QtStaticDir] <Object>] [[-QtVersion] <Object>] [[-MingwDir] <Object>] [-NoPause]
Description
This scripts downloads Qt source code, compiles and installs a static version of Qt. It assumes that a prebuilt Qt / MinGW environment is already installed, typically in C:\Qt. This prebuilt environment uses shared libraries. It is supposed to remain the main development environment for Qt. This script adds a static version of the Qt libraries in order to allow the construction of standalone and self-sufficient executable.
This script is typically run from the Windows Explorer.
Parameters
-QtSrcUrl <Object>
URL of the Qt source file archive. By default, use the latest identified version (Qt 5.2.0-rc1 at the time of this post). You may want to modify the script using another URL / version.
-QtStaticDir <Object>
Root directory where the static versions of Qt are installed. By default, use C:\Qt\Static.
-QtVersion <Object>
The Qt version. By default, this script tries to extract the version number from the Qt source file name.
-MingwDir <Object>
Root directory of the MinGW prebuilt environment. By default, use the version which was installed by the prebuilt Qt environment.
-NoPause [<SwitchParameter>]
Do not wait for the user to press <enter> at end of execution. By default, execute a “pause” instruction at the end of execution, which is useful when the script was run from Windows Explorer.
Using the Qt static environment
To build your static applications, set up the environment variables to the static environment. For instance, using Qt 5.2.0-rc1, wherever you use C:\Qt\Qt5.2.0\5.2.0-rc1\mingw48_32 for the dynamic environment, use C:\Qt\Static\5.2.0-rc1 for the static environment.
Also add CONFIG+=static to all your qmake command lines.
Note that the static environment contains all libraries and command line tools (qmake, lupdate, lrelease, etc) but not the GUI tools (designer, assistant, creator, linguist, etc). The GUI tools are essentially used in the development process and the pre-built dynamic environment remains the preferred one. On the other hand, the command line tools are required in scripts to build the final static application.
↧