Displaying entries in "Qt"

 
 

How to compile QT4.8 32bit from GIT on Windows using Visual Studio 2010

14.05.2012Posted by BliZZarD

 

Revisions of this Tutorial:

  1. 14.05.2012 - First version published online

Overview

This tutorial for Qt will give you:

  • An OpenSource Build. (should also work with commercial builds with little to no work)
  • NO Webkit (webkit sucks, doesnt like compilers other than Gcc and is not compilable as a static LTCG (link time code generation) build)
  • STL compatibility (can be dissabled in the configure command)
  • OpenGL Desktop Usage
  • MySQL and SQLite support (both optional)
  • OpenSSL support (optional, but useful)
  1. A Debug Build, including symbols and pdb files, for debugging your application and stepping through code in MSVC
  2. A Release Build, stripped of symbols and optimized as STATIC librarys (.lib) with LTCG

The Basics

Decide on a folder where you want your QT installation to live, e.g. C:\QT or something along these lines - but make sure it is final, because moving a QT installation is a real pain in the ass!
We will call this base path (in my example C:\QT) $(QT_DIR) from now on.

MSVC Command Prompts:
Whenever I say something like "open a msvc cmd" I mean: Open a CMD Prompt with Administrator Privileges and the environment settings for Visual Studio 2010 x86 tools set.

Requirements:

  • NASM (required for OpenSSL)
    • Get it from http://www.nasm.us/, Downloads -> Latest Release Build, as of this writing v2.10, in the win32/ subfolder there is an installer or a zip file for a portable installation
    • Install/Unpack, remember the path of the Installation
    • If you choose the portable zip version, there is no nasmpath.bat, use this command: (Replace the path with your location)
  • Perl for Windows (required for OpenSSL AND Qt)
    • Get it from http://www.activestate.com/activeperl -> Free Downloads (Download Perl Binaries: ActivePerl Community Edition) and choose either the 64bit or 32bit version, depending on your OS (not in any way related to the version of QT you want to build!)
    • Install it and let it add itself to your PATH (QT requires it to be in the path...)
  • Python for Windows (required for Qt)
    • Get it from http://www.activestate.com/activepython/downloads (Download Python: ActivePython Community Edition) and choose either the 64bit or 32bit version, depending on your OS (not in any way related to the version of QT you want to build!)
    • Install it and let it add itself to your PATH (QT requires it to be in the path...)
  • CMake (required for MySQL)
  • MySQL (optional addon for Qt, provides MySQL support in QSql)
    • Get it from http://dev.mysql.com/downloads/mysql/ and Download the MySQL Community Server as "Source Code", Generic Linux (Architecture Independent), Compressed TAR Archive
    • Unpack it to $(QT_DIR), the archive contains a folder named "mysql-5.xxx" (xxx being the current version).
    • Start CMake-gui, select "$(QT_DIR)\mysql-5.xxx" for "Where is the source code:" and "$(QT_DIR)\mysql-5.xxx\BUILD" for "Where to build the binaries:"
    • Press Configure and select Visual Studio 10 as Compiler
    • In the Variables that CMake displays, change CMAKE_INSTALL_PREFIX to "$(QT_DIR)\mysql"
    • Press Generate and close CMake
    • Open Visual Studio and open the Solution file in "$(QT_DIR)\mysql-5.xxx\BUILD" called "MySQL.sln"
    • Select "Release" Configuration in the upper menu, right-click on the Project "ALL_BUILD" and select Build.
    • After the compilation is done, right-click on the Project "INSTALL" and select Build.
  • OpenSSL (optional addon for Qt, provides SSL/TLS and encryption support for Qt)
    • Get it from http://www.openssl.org/source/
    • ALWAYS MAKE SURE TO GET THE LATEST VERSION! This is critical software, if you want to use file encryption and/or encrypted communication via TLS/SSL it is of the utmost importance to have the latest build of OpenSSL with your QT Build.
    • Download the tarball and unpack it to $(QT_DIR), the archive contains a folder named "openssl-1.xxx"
    • Create a new folder in $(QT_DIR) named "openssl"
    • Open a msvc cmd and set the environment settings for NASM and Perl. For NASM, just execute the file "nasmpath.bat" in the msvc cmd, e.g. if your installpath for NASM was "C:\Program Files (x86)\nasm" type "C:\Program Files (x86)\nasm\nasmpath.bat" INCLUDING the " into your cmd prompt and press enter.
    • For Perl, it should be in your path, if you selected otherwise, you should know how to make it accessible :)
    • To test nasm, enter "nasm -v" and press enter, it should say: "NASM version 2.10 compiled on Mar 12 2012" (or something newer)
    • To test perl, enter "perl --version" and press enter, it should print some text containing "This is perl 5, version 12, subversion 4 (v5.12.4) built for MSWin" (or an newer version)

    • Now cd to $(QT_DIR)\openssl-1.xxx and execute (always replace $(QT_DIR) with your folder!):
  • SQLite (optional addon for Qt, provides SQLite >=3 support in QSql)
    • Get it from http://sqlite.org/download.html, download the latest amalgamation zip file from "Source Code" (sqlite-amalgamation-3xxxxxxx.zip)
    • Unpack it to $(QT_DIR), the archive contains a folder named "sqlite-amalgamation-3xxxxxxx".
  • GIT (required to get the latest Qt Version)

Download, configuring and compiling Qt

Now we will clone the current Qt4 Repository using Git, if you are behind a firewall and cant access via git://, use http instead (google!).

A note regarding the folder names that follow:
Because Qt is kind of uncute, it detects stuff like "-debug" or "-release" in its FOLDERNAME - DONT USE THAT. It will destroy the other installation and you will be in agony city.
I named my folders "qt4.8-git-d" and "qt4.8-git-r", that seems to work.
Open a Git Bash, cd to your $(QT_DIR) and be happy. No, seriously, enter "git clone git://gitorious.org/qt/qt.git qt4.8-git" and lean back, because that's going to take a while.

After it finished (successfully!), copy this folder twice into $(QT_DIR), name them "qt4.8-git-d" and "qt4.8-git-r", or whatever you want.

Compiling Qt: Debug Build

Now open a msvc cmd prompt, change into your debug folder (e.g. $(QT_DIR)\qt4.8-git-d) and execute: Replace $(QT_DIR) with your folder and add the correct version suffix for the SQLite folder. If you DONT want MySQL or SQLite, remove the "-qt-sql-" string for it and the -I/-L imports at the end, and for mysql the "-l libmysql"
If you WANT the demos and examples, remove the appropriate "-nomake xxx"

The configure command should print some happy little trees (information) and end with something like "You are now ready to build, just run nmake..." - if it does NOT, fix the problem, google it, write your congressman, et cetera.
Run nmake by typing "nmake" and pressing enter. Wait.
Congratulations, your debug Build is ready to go. On to the next one.

Compiling Qt: Release Build

Back to your msvc prompt, cd into your release directory and execute: Replace $(QT_DIR) with your folder and add the correct version suffix for the SQLite folder.
If you DONT want MySQL or SQLite, remove the "-qt-sql-" string for it and the -I/-L imports at the end, and for mysql the "-l libmysql"
If you WANT the demos and examples, remove the appropriate -nomake xxx (WARNING: As this is a static/LTCG Build, this can take UP TO 72 HOURS!)

The configure command should print some happy little trees (information) and end with something like "You are now ready to build, just run nmake..." - if it does NOT, fix the problem, google it, write your congressman, et cetera.
Run nmake by typing "nmake" and pressing enter. Wait.
Congratulations, your release Build is ready to go.

Preparing Visual Studio for Qt

Now download the current Qt Visual Studio Add-In from http://qt-project.org/downloads#qt-other and install it.
Open MSVC and open (Menu) Qt -> Qt Options and in the "Qt Versions" Tab select "Add". Enter a cute Version Name, e.g. "qt4.8-git-debug" and select the correct folder, repeat for the release Build.
You may now select your debug or release build as "Default Qt/Win version" (I use my debug build). Finish with "Ok".

Cleanup

At last, you can clean up a bit of the mess Qt left. After compilation, all the .obj files are useless and can be deleted. For this task I use a small bat file: Replace the path with your $(QT_DIR)s (debug and release, respectivly), save it as "delete-qt-obj-files.bat" and execute it.



Hints, Tips & Tricks

Everything should work out of the box, just some hints:

  • When changing between Debug and Release Configuration in Visual Studio, dont forget to right-click your Solution -> "Change Solutions Qt Version" and select the appropriate version.
  • The Release Build MAY require you to add a few extra Windows Libs to the Linker, in my case "ws2_32.lib winmm.lib Imm32.lib libmysql.lib" via Project Settings, Linker -> Input -> Additional Dependencies
  • Also, when build with MySQL support, libmysql.dll from $(QT_DIR)\mysql\lib is required to be in either a system directory or within your applications directory.

Report back with Errors, Hints or Questions

I hope everything worked for you, if not, drop me a note using my contact formular.
I await your appraisal ;)