一名热爱体感技术的
业余专业开发人员

Qt程序在Windows下的发布

弄qt的人知道,在windows下发布qt可真不容易,我之前少不更事,为了发布qt程序,业余时间试了一个月,复制了一堆qt安装目录下的东西才发布成功,但是仍旧一团迷惑。前两天去qt官网搜下如何在windows下发布qt程序,欣喜发现官网有一篇文章说了多种解决发布的办法,其中最后的使用qt文件目录bin下的windeployqt.exe程序可以完美解决QT的发布问题!也就是一行指令的事情。

官方原文阅读:Qt for Windows – Deployment

一篇简单的中文文章:win7平台下QT软件的打包与发布(部署与安装)

另外我觉得这篇文章也很好,可能有人会用到,Transition from Qt 4.x to Qt5

以下复制自官网原文:

Qt for Windows – Deployment

This documentation describes deployment issues for Windows. We demonstrate the procedures in terms of deploying the Plug & Paint application that is provided in Qt’s examples directory.

Static Linking

To build static applications, build Qt statically by configuring Qt with -static:

If you later need to reconfigure and rebuild Qt from the same location, ensure that all traces of the previous configuration are removed by entering the build directory and running nmake distclean ormingw32-make distclean before running configure again.

Linking the Application to the Static Version of Qt

As an example, this section will build the Plug & Paint example statically.

Once Qt finishes building, build the Plug & Paint application. First we must go into the directory that contains the application:

Run qmake to create a new makefile for the application, and perform a clean build to create the statically linked executable:

You probably want to link against the release libraries, and you can specify this when invoking qmake. Now, provided that everything compiled and linked without any errors, we should have a plugandpaint.exefile that is ready for deployment. To check that the application has the required libraries, copy the executable to a machine that does not have Qt or any Qt applications installed, and run it on that machine.

Remember that if your application depends on compiler specific libraries, these must still be redistributed along with your application. You can check which libraries your application is linking against by using thedepends tool. For more information, read the Application Dependencies section.

Since we cannot deploy plugins using the static linking approach, the application we have prepared is incomplete. It will run, but the functionality will be disabled due to the missing plugins. To deploy plugin-based applications we should use the shared library approach.

Shared Libraries

We have two challenges when deploying the Plug & Paint application using the shared libraries approach: The Qt runtime has to be correctly redistributed along with the application executable, and the plugins have to be installed in the correct location on the target system so that the application can find them.

Building Qt as a Shared Library

For this example, we assume that Qt is installed as a shared library, which is the default when installing Qt, in the C:pathtoQt directory.

Linking the Application to Qt as a Shared Library

After ensuring that Qt is built as a shared library, we can build the Plug & Paint application. First, we must go into the directory that contains the application:

Now run qmake to create a new makefile for the application, and do a clean build to create the dynamically linked executable:

This builds the core application, the following will build the plugins:

If everything compiled and linked without any errors, we will get a plugandpaint.exe executable and thepnp_basictools.dll and pnp_extrafilters.dll plugin files.

Creating the Application Package

To deploy the application, we must make sure that we copy the relevant Qt DLLs (corresponding to the Qt modules used in the application) and the Windows platform plugin, qwindows.dll, as well as the executable to the same directory tree in the release subdirectory.

In contrast to user plugins, Qt plugins must be put into subdirectories matching the plugin type. The correct location for the platform plugin is a subdirectory named platforms. Qt Plugins section has additional information about plugins and how Qt searches for them.

Qt relies on the ICU library for unicode support. You must include the ICU DLLs that are located in the bindirectory of your Qt installation if Qt was configured to use ICU. The Qt version bundled in the Qt5 package uses ICU, so deployment is needed there. The ICU DLLs are version dependent and have to match the ones your Qt version was linked against.

If ANGLE (the default) is used, you additionally need to include both libEGL.dll and libGLESv2.dllfrom Qt’s ‘lib’ directory as well as the HLSL compiler from DirectX. The HLSL compiler library, d3dcompiler_XX.dll, where XX is the version number that ANGLE (libGLESv2) was linked against.

If your application depends on Qt WebEngine, you must deploy <Qt install path>/bin/QtWebEngineProcess.exe to the application install path. If you chose to deploy the binary to a different path, set the QTWEBENGINEPROCESS_PATH environment variable to the binary’s absolute path (including its file name). This enables the application to find the binary and execute it for every instance of QWebEngineView or WebEngineView created. For example, a browser application with two tabs open should have two separate processes of QtWebEngineProcess.exe running. This is a common approach used by most modern web engines to provide a stable browsing experience.

Note: To support HTML5 videos, you must additionally deploy ffmpegsumo.dll (WebM codec plugin) into the qtwebengine directory under the application install path or under the path that the PluginsPathvariable was set to in <Qt install path>/<Qt version>/msvc2013/qt.conf.

Remember that if your application depends on compiler specific libraries, these must be redistributed along with your application. You can check which libraries your application is linking against by using thedepends tool. For more information, see the Application Dependencies section.

We’ll cover the plugins shortly, but first we’ll check that the application will work in a deployed environment: Either copy the executable and the Qt DLLs to a machine that doesn’t have Qt or any Qt applications installed, or if you want to test on the build machine, ensure that the machine doesn’t have Qt in its environment.

If the application starts without any problems, then we have successfully made a dynamically linked version of the Plug & Paint application. But the application’s functionality will still be missing since we have not yet deployed the associated plugins.

Plugins work differently to normal DLLs, so we can’t just copy them into the same directory as our application’s executable as we did with the Qt DLLs. When looking for plugins, the application searches in aplugins subdirectory inside the directory of the application executable.

So to make the plugins available to our application, we have to create the plugins subdirectory and copy over the relevant DLLs:

An archive distributing all the Qt DLLs and application specific plugins required to run the Plug & Paintapplication, would have to include the following files:

Component File Name
The executable plugandpaint.exe
The Basic Tools plugin pluginspnp_basictools.dll
The ExtraFilters plugin pluginspnp_extrafilters.dll
The Qt Windows platform plugin platformsqwindows.dll
The Qt Core module Qt5Core.dll
The Qt GUI module Qt5Gui.dll
The Qt Widgets module Qt5Widgets.dll

In addition, the archive must contain the following compiler specific libraries depending on your version of Visual Studio:

VC++ 8.0 (2005) VC++ 9.0 (2008) VC++ 10.0 (2010)
The C run-time msvcr80.dll msvcr90.dll msvcr100.dll
The C++ run-time msvcp80.dll msvcp90.dll msvcp100.dll

If ICU was used, the archive must contain:

File Name
icudtXX.dll icuinXX.dll icuucXX.dll

Finally, if ANGLE was used, then the archive must additionally contain:

File Name
libEGL.dll libGLESv2.dll d3dcompiler_XX.dll

To verify that the application now can be successfully deployed, you can extract this archive on a machine without Qt and without any compiler installed, and try to run it.

An alternative to putting the plugins in the plugins subdirectory is to add a custom search path when you start your application using QApplication::addLibraryPath() or QApplication::setLibraryPaths().

One benefit of using plugins is that they can easily be made available to a whole family of applications.

It’s often most convenient to add the path in the application’s main() function, right after the QApplicationobject is created. Once the path is added, the application will search it for plugins, in addition to looking in the plugins subdirectory in the application’s own directory. Any number of additional paths can be added.

Manifest files

When deploying an application compiled with Visual Studio 2005 onwards, there are some additional steps to be taken.

First, we need to copy the manifest file created when linking the application. This manifest file contains information about the application’s dependencies on side-by-side assemblies, such as the runtime libraries.

The manifest file needs to be copied into the same folder as the application executable. You do not need to copy the manifest files for shared libraries (DLLs), since they are not used.

If the shared library has dependencies that are different from the application using it, the manifest file needs to be embedded into the DLL binary. Since Qt 4.1.3, the following CONFIG options are available for embedding manifests:

To use the options, add

to your .pro file. The embed_manifest_dll option is enabled by default. The embed_manifest_exeoption is NOT enabled by default.

You can find more information about manifest files and side-by-side assemblies at the MSDN website.

The correct way to include the runtime libraries with your application is to ensure that they are installed on the end-user’s system.

To install the runtime libraries on the end-user’s system, you need to include the appropriate Visual C++ Redistributable Package (VCRedist) executable with your application and ensure that it is executed when the user installs your application.

For example, on an 32-bit x86-based system, you would include the vcredist_x86.exe executable. Thevcredist_IA64.exe and vcredist_x64.exe executables provide the appropriate libraries for the IA64 and 64-bit x86 architectures, respectively.

Note: The application you ship must be compiled with exactly the same compiler version against the same C runtime version. This prevents deploying errors caused by different versions of the C runtime libraries.

Manual installations with Visual Studio 2008 and 2010

As well as the above details for VS 2005 and onwards, Visual Studio 2008/2010 applications may have problems when deploying manually, say to a USB stick.

The recommended procedure is to configure Qt with the -plugin-manifests option using the ‘configure’ tool. Then follow the guidelines for manually deploying private assemblies.

In brief the steps are

  1. create a folder structure on the development computer that will match the target USB stick directory structure, for example ‘app’ and for your dlls, ‘applib’.
  2. on the development computer, from the appropriate ‘redist’ folder copy over Microsoft.VC80.CRT and Microsoft.VC80.MFC to the directories ‘app’ and ‘applib’ on the development PC.
  3. xcopy the app folder to the target USB stick.

Your application should now run. Also be aware that even with a service pack installed the Windows DLLs that are linked to will be the defaults. See the information on how to select the appropriate target DLLs.

Application Dependencies

Additional Libraries

Depending on configuration, compiler specific libraries must be redistributed along with your application.

For example, if Qt is built using ANGLE, its shared libraries and the required shared libraries of the Direct X SDK need to be shipped as well.

You can check which libraries your application is linking against by using the Dependency Walker tool. All you need to do is to run it like this:

This will provide a list of the libraries that your application depends on and other information.

When looking at the release build of the Plug & Paint executable (plugandpaint.exe) with the dependstool, the tool lists the following immediate dependencies to non-system libraries:

Qt VC++ 10.0 (2010) VC++ 11.0 (2012) VC++ 12.0 (2013) MinGW
  • QT5CORE.DLL – The QtCoreruntime
  • QT5GUI.DLL – TheQtGui runtime
  • QT5WIDGETS.DLL – The QtWidgetsruntime
  • MSVCR100.DLL – The C runtime
  • MSVCP100.DLL – The C++ runtime
  • MSVCR110.DLL – The C runtime
  • MSVCP110.DLL – The C++ runtime
  • MSVCR120.DLL – The C runtime
  • MSVCP120.DLL – The C++ runtime
  • LIBWINPTHREAD-1.DLL
  • LIBGCC_S_DW2-1.DLL
  • LIBSTDC++-6.DLL

When looking at the plugin DLLs the exact same dependencies are listed.

Qt Plugins

All Qt GUI applications require a plugin that implements the Qt Platform Abstraction (QPA) layer in Qt 5. For Windows, the name of the platform plugin is qwindows.dll. This file must be located within a specific subdirectory (by default, platforms) under your distribution directory. Alternatively, it is possible to adjust the search path Qt uses to find its plugins, as described below.

Your application may also depend on one or more Qt plugins, such as the print support plugin, the JPEG image format plugin or a SQL driver plugin. Be sure to distribute any Qt plugins that you need with your application. Similar to the platform plugin, each type of plugin must be located within a specific subdirectory (such as printsupport, imageformats or sqldrivers) within your distribution directory.

Note: If you are deploying an application that uses Qt WebKit to display HTML pages from the World Wide Web, you should include all text codec plugins to support as many HTML encodings possible.

The search path for Qt plugins is hard-coded into the QtCore library. By default, the plugins subdirectory of the Qt installation is the first plugin search path. However, pre-determined paths like the default one have certain disadvantages. For example, they may not exist on the target machine. For that reason, you need to examine various alternatives to make sure that the Qt plugins are found:

If you add a custom path using QApplication::addLibraryPath it could look like this:

Then qApp->libraryPaths() would return something like this:

  • C:/customPath/plugins
  • C:/Qt/%VERSION%/plugins
  • E:/myApplication/directory

The executable will look for the plugins in these directories and the same order as the QStringList returned by qApp->libraryPaths(). The newly added path is prepended to the qApp->libraryPaths() which means that it will be searched through first. However, if you use qApp->setLibraryPaths(), you will be able to determine which paths and in which order they will be searched.

The How to Create Qt Plugins document outlines the issues you need to pay attention to when building and deploying plugins for Qt applications.

The Windows Deployment Tool

The Windows deployment tool can be found in QTDIR/bin/windeployqt. It is designed to automate the process of creating a deployable folder that contains all libraries, QML imports, plugins, translations that are required to run the application from that folder. This is used to create the sandbox for Windows Runtime or an installation tree for Windows desktop applications that can be easily bundled by an installer.