Building Qt Static (and Dynamic) and Making it Small with GCC, Microsoft Visual Studio, and the Intel Compiler

This article will show you how to build Qt, the popular C++ framework from Nokia, so that it is both small and, if you prefer, available for static linking . Your Qt applications will be smaller, possibly faster, and can be distributable as a single executable.

Also answered: How small can Intel’s C++ compiler make a large library? How does Microsoft fare? Three compilers (settings tuned for small file output) and their resulting code size is compared.

 

A nice table of contents so that you can see what you’re getting into:

* optional

  1. Download the latest Qt source code and put it in its own directory.
  2. *Modify the compiler flags for use when building Qt.
  3. Open a command-line window for your compiler.
  4. Configure.
  5. Compile.
  6. *If you want to use static linking, modify your Qt project.

A few things to keep in mind before we get started:

Before getting started, you may wish to install a separate compiler. The full Qt SDK comes with G++3 as of this writing (G++ is the C++ compiler that comes with GCC). It works, but G++4 generates better code. I recommend the TDM release . Microsoft Visual Studio Intel’s compiler are also very capable. This article covers them all.

G++ and the Intel compiler are compatible. Either compiler can link to libraries built with the other compiler.
Microsoft’s C++ compiler is incompatible with the other two, so if you build Qt with it, you are stuck with Microsoft’s compiler for the whole project. This isn’t necessarily bad.  Visual C++ is a fine compiler, and finishes compiling Qt noticeably faster than the other two.

GCC is available free of charge, source code included.
Microsoft’s compiler is available for free in their “Express edition “. It is more or less fully functional.
Intel’s compiler, however, is only available free of charge on the Linux platform, and even then only for non-commercial software development. That said, it is widely thought of as producing the fastest code.

With that out of the way, let’s begin:

  1. Download the latest Qt source code and put it in its own directory.
    You can get the entire SDK or  just the source code . Either way, each compilation should be in a separate folder.
  2. *Modify the compiler flags for use when building Qt.
    You’ll be changing one line of the file “make.conf” in the “mkspecs” folder. If Qt in the “C:/Qt” folder, for example, then the file is “C:/Qt/mkspecs/COMPILER /make.conf”, where COMPILER is :
    win32-g++ ” for GCC
    win32-icc ” for the Intel compiler
    win32-msvc2008 ” for the Microsoft compiler (replace 2008 with the year of release).In all three cases, you will edit the line: “QMAKE_CFLAGS_RELEASE = “, changing the contents after the equals sign to the following:
    1. For GCC: -Os -momit-leaf-frame-pointer
    2. For ICC: -Os -Oy
    3. For VC++: -O1 -Og  -GL -MD

    For example, my ICC line shows: “QMAKE_CFLAGS_RELEASE    = -Os -Oy
    You may wish to add other optimizing flags as well. See your compiler’s documentation.

  3. Open a command-line window appropriate for your compiler .
    1. GCC: Open the command prompt (Start –> Run –> “cmd”), then run “mingwvars.bat”. For example, if GCC is installed in “c:/MinGW”, then enter the command: “c:/MinGW/mingwvars.bat
    2. Microsoft Visual Studio : Look for the “Visual Studio 2008 Command Prompt” entry in your start menu. It is usually found under “Programs/Microsoft Visual Studio 200X /Visual Studio Tools/”. The Express versions should be similar. Alternatively, search your hard drive for “vcvarsall.bat” and from that folder, run “vcvarsall.bat x86 ” from the command line.
    3. Intel compiler : Similar to Visual Studio, an icon is provided in your start menu, usually in: “Programs/Intel(R) Software Development Tools/Intel(R) C++ Compiler x.y.z “. Alternatively, find iclvars.bat and from that folder, run “iclvars.bat ia32
  4. Configure Qt.
    Change to the Qt folder. For example, type cd C:/Qt/4.6.3-msv c if you unzipped Qt to C:/Qt/4.6.3-msvc. This folder should have configure.exe within. Run the following long command:
    configure -release  -nomake examples -nomake demos -no-exceptions -no-stl -no-rtti -no-qt3support -no-scripttools -no-openssl -no-opengl -no-webkit -no-phonon -no-style-motif -no-style-cde -no-style-cleanlooks -no-style-plastique -no-sql-sqlite
    1. Add “-platform win32-??? ” for your compiler. Replace “???” with the name of your compiler, same as in step 2. You can also look for your compiler’s name in the Qt/mkspecs folder. For example, a Microsoft Visual Studio 2008 user would use “-platform win32-msvc2008 “.
    2. Add “-static ” if you are compiling for static linking (libraries included in your .exe rather than as separate files. This mostly applies to qtcore4.dll and qtgui4.dll )
    3. Add any static functionality your application needs., if you are compiling static For example, -qt-libjpeg -qt-zlib -qt-libpng for JPEG image, ZIP compression, and PNG (which also needs zlib) respectively.  See How to Statically Link Qt4 .
    4. Remove “-no-exceptions -no-stl -no-rtti ” if you need those C++ features. Note that with -no-stl , you are still able to use the ST L, but Qt will not have built-in convenience functions to, for example, set a QVector equal to a C++ vector. That is, you will have to do so manually.
    5. Replace “-release ” with “-debug-and-release ” if this will be your debug install, too. Do not use -static .
    6. The remaining “-no-foo ” options disable various features. See the output of “configure –help ” for documentation. You can write this to a file with “configure –help > options.txt “.
  5. Compile Qt .
    After step 4, you will be informed of which command starts the actual compile (usually mingw32-make or nmake ).
  6. (static only) Configure your project for use of the static library.

It’s the size that counts and how you use it.

When working towards making Qt (and therefore your projects) small, it is important to have some idea of what each compiler can do. I will compare the sizes of QtCore4.dll (core Qt functionality) and QtGui4.dll (Graphical interface libraries and operating system interface). These two files that are fairly representative of size scaling among Qt libraries, and also happen to be the two most common files needed by Qt applications.

Relative size of Qt libraries from three compilers

Interpretation:

  • ICC’s code size is about the same as GCC’s unoptimized. The sum of both file’s sizes is 11.38MB on both compilers.
  • With appropriate settings, GCC’s compiled Qt code size drops more than 40%, from 11.38MB to only 6.63MB combined.
  • Microsoft Visual C++ handily beats the other compilers, with a library size less than half of ICC’s and about 15% smaller than GCC’s best effort.(Edit : Arik notes that this compilation is still dependent on Microsoft’s msvcrtXX .dll . See his comment below regarding how to eliminate this dependency. This may change the size of your application).

Additionally, Microsoft’s compiler finished the job in about 1/5 the time of ICC and about 1/3 the time of GCC Optimized. I actually ran it twice because I thought it may have aborted without error, because the time difference was so substantial.

Update : I’ve compiled CNB ImageGuide to provide a real application size example. Using G++, executable size is 5.8MB. Using Microsoft C++ 2008 and Arik’s suggestion, it is 3.56MB, or about a 60% difference. I’d like to welcome theories as to why the difference is so substantial.

However :

  • This does not take into account the performance of the resulting libraries , with Intel likely taking the lead.
  • ICC is designed for absolute performance, not small code size.
  • MSVC is not available on most platforms supported by Qt.
  • The author is far more familiar with the compiler flags for G++ than for the other two compilers.  It is possible that a particular combination of options will significantly change the results.

I hope this has been helpful and informative. Communicate any feedback, good or bad, in the comments section.

Repeatability

Everyone is welcome to try to repeat, improve upon, or dispute my results. The settings used follow.

Intel Compiler 11

cflags : -Os -Oy
Qt configuration :
-release -qt-libjpeg -qt-zlib -qt-libpng -nomake examples -nomake demos -no-exceptions -no-stl -no-rtti -no-qt3support -no-scripttools -no-openssl -no-opengl -no-webkit -no-phonon -no-style-motif -no-style-cde -no-style-cleanlooks -no-style-plastique -no-sql-sqlite -platform win32-icc

Microsoft Visual C++ 2008

cflags : -O1 -GL -MD
Qt configuration :
-release -qt-libjpeg -qt-zlib -qt-libpng -nomake examples -nomake demos -no-exceptions -no-stl -no-rtti -no-qt3support -no-scripttools -no-openssl -no-opengl -no-webkit -no-phonon -no-style-motif -no-style-cde -no-style-cleanlooks -no-style-plastique -no-sql-sqlite -platform win32-msvc2008

GCC 4.4.1 default (TDM release 2)

cflags : -O2
Qt configuration :
-release -nomake examples -nomake demos -platform win32-g++

GCC 4.4.1 Optimized

cflags : -Os -mpreferred-stack-boundary=2 -finline-small-functions -momit-leaf-frame-pointer
Qt configuration :
-release -qt-libjpeg -qt-zlib -qt-libpng -nomake examples -nomake demos -no-exceptions -no-stl -no-rtti -no-qt3support -no-scripttools -no-openssl -no-opengl -no-webkit -no-phonon -no-style-motif -no-style-cde -no-style-cleanlooks -no-style-plastique -no-sql-sqlite -platform win32-g++

Edit 27 Jan 2010:

Memory footprint data for both executables (GCC and Microsoft compiled)

Memory footprint for Qt static compiled program using GCC 4.4

Memory footprint - Qt static compiled with GCC 4.4

Memory footprint for Qt static compiled program using Microsoft Visual Studio 2008

Memory footprint - Qt static compiled with MS Visual Studio 2008

Interpretation: The Microsoft-compiled static Qt executable seems to use about 10% less memory than the GCC compiled one.

Qt Creator note : If  you are using Qt Creator for your projects and have compile errors, deselect “use jom instead of nmake” under Tools –>

 

http://www.formortals.com/build-qt-static-small-microsoft-intel-gcc-compiler/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值