You are on page 1of 2

Building Qt Static (and Dynamic) and Making it Small with GCC, Microsoft Visual

Studio, and the Intel Compiler
A nice table of contents so that you can see what you’re getting into:
* optional
Download the latest Qt source code and put it in its own directory.
*Modify the compiler flags for use when building Qt.
Open a command-line window for your compiler.
Configure.
Compile.
*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++ compi
ler 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 artic
le covers them all.
G++ and the Intel compiler are compatible. Either compiler can link to libra
ries built with the other compiler.
Microsoft’s C++ compiler is incompatible with the other two, so if you build Q
t 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 c
ompiling 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 platf
orm,
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:
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 compil
ation should be in a separate folder.
*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\COMP
ILER\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 relea
se).
In all three cases, you will edit the line: “QMAKE_CFLAGS_RELEASE =“,
changing the contents after the equals sign to the following:
For GCC: -Os -momit-leaf-frame-pointer
For ICC: -Os -Oy
For VC++: -O1 -Og -GL -MD

For example. This folder should have configure..6. You can also look for your compiler’s name in the Qt \mkspecs folder. type cd C:\Qt\4. but Qt will not have bui lt-in convenience functions to. my ICC line shows: “QMAKE_CFLAGS_RELEASE = -Os -Oy” You may wish to add other optimizing flags as well. Replace “???” with the name of your compiler. Compile Qt. Open a command-line window appropriate for your compiler. too. For example. Do not use -static.bat and from that folder.bat“ Microsoft Visual Studio: Look for the “Visual Studio 2008 Command Prompt” en try in your start menu. Replace “-release” with “-debug-and-release” if this will be your debug install.dll and qtgui4. Remove “-no-exceptions -no-stl -no-rtti” if you need those C++ features. and PNG (which also needs zlib) respectively. you will be informed of which command starts the actual compil e (usually mingw32-make or nmake). same as in step 2. -qt-libjpeg -qt-zlib -qt-libpng for JPEG image. you will have to do so manually. See How to Statically Lin k Qt4. set a QVector equal to a C++ vector .For example.dll) Add any static functionality your application needs. then enter the comma nd: “c:\MinGW\mingwvars. if you are compili ng static For example.bat”.exe rather than as separate files. Alternatively. if GCC is installed in “c:\MinGW”. That is. for example. run “iclvars . For example. an icon is provided in your st art menu. then run “mingwvars. See your compiler’s docume ntation. Alternatively. (static only) Configure your project for use of the static library. After step 4. GCC: Open the command prompt (Start –> Run –> “cmd”).3-msvc. The remaining “-no-foo” options disable various features. You can write this to a file with “configure –help > options.bat ia32“ Configure Qt.txt“. usually in: “Programs\Intel(R) Software Development Tools\Intel(R) C++ Compiler x. find iclvars.bat” and from that folder.bat x86” from the command line. Run the following long com mand: configure -release -nomake examples -nomake demos -no-exceptions -no-stl -n o-rtti -no-qt3support -no-scripttools -no-openssl -no-opengl -no-webkit -no-phon on -no-style-motif -no-style-cde -no-style-cleanlooks -no-style-plastique -no-sq l-sqlite Add “-platform win32-???” for your compiler. Intel compiler: Similar to Visual Studio.z“. The Express versions should be similar. Not e that with -no-stl.3-msvc if you unzipp ed Qt to C:\Qt\4.exe within. run “vcvarsall. ZIP compr ession. you are still able to use the STL. See the output of “c onfigure –help” for documentation. It is usually found under “Programs\Microsoft Visual Studio 200X\V isual Studio Tools\”. . a Microsoft Visual Studio 2008 user would use “-plat form win32-msvc2008“. This mostly applies to qtcore4.6. Change to the Qt folder. search yo ur hard drive for “vcvarsall.y. Add “-static” if you are compiling for static linking (libraries included in your .