Basics of Qt (III): master the basic static Compilation Method of Qt4, qtqt4

Source: Internet
Author: User

Basics of Qt (III): master the basic static Compilation Method of Qt4, qtqt4

For reprint, please indicate the source:CN_Simo.

Lead:

Static Compilation has been mentioned in the previous two chapters. In Windows, the next Static Compilation takes about three hours to complete. It is also very easy to cause errors for various reasons. So why do we have to spend the time doing this! I just wanted to use Qt to make an exe card and send it to my classmates. But normally, the exe we made requires various dll dynamic link files to run properly (compress and package it to others? Directly sending an exe file is too lazy to open, not to mention the need to decompress it), then I want to be able to simply need an exe?
The role of "static compilation" is to lift the exe from the dll constraint, but at the same time, your exe may be several times larger than before (no way, everything is absolutely perfect ). Preface here, we will go to the text:

Thoughts: Let's start: Compilation preparation: required environment:

The better the configuration, the faster it will be. I compiled the configuration for one morning.

Required files:
  • Qt-everywhere-opensource-src-4.8.6.zip
  • Qt installed in tutorial 1 (available in version 4 or 5)
Preliminary work:

 

Step 1: configure the compiling file qmake. conf

I decompress the zip file to the E drive, qmake. conf file is in the source directory \ mkspecs \ win32-g ++, open with notepad to modify the following parameters:QMAKE_LFLAGSAndQMAKE_LFLAGS_DLL. The parameter value is-static, for example:

After the modification, I create a new "qt4.8.6 _ static" folder on the E drive as the compilation output directory, and copy the changed "mkspecs" folder to the qt4.8.6 _ static folder. If you do not perform this operation step 3, an error may be reported, similar to the following:

Could not find mkspecs for your QMAKESPEC(win32-g++) after trying:E:/qt/qt4.8.6_static\mkspecs
Step 2: Set environment variables and required compilation content

Open the Qt command window in the Start menu, and run the cd command to enter the source code directory:

e:&cd e:\qt-everywhere-opensource-src-4.8.6

All the following operations are performed in the local directory.

Enter the following two commands:

 

set QMAKESPEC=win32-g++configure -confirm-license -opensource -prefix “F:\Qt\Qt_4.8.6_static” -debug-and-release -static -platform win32-g++ -nomake demos -nomake examples -nomake tests -fast -mp

 

  • Set command to set environment variables (Note: When the window is closed, the settings will be cleared. You need to reset the settings.)
  • The configure command configures the compilation content. parameter descriptions:


After you press enter, the following information is displayed, indicating that the process is complete:

Start Compilation:
mingw32-make

Enter the command to start compilation. This process is the most error-prone. because of the long duration, you may encounter many problems that you cannot solve. At this time, you can only start again. If no error occurs, it may take more than three hours to complete (see my computer configuration). you can do other things, in the compilation process, it is best not to use a computer to do other tasks that occupy a large amount of system resources, because doing so will greatly increase the chance of failure.

Compilation ended:

After that, enter the following command to start a bunch of copy commands. Then you can see that the qt4.8.6 _ static directory has started to expand sharply.

mingw32-make install

The copy can be completed in about 15 minutes.
YesNote:,If you move or rename the qt4.8.6 _ static directory, the program in the bin directory will be abnormal.To solve this problem:
Create a qt. conf file in the bin directory. The content is written as follows:

[paths]Prefix = ..

In this way, you can move or rename the image.

Configure Qt Creator:

Open qt creator, [tools]-[Options]-[build and run], and add the compiled static version qt4.8.6 (Qmake.exe), And then add a new build kit. Select qt4.8.6 for the qt version. The name is random, for example:

Hello World test:

[New] a project. Select the added Building Suite. Here, I will test the helloworld project in tutorial 2.
After the helloworld project is opened, crlt + 5 will add the new build Kit, click the "deploy build Kit" button in the lower left corner to select the release version of the original Kit and the static version Kit to run the project, respectively, A few seconds later, you can see the helloworld running interface:

Exe file comparison:

Looking for the Directory generated by exe and finding two exe files, I was surprised to find this result:

(The right side is the program compiled by our qt static library, and the left side is the original)

Result:

  • In the same release version, the size of the helloworld program is not at the same level (one KB level, one MB level ),Static compilation failure;
  • To open a dynamically compiled exe file, you need support for dll dynamic link files. The exe file compiled by static links can be opened directly without the limitations of dll files,Static compilation wins;
  Summary:

Through the above results, we can see that both dynamic compilation and static compilation have their respective advantages and disadvantages. We should select what we need as appropriate;
The longer the layer of qt source code compilation is, the easier it is to get errors. You can select a computer with better configuration to increase the compilation speed;
Through the knowledge in this chapter, we may feel troublesome and interesting in our hearts. If you are the latter, we hope that you will be able to learn better in the future.
Let's look forward to the next content [@ CN_Simo]!

Conclusion:

Previous: Basics of Qt (II): Analysis of Qt Project Creation, compilation, running, and release processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.