compiling Qt 4.3.5 with gcc 4 on Windows

Preface

Trolltech’s Qt open source version for Windows comes compiled with gcc 3 for Windows, using the official MinGW stack – and these libraries won’t link properly with gcc 4 compiled libraries. Unfortunately Trolltech insists that gcc 4 compilation on Windows is ‘alpha’ and ‘not supported’, etc. – even though Qt is compiled with gcc 4 on both Linux and Mac officially by them. For the life of me I don’t get why they insist on using a compiler that was end-of-lifed two years ago (the last and final version of gcc 3 came out March 2006), and why don’t they embrace the new compiler version that is already 3 years old (gcc 4.0.0 came out Aprli 2005).

Issues with compiling Qt with gcc 4 on Windows have been noted several times into the Qt bugtracker, and also documented on various forums. The good news is that one can compile it just fine, but one has to make simple manual adjustments to two of the source code files.

The Quick Fix

If you’re not really interested in compiling Qt for gcc4, just want to use the results, simply download the zip file with the binaries I created by following the steps described below. Unzip into a convenient location, say C:\usr\qt-all-opensource-src-4.3.5 & edit the following included bin\qt.conf file to reflect the location you unzipped into, e.g.:

[Paths]
Prefix=C:/usr/qt-all-opensource-src-4.3.5

Make sure to use forward slashes in this config file.

Preparations

Set up the toolchain

To compile Qt with gcc 4 on Windows, of course you have to have gcc 4. This is easy – just download nuwen’s MinGW distribution and unzip it – this contains a recent version of gcc 4 that you can use. We’ll assume that you’ve put MinGW under C:\MinGW .

There’s an anomaly in the MinGW world about how to name the GNU make utility. The official MinGW goes for mingw32-make.exe, while nuwen’s MinGW uses the traditional make.exe name. Make sure to have both, that is:

 copy C:\MinGW\bin\make.exe c:\MinGW\bin\mingw32-make.exe

Open a Command Prompt, and make sure MinGW is in your PATH by typing:

set PATH=C:\MinGW\bin;%PATH%

Check that you have gcc 4 by:

C:\>gcc --version
gcc (GCC) 4.2.1-dw2 (mingw32-2)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Note: make sure you don’t have MSYS in your path while performing the steps below. (If you don’t know what MSYS is, don’t worry.)

Get the sources

You need the Qt sources from Trolltech’s FTP server, in this document we’re using qt-all-opensource-src-4.3.5.zip. Download and unzip at a convenient location. We’ll assume you’ve put the sources under C:\src\qt-all-opensource-src-4.3.5 .

Note: make sure to get & unzip the zip file – don’t go for the tar.gz or tar.bz2 files, as you might run into issues like files missing after un-taring.

Patch the sources

Use the GNU patch utility that comes with nuwen’s MinGW to patch the sources, so as to fix the issues that would make them fail compilation using gcc 4. Download the appropriate patch from here into C:\src, and then, from the Command Prompt, in C:\src\qt-all-opensource-src-4.3.5 do:

C:\src\qt-all-opensource-src-4.3.5>patch -p2 < ..\qt-4.3.5-gcc4.patch

Compile

After all the preparations are done, configure and compile in C:\src\qt-all-opensource-src-4.3.5:

configure.exe -platform win32-g++
mingw32-make

This will take quite some time, so grab a tea. Both the release and debug libraries will be compiled.

Clean up

After compilation finishes, you can just use this very directory when building your Qt application. But the directory still contains a lot of intermediate build files, which take several hundred megabytes, so you might want to clean them up by:

mingw32-make clean

This will only clean the intermediate files, but the compiled libraries & executables will remain in place.

Resources

Or copy-paste the patch from here:

--- ./qt-all-opensource-src-4.3.5/src/corelib/kernel/qeventdispatcher_win.cpp   2008-05-26 11:06:55.000000000 +0200
+++ ./qt-all-opensource-src-4.3.5-gcc4/src/corelib/kernel/qeventdispatcher_win.cpp  2008-07-15 16:56:43.000000000 +0200
@@ -96,6 +96,8 @@
static ptimeSetEvent qtimeSetEvent = 0;
static ptimeKillEvent qtimeKillEvent = 0;

+LRESULT CALLBACK qt_internal_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp);
+
static void resolveTimerAPI()
{
static bool triedResolve = false;
--- ./qt-all-opensource-src-4.3.5/src/gui/inputmethod/qwininputcontext_win.cpp  2008-05-26 11:06:55.000000000 +0200
+++ ./qt-all-opensource-src-4.3.5-gcc4/src/gui/inputmethod/qwininputcontext_win.cpp 2008-07-15 17:29:20.000000000 +0200
@@ -76,6 +76,8 @@

#define IFMETHOD HRESULT STDMETHODCALLTYPE

+extern bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event);
+
interface IActiveIMMApp : public IUnknown
{
public:

Leave a Comment

You must be logged in to post a comment.