Silmor . de
Site Links:
Impressum / Publisher

Compiling Qt 5.0.0 with MinGW

...meditations on an empty box with Windows 8...

By Konrad Rosenbaum, anno 2012 on the 31st day of December.

Prologue

Since Qt 5.0.0 has been released a few days ago it seems prudent to move all my Qt4 based software over to the new version. Using Linux this is a comparatively easy port:

Qt5 gained a few dependencies over Qt4 - notably XCB (instead of plain old XLib and you now have to mention -xcb to the configure script or your programs refuse to show up on screen for lack of an output plugin) and ICU for Unicode support. The Building Qt 5 from Git page pretty much sums up what you have to do to get it to work.

Qt5 retains most of the API of Qt4, so porting your own programs is a simple matter of replacing a few obsolete method calls with new ones (most of my effort was to replace QString::toAscii() with QString::toLatin1()) and renaming a few method calls (e.g. QFileDialog::setFilter(const QString&) with QFileDialog::setNameFilter(const QString&) - which was a bit tricky, since Qt5 retains another QFileDialog::setFilter(QDir::Filters) method).

All-in-all I ported a project of 35000 lines of code within less than 3 hours with no particular hurry (I'm on vacation after all!). And without using any special tools (I would have created some scripts north of 4 hours).

So far I find that the port to Qt5 is well worth it. Not only will Qt4 not receive any new features, Qt5 also intoduces a few new features like QML and support for C++11 (the new C++ standard with such niceties as lambda-expressions or range-based loops).

Options ...?

So I will not spend any time on explaining how to compile/install Qt5 on Linux, since this is already sufficiently done in the Qt-Wiki. Drop me a note with specific questions if you think otherwise (you'll find my mail address at the bottom of this page)..

I will not spend any thoughts on MacOS either. Unless someone tells me how to compile Qt5 with a vanilla GCC 4.7 or an unmodified clang from the LLVM site (XCode 4.4/4.5 does not install on Snow Leopard, so all I have is an old Apple GCC 4.2, an unmodified GCC 4.7 that does not compile the Apple dialect of Objective C++ and a vanilla clang 3.2 whose libc++ does not want to compile).

Instead I'll focus on Windows, since it is well-supported, but a bit tricky to handle.

You have several compiler options on Windows, each one with their distinctive pros and cons:

MinGW/GCC has a few positives on its side: it is a platform independent compiler, so if you use it on Linux and Windows you will have to deal only with one compiler and its quirks. It produces decent code and has excellent support for C++11. If you write Open Source/Free Software you have absolutely no problems redistributing any DLLs that the compiler forces on you. Even if you write commercial/proprietary software you do not have any licensing problems, since the Runtime Exception of GCC allows you to distribute the runtime DLLs together with your own software under terms of your own choice (see the licenses/gcc/COPYING.RUNTIME file in your MinGW folder for details). It has the downside that WebKit does not work in Qt 5.0.0 - but this will be remedied in Qt 5.0.1 when the necessary patches are in. Another downside is that compiling with GCC on Windows is relatively slow: the version of make delivered with MinGW is not capable of using more than one CPU core (unless started from an MSys Shell) and GCC is quite slow on Windows (both GCC on Linux and MSVC on Windows are significantly faster).

All versions of MSVC have the advantage of producing faster code, but you will only notice this if you do computation heavy work in C++. A normal GUI application will not benefit from the more aggressive optimizations of MSVC. You will however notice the speed at which your own application compiles: it is much higher than with GCC, since MSVC is less demanding to a Windows file system (you should consider MSVC if Windows is your primary development workstation). They all have a major drawback for Open Source/Free Software (at least if you are using the GPL): you cannot ship the runtime DLL with your program, you have to hope it is there or ask your users to download and install the "MSVC redistibutable" themselves (see the GPL FAQ).

MSVC2010 makes it very easy for you to get started, just install the pre-compiled binary package and start Qt Creator, the remainder works as if by magic (once you have any version of MSVC2010 installed, even the Express edition will do). You can stop reading here if you can live with this option. I also recommend you use this option if you are new to Qt and are normally at home in Windows - use it for a while and proceed to other options once you have come up to speed or are bored enough to try something new.

MSVC2012 requires that you recompile Qt. The process is similar to compiling with MinGW, but you can use pre-compiled binaries for ICU and OpenSSL, or alternatively you need to make some minor adjustments to the compiling process described below.

A final note: you can install all of those in parallel. I run a virtual machine with Windows 8 that has two versions of MinGW (both 4.7, but 32bit and 64bit hosts+targets) and two versions of MSVC (2010 and 2012, both Express Editions) installed in relative harmony.

As an overview a comparison between the compiler options that you have:

FeatureMinGW/GCC 4.7MSVC2010MSVC2012
comes pre-compilednoonly 32bitno
can compile 32bit and 64bityesyes
can compile 32bit on 64bit systemyesyes
can compile 64bit on 32bit systemyes, but cross-compiling needs customizationsyes
execution speed of programsokgreat
compiler speeddamn slowhigh
Qt 5.0.0 Webkit worksnoyes
all other Qt 5.0.0 modules workyesyes
C++11 supportgood, almost completepretty badbetter, but still mediocre
can distribute runtime DLL with Open Sourceyesno
can distribute runtime DLL with commercial softwareyesyes

Solutions!

Let's go with MinGW then. I recommend you still start by downloading and installing the pre-packaged Qt5 SDK. This way you'll have a working Qt-Creator and you can look at the MSVC2010 target to compare what it should like after installation in case of trouble (you can delete that directory later if you want to save a bit of space). Also one warning ahead: this experiment will require the installation of about a dozen packages (some of them quite large) and will require several GB of disk space (at least 13GB if you follow my recipe for just one target, 3GB more for the second target).

All of this will require a bit of very 'Unixy' fiddling that not all Windows users may feel up to - but, heck, you want to use MinGW instead of MSVC anyway! Right?

So here is my "recipe" for compiling Qt 5.0.0 yourself:

  1. Start by carefully reading Building Qt 5 from Git (it refers to a GIT checkout of Qt, but you can use the sources delivered with the VS2010 package too - configure.bat is in the qtbase subdirectory then)
  2. Download MinGW as described on the Qt Wiki - use the 32bit package if you target all Windows-Versions and the 64bit package if you target 64bit versions only (you can do both and in theory each version can cross-compile for both targets, but it is easier to just use their respective default targets)
  3. Download MSys from the MinGW repository: - it runs on both 32bit and 64bit systems and in conjunction with both versions of MinGW, you have to modify etc/fstab to let /mingw point to the correct path
  4. Download, Patch and Compile ICU as described on the ICU page of the Qt wiki
  5. Download and Compile OpenSSL the same way (you need OpenSSL for encrypted network connections and I recommend compiling it yourself to avoid dependencies on VC2010 DLLs)
  6. Install the DirectX SDK, otherwise Qt will not be able to compile ANGLE - which is needed to translate the use of OpenGL to DirectX, which means QML and QGLWidget will not work
  7. Download Qt5.0.0 sources either from GIT, copy the ones from the pre-built package or download the ZIP source packages from http://qt-project.org; if you plan to use GIT:
    1. Download Tortoise GIT (much easier to use than plain GIT) from Google Code
    2. Download Portable GIT (used to be called MSysGIT) from Google Code - use the Portable GIT package, the MSysGIT package is for GIT developers only
    3. Install Portable GIT and then Tortoise GIT, when asked: make GIT available in the path, but not the MSys environment of GIT
    4. Install ActivePerl (http://www.activestate.com/activeperl)
    5. GIT clone git://qt.gitorious.org/qtsdk/qtsdk.git into a fresh directory
    6. open cmd.exe (Win-R, cmd.exe, Return), change into the checked out directory
    7. switch to the right tag:
      git checkout -b local-qt5.0.0 v5.0.0
    8. use ActivePerl to call init-repository:
      C:\Perl\bin\perl init-repository -no-webkit
      make sure to use ActivePerl, since GIT may have injected an incompatible version of Perl into your PATH;
      this will clone all the sub-projects of Qt (except for WebKit) and may take a while;
      depending on your network connection you may have time for a coffee or an extended lunch;
      for versions newer than Qt 5.0.0 you can omit the -no-webkit switch and include Webkit in your download and compilation
  8. To avoid problems with Qt 5.0.0 and MinGW: delete the qtwebkit and qtwebkit-examples-and-demos folders (skip this for newer versions of Qt)
  9. delete the qt-creator folder if you do not want to build it yourself (building Qt takes long enough!)
  10. Install ActivePerl, Python and Ruby (needed by various parts of the build-process)
  11. Build Qt, make sure the right version of MinGW/Perl/Python/Ruby is in your path, you'll need these options to configure.bat:
    • -opensource - mark it as the Open Source version (or -commercial if you are a commercial licensee)
    • -prefix D:\Qt\Qt5.0.0\5.0.0\mingw64 (that's what I used to inject the binaries into my regular installation, built for 64bit, feel free to chose any other path, make sure it does not contain spaces)
    • -nomake examples -nomake demos -nomake tests (unless you feel like you have too much time to waste, like a few days)
    • -platform win32-g++ (pin it on MinGW, otherwise it will happily use a parallel installation of VS201*)
    • -c++11 (we are, like, modern - right? - actually you need this for the new signal-slot-connections to work properly, I highly recommend it)
    • -opengl desktop (otherwise QGLWidget and QML/QtQuick will not work)
    • -openssl -I d:\openssl\include -L d:\openssl\lib (the -I and -L switches are needed so Qt can actually find OpenSSL, leave all of this out if you do not want to use it)
    • -icu -I d:\icu\dist\include -L d:\icu\dist\lib (same for the Unicode library, but you do need it!)

The compile process for Qt on MinGW only uses one CPU core. Sadly, since the cmd.exe shell of Windows does not have proper job control. So if you make two copies of the Qt sources and bother to compile two versions of icu and openssl you can do a 32bit and 64bit compile of Qt in parallel - they just must not touch the same directories at the same time, otherwise it is quite reasonable.

Once the compile is under way: take the day off, see you kids or parents - have a nice day and check back a few hours later. I left my computer compiling all evening and throughout the night - it took at least 6 hours, possibly lots more.

Random Notes

Never ever compile on a network drive! It will take weeks instead of hours! GCC is very I/O-intensive (it reads and writes files in small chunks) and Windows is (still) very stupid about I/O optimizations. GCC assumes an optimized file system layer in the underlying OS - a Linux kernel will easily optimize by reading ahead for the next few lines that GCC will want to read and buffer data that GCC wants to write until it has collected enough to warrant looking for the right sector on the disk. Windows is still the only Desktop/Server system that does not have a proper file system cache - even in Windows 8 and after dozens of other systems have shown how to do it for decades.

You can easily support two versions of MinGW by creating a little batch file to initialize your environment, for example this is what I use for my 32bit environment (D: is my projects disk, this file is D:\mingw32\DevEnv.bat):

@echo off
rem above: switch command echo off to be less annoying
echo Initializing MinGW 32bit Environment.

rem Ruby is needed for some Qt configuration
rem ...also Python and ActivePerl 
rem ...which are normally already in PATH
rem for normal development it would not hurt
rem to leave them out of PATH though...
set PATH=c:\Ruby193\bin;%PATH%

rem ICU and OpenSSL for features
set PATH=d:\openssl-x86\bin;d:\icu32\bin;d:\icu32\lib;%PATH%

rem MinGW and Qt itself...
set PATH=d:\mingw32\bin;d:\Qt5.0.0\mingw32\bin;%PATH%

rem output for comparison
echo PATH: %PATH%

rem go to my development directory
d:
cd\

rem done: go back to normal shell mode
echo Ready.
@echo on
You then link this file with a shortcut to the target %comspec% /k ""D:\mingw32\DevEnv.bat"" - this will start a version of cmd.exe that initializes itself with this batch file and then becomes interactive. The "set PATH" lines extend the PATH variable to include all directories that contain executables and DLLs that I normally need to compile Qt5 programs (in this case for 32bits) - make sure you use the right ones for the kind of target you want (clean 32bit or clean 64bit).


Webmaster: webmaster AT silmor DOT de