Friday, June 20, 2008

Automating Qt compilation with MSBuild scripts

Qt introduces some special mechanisms to introduce some kind of "reflection" and a similar concept to events/delegates in C#, called Signals and Slots in Qt. This is very useful indeed, but when developing under windows with Visual Studio, the compilation process becomes a bit cumbersome:

1a) create *.pro file, using 'qmake -project'
1b) derive a makefile from the *.pro, using 'qmake -makefile'
2) generate *.vcproj (to work in Visual Studio)
3) compile *.vcproj

This can be perfectly automated using MSBuild scripts (*.proj files). With MSBuild scripts you can do anything that you can do with *.bat files, and a whole lot more. They are typically used to automate compilation (and 'Continuous Integration') of .Net projects, but they can be perfectly abused for C++/Qt stuff too! :-)

Prequisites:
- Install .Net 2.0 SDK (it's needed for executing VCBuild tasks in MSBuild files)
- Append 'C:\Program Files\Microsoft Visual Studio 9.0\VC\vcpackages' to your Path Environment Variable.
- 'Qt-ready' environment

We put Steps 1a, 1b, and 2 in a 'compile_qt.proj' MSBuild script:


We put Step 3 in a separated 'compile_vc.proj' MSBuild script (it MUST be in a separated MSBuild script, because the vcproj files are recognized danymically with msbuilds 'ItemGroup' mechanism, which is always invoked at the startup of the script, hence only working in our case properly if we start this script AFTER steps 1 and 2):


It would be possible to call compile_vc.proj directly from within compile_qt.proj, but somehow I find it more convenient to have a spearate third 'compile.proj' that calls all other *.proj (many other custom build steps can be added later, and clearly separated in different *.proj files for easy maintenance):


Now the whole conversion/compilation process can be run with one single command:

C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe compile.proj

I recommend to define C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe as the default application to run *.proj files, then you only need to double-click compile.proj and that's it.

If run with the 'just clicking the *.proj', then if there is an error in the conversion/compilation process, the shell closes immediately after the error was displayed, making it impossible to investigate the problem. The simplest solution to that is to have a little 'compile.bat' file:

C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe compile.proj @pause
...and to just click compile.bat instead of compile.proj when facing errors.

Important note: All *.proj and *.bat files in this article must be located in the root directory of your Qt project.

PS: Sorry for the pictures... ...can anybody tell me how to add ordinary 'code blocks' with the blogspot engine?!

Friday, June 13, 2008

Setting up Qt for Visual Studio 2008

This guide describes the compilation and configuration of Qt using Microsoft Visual C++ 9.0 Editions (Express, Standard or Professional editions will all work), using Microsoft's VC 9 compiler. These instructions are for Qt 4.4.0 but will most likely also work for newer versions.

Prerequisites:

  • Microsoft Visual C++ 9.0 (e.g. Express Edition), aka VS 2008 must be installed.

Qt compilation:

  1. Download the qt source from here: http://trolltech.com/developer/downloads/qt/windows
  2. Unzip the contents of the zip to (for example): 'c:\qt\4.4.0' (so 'configure.exe' is located in 'c:\qt\4.4.0').
  3. In order to avoid lots of compiler warnings, edit the file 'c:\qt\4.4.0\mkspecs\win32-msvc2008\qmake.conf' by removing '-w34100 -w34189' from the line 'QMAKE_CXXFLAGS_WARN_ON = $$QMAKE_CFLAGS_WARN_ON -w34100 -w34189'. (Warning 4100 indicates unused function parameters, which will happen all the time. Warning 4189 shows initialized but unused variables, which is common in the release build).
  4. Open Environment Variables (In Windows Vista: Control Panel, System and Maintenance, System, Advanced system Settings, Environment Variables).
  5. Add the 'System Variable' named 'QTDIR' and set its value to 'c:\qt\4.4.0'.
  6. If it doesn't exist yet, add a 'System Variable' named 'QMAKESPEC' and set its value to 'win32-msvc2008'.
  7. Append ';c:\qt\4.4.0\bin' to the 'Path' variable
  8. Run a console with the VS 2008 paths set. Do this by selecting 'Tools' - 'Visual Studio 2008 Command Prompt' in Visual Studio 2008.
  9. In the console, navigate to 'c:\qt\4.4.0' (type 'c:' followed by 'cd c:\qt\4.4.0')
  10. Type 'configure', enter 'y'
  11. Type 'nmake'

Visual Studio Integration (recommended)


  1. Add 'c:\qt\4.4.0\include' to the include paths in VS 2008
  2. Add 'c:\qt\4.4.0\lib' to the library paths in VS 2008 (not necessary when using nmake, but it helps IntelliSense finding the Qt header files)
  3. Create desktop shortcuts to 'bin\designer.exe' and 'bin\assistant.exe'
  4. Associate '.ui' files with 'bin\designer.exe'