Talk about the size of the Qt program installation package, and a brief packaging guide

(This article is an article by the moderator of the KDE and Qt programming technology in the Mizuki community. I think it was well written and I reprinted it. The original address is: http://hgoldfish.com/blogs/article/103/ )

I often see some comments on the Internet that the Qt program is extremely large, even compared with the .NET program, saying that the Qt program is packaged with the .NET installation package. This has affected many people's choice of Qt. I think it is necessary to clarify this-

Obviously this statement is wrong! !

It is easy to understand that although Qt provides many components, not all components will be used by the program, and not all components need to be packaged into the program installation package. Taking Qt 5.7 as an example, a helloworld.exe program that can be used normally is not compressed but 20M. Using the lzma compression algorithm, the compression rate is 25%, and the compression is only 6M!

What about the rumors of Qt's hundreds of M capacity?

This matter starts from the beginning.

Qt4.4 introduced Webkit to facilitate programmers to embed HTML pages into their own programs. It didn't matter at first. If it is not actively included in the project anyway, the program installation package will not become larger. At that time, the Qt program was only 8M (compressed to 3M). In Qt5, Webkit was replaced with Chromium kernel Blink, and the name was changed to QtWebEngine. Qt developers found that the ICU used by Blink provided a more complete internationalization capability than the old version of Qt. Therefore, it is said that because of a misunderstanding, Qt developers forced to rely on ICU in Qt 5.0. This ICU contains a large amount of international meta-information, which has suddenly increased the capacity of nearly 30M. .

Another important reason for Qt's easy growth is QML introduced by Qt since 4.7. This is a modified language based on ECMAScript. Since then, Qt development has two schools, one uses the original C ++ language for development, and the other uses the QML language for development. Both share the same underlying facilities, such as various data structures and event loops QtCore and QtGui with OpenGL support. But at the upper level, Qt provides QtWidgets for C ++ developers, and provides independent QtQuick for QML developers.

QtQuick itself has only a simple layout language, and Qt provides QtControls on this basis to simulate the appearance of Android, iOS and Windows programs. The early Qt5 was not well designed, which led to the introduction of a lot of components that may be used by the program when using QML. So a helloworld comes down, it's quite huge. And it is very strange that if QtWebEngine is used in the form of QtWidgets / C ++, almost all QML modules will also be introduced. At that time, a simple program will reach the size of more than one hundred trillion!

There is a special problem with the Windows platform. Now Qt's graphics architecture calls OpenGL ES 2.0 for low-level rendering, but the newly installed Windows only supports OpenGL 1.1. In response to this problem, early Qt provided two options during the configure phase. One is to use the same ANGLE as Chromium , Translate OpenGL calls into DirectX calls. Both assume that the user's system has a graphics driver installed and can support OpenGL 2.0. The former needs to contain several ANGLE DLLs, directly increasing the capacity of 17M!

Fortunately, these things are now resolved.

First of all, the early version of Qt 5 already provided a configure option, you can not compile ICU into Qt, but the official download version still contains ICU by default, and later Qt directly removed the dependence on ICU. Now Qt will use ICU when it finds the DLL of ICU at runtime, otherwise it will call the system's API, and if it is not available, it will just make use of Qt's built-in algorithm to handle internationalization.

Secondly, some strange dependencies of QML are now removed. So whether you use QtWidgets or QML, there is only about 20M capacity when uncompressed.

Last one, how many users' computers do not have the graphics driver installed? Well, it seems that there really is a vmware virtual machine. But I think it is enough to directly use the OpenGL driver that comes with the system. Qt will now automatically detect the existence of ANGLE at runtime. If it exists, use ANGLE. If it does not exist, use the system's own OpenGL. You don't have to configure it in the configure stage as before. In addition, QtWidgets does not use OpenGL, so programs that use QtWidgets / C ++ can also remove all ANGLE DLLs.

So much to say, how to do it. Very simple. With Qt 5.7, a program called windeployqt.exe is now provided, which is located in the bin directory of Qt. Run this program, followed by the program name:

  1. Create a dist directory
  2. Copy the compiled qttest.exe to the dist directory.
  3. Open cmd.exe, cd to the dist directory
    4.1 Run c: \ qt5 \ bin \ windeployqt.exe qttest.exe
    4.2 For the QML program, you need to specify the QML directory: c: \ qt5 \ bin \ windeployqt.exe –qmldir d: \ qttest qttest.exe

At this time, you will see that qt has copied all the DLLs it needs. On this basis, I will remove some stuff as needed:

  1. libEGL.dll, libGLESV2.dll These two files are ANGLE files and can be removed. opengl32sw.dll is a software emulation of OpenGL, unless the user's system does not even have DirectX support-this is the case with the virtual machine environment-otherwise this file is completely useless. QtWidgets / C ++ programs do not use OpenGL, so just remove them. You can add "–no-angle" and "–no-opengl-sw" when calling windeployqt.exe.
  2. If svg is not used, the three files iconengines \ qsvgicon.dll, imageformats \ qsvg.dll, Qt5Svg.dll can also be deleted
  3. If there are no international users, translation files in translations can also be deleted.
  4. QtWidgets / C ++ is not used in QML programs, you can delete Qt5Widgets.dll
  5. If there are several image formats in the imageformats directory that are not used, they can also be deleted. I usually delete the entire directory myself. Qt has compiled png support. It is enough to read and write the icons contained in the program. Other formats are not important.
  6. qmltooling and Qt5Network.dll are used for QML debugging and can be deleted.

After the above cropping, after using 7zip to compress, a QtWidgets / C ++ helloworld program finally only has 5.64M, a QtControls program is 5.86M, and the QtQuick program will be smaller.

Note:

As of this writing, Qt 5.8 has been released. In this version, it no longer depends on OpenGL ES 2.0, but bypasses ANGLE and directly calls DirectX rendering. To Qt 5.10, it also directly supports the Vulkan API. However, it is still experimental at present, if you are interested, you can look at the description of configure.

The new Qt 5.8 rewrites the configure system to cut Qt more deeply. A minimum of 5M uncompressed can deploy a QML program.

Reference materials:

  1. Qt 5 ICU
    https://wiki.qt.io/Qt_5_ICU
    http://wiki.qt.io/Qt-5-QLocale

  2. Dynamically Loading Graphics Drivers
    http://doc.qt.io/qt-5/windows-requirements.html

  3. Qt Lite
    http://blog.qt.io/blog/2017/01/23/qt-5-8-released/

The original article without "reprint" in the title is copyrighted. Please indicate the source for reprinting: http://hgoldfish.com/blogs/article/103/ .

 
Published 315 original articles · praised 937 · 650,000 views

Guess you like

Origin blog.csdn.net/super828/article/details/78848681