Friday, 23 September 2011

Building Qt library with Visual studio 2010 Express for x86 and x64 targets

Foreword
I have been looking for a decent C++ GUI library / application framework for a pretty long time. I must admit I have no idea which universe I was in when I last looked but I swear Qt was at that time a pathetic pre-jurrasic era GDI wrapper just as MFC or wx still are.

A few weeks ago though I looked at the latest version for the fun of it and was super impressed by their demo app with samples. The smooth animations, the high-res icons, the custom controls - exactly what I had been looking for for so long. So without further ado I went on to see if development in this framework is any easier than in other two. Of course it turns out it is not, but the framework provides so much more than others that it's a non-issue when it comes to selecting which one to work with.

Before we go into installation details, a word on MS x64 support:
I like 64 bit. It lessens restrictions that 32 bit modes impose though I'm not saying those problems are insurmountable. It does so with modest size penalty, but brings with it also the benefit of using a newer instruction set of your CPU. Besides, there's no reason I should be running a 32 bit app on my 64 bit OS if I can just as well run a native app, is there?
I have no idea why Microsoft insists on express only having x86 compiler. They are imposing the chicken and egg problem on themselves when their x64 operating systems are concerned.

However, the 2010 version makes it extremely easy to get an x64 compiler working and to make it even simpler, the IDE itself also adapts with just a few mouse clicks. There are plenty of guides for doing this on the net so I will not go into details, but following this particular guide will also get you fully functional cross platform compilers.

Please note that I am doing this on my Windows 7 Home premium x64 computer so your mileage may vary, but I suppose it should not vary by much. So without prolonging this too much, here are the prerequisites:

The prerequisites
To successfully compile Qt libraries for your VS2010 Express, you will need:
  1. Visual Studio 2010 Express install ISO
  2. VS2010 Service Pack 1
  3. Windows Platform SDK
  4. Compiler update for Platform SDK after SP1 patch
  5. Qt Creator
  6. Qt Libraries for VS 2008
  7. Qt jom
Jom is there only to make use of your multi-core CPU. You can use nmake instead, but I highly recommend you use jom. Your beard will be much shorter after compilation.

Installing Visual Studio 2010 Express with x64 compiler support
This one is pretty straight forward:
  1. Install VS2010 Express
  2. Install VS2010 SP 1
  3. Install platform SDK
  4. Install compiler update - this one will provide cross platform compilers in your VS bin directory though it isn't needed for IDE x64 development. Absolutely *must* be installed last of these
Installing Qt
There's nothing special about this one. I'd just care to mention that it pays off to think a bit ahead and not take the library proposed path when installing Qt Visual Studio libraries.
I suggest you install all Qt related files into the same base directory for later easy access.
While installing the library, instead of taking the x.y.z (version) directory, I suggest you append _x86 to it for later reference. Then duplicate the library directory into a new one with a _x64 suffix. This is necessary because Qt doesn't have x86-and-x64 target meaning you can have debug and release files in the same directory, but not multi-platform ones.

Your Qt directory structure should now look like this:
You see how the x86 and x64 targets are nicely aligned and distinguishable? :D I hope my nagging about suffixes paid off for you as well as it did for me :)

You are all set for compilation by this point.

Compilation
I should mention that installing SP1 and Compiler update is mandatory for x64 target because the original VS2010 x64 compiler had an optimization bug which produced segfaulting executables (misaligned SSE data). This is extremely important! The code will build even without those two updates, but it will be unusable!
The next thing to take care of is the fact that your library directory will increase in size from 1,3GB installed to almost 8GB compiled (OK, only 6,5GB for x86 version ;) ).

To compile, the following steps need to be performed from a command prompt. I made two batch files for me in the Qt base directory, you may choose for yourself, naturally:
  1. call "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\setenv" /x86 /win7
  2. set QTDIR=C:\Qt\4.7.4_x86
  3. set QMAKESPEC=win32-msvc2010
  4. set path=%path%;%QTDIR%\bin
  5. cd %QTDIR%
  6. configure -debug-and-release -opensource -no-qt3support -platform %QMAKESPEC%
  7. ..\jom\jom -j 4
The above steps are for x86 (32 bit) compilation. To compile for x64, just replace all occurences of x86 with x64. Be careful not to change "win32-msvc2010" because this Qt profile will work for both targets and there is no "win64-msvs2010" profile.

Note that the paths may be different in your case. The path in line 1 is your Platform SDK install dir, path in line 2 is your Qt library install dir.
Also you may choose to apply different Qt configurations in line 6 than I did. The flags I chose build everything but legacy support.

You can easily compile both targets at the same time, but make sure you run the compilation before you go to sleep because your CPU is not fast enough to wait it out :)