Building Qt Desktop for Windows with MSVC

Required SDKs and tools

Here are the tools required to build a Qt Shared or a Qt Static library. First, please, download and install (it is a good idea to find latest versions of software, listed below):

  1. Qt source code (I think you know where to find it :))
  2. On machines not having a previous Qt version already installed you need to install also Perl (e.g. ActivePerl [activestate.com] ) prior to configure. Otherwise you may receive following error message at the end of configure:
    > Perl not found in environment – cannot run syncqt.
  3. Microsoft Platform SDK [microsoft.com] (most recent at time of last wiki edit is Microsoft Windows 7.1 SDK.). The SDK includes the Microsoft Visual C++ compilers and debugging tools. You can in addition install Visual Studio Express [microsoft.com] if you like the IDE (not required!).
  4. KB2485545 [connect.microsoft.com] or Service Pack 1 [microsoft.com] are required for MSVC 2010 when building a 64bit library on intel x86 based CPUs. They fix a bug in the MSVC2010 compiler, and without it you will experience crashes when running 64bit binaries.
  5. OpenSSL [openssl.org] libraries or source code (if you want to build it yourself)

For Shared (if you want Phonon support) also required

  1. Microsoft DirectX SDK [go.microsoft.com]

Ok, all done but… If you decided to build OpenSSL yourself (really, I recommend you) it is also required

  1. ActivePerl [activestate.com] (community edition is free)
  2. The Netwide Assembler [nasm.us]

Installation

At first I recommend you to install all packages into one directory (for example C:\Development\):

  • C:\Development\MSVC (for Visual C++)
  • C:\Development\Windows 7.1 SDK (for platform SDK)
  • C:\Development\OpenSSL (for OpenSSL library or unpack source code anywhere for build)

It is not required to install examples, demos, docs, libraries not for target architecture e.g. ;)

Environment setup

It looks good to have a script to set up the environment instead of setting it all using system PATH variable:

  1. @ECHO OFF
  2.  
  3. set DEV=C:\Development
  4. set QTDIR=%DEV%\Qt
  5. set PATH=%SystemRoot%;%SystemRoot%\system32;%QTDIR%\bin
  6.  
  7. echo Setting OpenSSL Env.
  8. set OPENSSL=%DEV%\OpenSSL
  9. set PATH=%OPENSSL%\bin;%PATH%
  10. set LIB=%OPENSSL%\lib
  11. set INCLUDE=%OPENSSL%\include
  12.  
  13. echo Setting NASM Env.
  14. set PATH=%DEV%\NASM;%PATH%
  15.  
  16. echo Setting DirectX Env.
  17. set LIB=%DEV%\DirectX SDK\Lib\x86;%LIB%
  18. set INCLUDE=%DEV%\DirectX SDK\Include;%INCLUDE%
  19.  
  20. echo Setting Windows SDK Env.
  21. set WindowsSdkDir=%DEV%\Windows 7.1 SDK
  22. set PATH=%WindowsSdkDir%\Bin;%PATH%
  23. set LIB=%WindowsSdkDir%\Lib;%LIB%
  24. set INCLUDE=%WindowsSdkDir%\Include;%INCLUDE%
  25. set TARGET_CPU=x86
  26.  
  27. echo Setting MSVC2010 Env.
  28. set VSINSTALLDIR=%DEV%\MSVC
  29. set VCINSTALLDIR=%DEV%\MSVC\VC
  30. set DevEnvDir=%VSINSTALLDIR%\Common7\IDE
  31. set PATH=%VCINSTALLDIR%\bin;%VSINSTALLDIR%\Common7\Tools;%VSINSTALLDIR%\Common7\IDE;%VCINSTALLDIR%\VCPackages;%PATH%
  32. set INCLUDE=%VCINSTALLDIR%\include;%INCLUDE%
  33. set LIB=%VCINSTALLDIR%\lib;%LIB%
  34. set LIBPATH=%VCINSTALLDIR%\lib
  35.  
  36. echo Setting Framework Env.
  37. set FrameworkVersion=v4.0.30319
  38. set Framework35Version=v3.5
  39. set FrameworkDir=%SystemRoot%\Microsoft.NET\Framework
  40. set LIBPATH=%FrameworkDir%\%FrameworkVersion%;%FrameworkDir%\%Framework35Version%;%LIBPATH%
  41. set PATH=%LIBPATH%;%PATH%
  42.  
  43. echo Env. ready.
  44.  
  45. title Qt Framework 4.7.1 Development Kit.
  46.  
  47. cd %DEV%

This file is *.bat (or *.cmd) script. Just modify and save it for your environment. I set it up from tool’s (MSVC, SDKs) prompts.

You can add it to start menu or make a label (change script address at label preferences to “cmd /k path/to/setqtenv.cmd”).

What we have

C:\Development

At my computer I have installed MSVC++ 2010 Express Edition to C:\Development\MSVC, Windows 7.1 SDK C:\Development\Windows 7.1 SDK, DirectX SDK C:\Development\DirectX SDK, Perl C:\Perl (impossible to change), NASM unpacked to C:\Development\NASM, Qt Desktop (4.7.1) source code (from qt-everywhere-opensource-src-4.7.1.tar.gz) unpacked to C:\Development\Qt and OpenSSL source code unpacked to C:\Development\openssl-1.0.0c

“Static” tips

Before you start to build a static Qt Desktop library, you must know that for now you will have a dependence with MSVC Redistributable. To escape this you must replace -MD(d) build flag with -MT(d) (I will not recommend you use it, because it can cause runtime errors, etc. For example with this configuration I cannot start QtDesigner app. The reason is unknown.)

It’s available as both Static and Shared builds.

Building OpenSSL

At first, open your new Qt prompt, cd to your OpennSSL source directory and setup (if not set) Perl bin directory path (and check it?):

  1. > cd openssl-*
  2. > set PATH=C:\Perl\bin;%PATH%
  3. > echo %PATH%

Something like this:

OpenSSL build setup

If all is ok, we can continue…

Configuring and preparing OpenSSL (copy-paste it, here is underscore symbols):

If you have NASM:

  1. > perl Configure VC-WIN32 --prefix=C:\Development\OpenSSL
  2. > ms\do_nasm

And if not:

  1. > perl Configure VC-WIN32 no-asm --prefix=C:\Development\OpenSSL
  2. > ms\do_ms

Here we set up compiler and destination directory.

Now open ms\ntdll.mak (for Static — ms\nt.mak) file and replace at LFLAGS /debug with /release.

For Static build flag -MT is applied by default (look at “Statictips). For shared -MD is used (change *CFLAG*’s at ms\ntdll.mak from /MD to /MT if required for you).

Ok, building, testing and “installing”:

For Shared:

  1. > nmake -f ms\ntdll.mak
  2. > nmake -f ms\ntdll.mak test
  3. > nmake -f ms\ntdll.mak install

And for Static:

  1. > nmake -f ms\nt.mak
  2. > nmake -f ms\nt.mak test
  3. > nmake -f ms\nt.mak install

Ready. Now you can remove your OpenSSL source code (C:\Development\openssl-1.0.0c here, on screenshot) directory and Perl installation. They are no longer required.

Configuring Qt Desktop

There are 2 steps:

  1. Check/Change build flags
  2. Configure

Note: MSVC supports build with multiple processes. Really. You can add -MP[processMax] flag. Details here [msdn.microsoft.com].

For first step (are you really sure with “Static” tips?) we are going to %QTDIR%\mkspecs\win32-msvc2010 (I know that you know that real path is C:\Development\mkspecs\win32-msvc2010 and if you have different version of MSVC it will be different spec directory ;)). Here we are interested in the qmake.conf configuration file.

Edit QMAKE_CFLAGS_RELEASE, QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO & QMAKE_CFLAGS_DEBUG variables. For example:

  1. QMAKE_CFLAGS_RELEASE    = -O2 -MD -MP2
  2. QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MD -Zi -MP2
  3. QMAKE_CFLAGS_DEBUG      = -Zi -MDd -MP2

Next — configuring. My configuration (shared) is:

  1. > configure -release -opensource -shared -ltcg -no-fast -exceptions -accessibility -stl -qt-sql-sqlite -no-qt3support -no-openvg -platform win32-msvc2010 -graphicssystem raster -qt-zlib -qt-gif -qt-libpng -qt-libmng -qt-libtiff -qt-libjpeg -no-dsp -no-vcproj -incredibuild-xge -plugin-manifests -qmake -process -rtti -mmx -3dnow -sse -sse2 -openssl -no-dbus -phonon -phonon-backend -multimedia -audio-backend -webkit -script -scripttools -declarative -arch windows -qt-style-windows -qt-style-windowsxp -qt-style-windowsvista -no-style-plastique -no-style-cleanlooks -no-style-motif -no-style-cde -no-style-windowsce -no-style-windowsmobile -no-style-s60 -native-gestures

As you see, I do not want to build Qt3 support module, some styles, using image libs from Qt source, some additional instructions sets are enabled and some another stuff.

Configuring Qt Desktop

Ok, let’s configure… Accepting license… Please, wait… (building qmake now) :)

All the project’s files are read and now we able to build libs, tools, examples, etc. Just nmake it to build all. Also we are able to make only libs (nmake sub-src), tools (nmake sub-tools, but after libs)…

Recommendations

Copy (or move) OpenSSL libraries (*.dll) from %QTDIR%\OpenSSL\bin directory to Qt bin directory and add %QTDIR%\Qt\bin to system %PATH% variable.

  1. > move OpenSSL\bin\*.dll Qt\bin\

Reduce used HDD space

Attention! This operations removes debugging information! (if you set release-and-debug)

Just at %QTDIR% run nmake clean to delete all tmp objects, etc. Then rebuild translations (go to %QTDIR%\translations and run nmake). And remove libraries (*.dll) from %QTDIR%\Qt\lib.

  1. > cd Qt
  2. > nmake clean
  3. > cd translations
  4. > nmake
  5. > cd ..\lib
  6. > del *.dll

Categories: