Showing posts with label QT. Show all posts
Showing posts with label QT. Show all posts

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'

Tuesday, October 30, 2007

Hands on KDevelop, QT4, SDL

Got my hands on KDevelop, QT4 and SDL... ...well, just some Hello World stuff.

The good news: I was able to do what I wanted. Everything needed to make my own programs with a nice GUI (QT4), or even cross platform multimedia applications (using SDL) can be installed through the Synaptic Package Manager.

The bad news: One would assume that installing an environment for a C++ 'Hello World' application would be as simple as installing a development IDE (KDevelop), create a "new Hello World Project", compile and run. This isn't exactly the case. Everything went well until I tried to compile. Then I had to go through a painful process of installing additional packages related to the compiler, the automated creation of makefiles (e.g. what the heck is this AM_PROG_TOOL stuff?!), 'konsole', etc.!

Here is a list of steps and packages I have added:

1) Add KDevelop (just add KDevelop, all the other packages will be selected automatically):
cvs (1:1.12.13-8)
kdebase-bin (4:3.5.8-0ubuntu2)
kdelibs-data (4:3.5.8-0ubuntu3.1)
kdelibs4c2a (4:3.5.8-0ubuntu3.1)
kdevelop (4:3.5.0-0ubuntu2)
kdevelop-data (4:3.5.0-0ubuntu2)
libapr1 (1.2.7-8.2ubuntu1)
libaprutil1 (1.2.7+dfsg-2build1)
libarts1c2a (1.5.8-0ubuntu1)
libartsc0 (1.5.8-0ubuntu1)
libaudio2 (1.9-2)
libavahi-qt3-1 (0.6.20-2ubuntu3)
libcvsservice0 (4:3.5.8-0ubuntu1)
liblua50 (5.0.3-2build1)
liblualib50 (5.0.3-2build1)
libopenexr2c2a (1.2.2-4.3ubuntu2)
libpq5 (8.2.5-1.1)
libqt3-mt (3:3.3.8really3.3.7-0ubuntu11)
libsvn1 (1.4.4dfsg1-1ubuntu3)

2) Add the g++ compiler:
g++ (4:4.1.2-9ubuntu2)
g++-4.1 (4.1.2-16ubuntu2)
libstdc++6-4.1-dev (4.1.2-16ubuntu2)

3) Sort out problems related to the automated creation of makefiles (this step was quite cumbersome!):
build-essential (11.3ubuntu1)
dpkg-dev (1.14.5ubuntu16)
autoconf (2.61-4)
autoconf-doc (2.61-0ubuntu1)
automake (1:1.10+nogfdl-1)
automake1.10-doc (1.10-1)
autotools-dev (20070306.1)
m4 (1.4.10-0ubuntu2)
autobook (1.4.4-unofficial-4)
patch (2.5.9-4)
libtool (1.5.24-1ubuntu1)
libtool-doc (1.5.24-1ubuntu1)

4) konsole (yes, written with k!)
konsole (4:3.5.8-0ubuntu2)¨

5) QT Designer (Just add QTDesigner, the rest will be added automatically):
comerr-dev (2.1-1.40.2-1ubuntu1)
libaudio-dev (1.9-2)
libc6-dev (2.6.1-1ubuntu9)
libexpat1-dev (1.95.8-4ubuntu1)
libfontconfig1-dev (2.4.2-1.2ubuntu4)
libfreetype6-dev (2.3.5-1ubuntu4)
libgl1-mesa-dev (7.0.1-1ubuntu3)
libglib2.0-dev (2.14.1-1ubuntu1)
libglu1-mesa-dev (7.0.1-1ubuntu3)
libglu1-xorg-dev (1:7.2-5ubuntu13)
libice-dev (2:1.0.3-3)
libjpeg62-dev (6b-14)
libkadm55 (1.6.dfsg.1-7build1)
libkrb5-dev (1.6.dfsg.1-7build1)
liblcms1-dev (1.16-5ubuntu3)
libmng-dev (1.0.9-1)
libmysqlclient15off (5.0.45-1ubuntu3)
libpng12-dev (1.2.15~beta5-2ubuntu0.1)
libpq-dev (8.2.5-1.1)
libqt4-core (4.3.2-0ubuntu3)
libqt4-dev (4.3.2-0ubuntu3)
libqt4-gui (4.3.2-0ubuntu3)
libqt4-qt3support (4.3.2-0ubuntu3)
libqt4-sql (4.3.2-0ubuntu3)
libsm-dev (2:1.0.3-1)
libsqlite0 (2.8.17-2.1build1)
libsqlite0-dev (2.8.17-2.1build1)
libssl-dev (0.9.8e-5ubuntu3.1)
libx11-dev (2:1.1.1-1ubuntu4)
libxau-dev (1:1.0.3-2)
libxcursor-dev (1:1.1.8-2)
libxdmcp-dev (1:1.0.2-2)
libxext-dev (2:1.0.3-2build1)
libxfixes-dev (1:4.0.3-2)
libxft-dev (2.1.12-2ubuntu4)
libxi-dev (2:1.1.2-1)
libxinerama-dev (2:1.0.2-1build1)
libxmu-dev (2:1.0.3-1ubuntu1)
libxmu-headers (2:1.0.3-1ubuntu1)
libxrandr-dev (2:1.2.1-1)
libxrender-dev (1:0.9.2-1)
libxt-dev (1:1.0.5-3)
linux-libc-dev (2.6.22-14.46)
mesa-common-dev (7.0.1-1ubuntu3)
mysql-common (5.0.45-1ubuntu3)
qt4-designer (4.3.2-0ubuntu3)
x11proto-core-dev (7.0.10-2)
x11proto-fixes-dev (1:4.0-2ubuntu1)
x11proto-input-dev (1.4.2-1)
x11proto-kb-dev (1.0.3-2ubuntu1)
x11proto-randr-dev (1.2.1-2)
x11proto-render-dev (2:0.9.2-4ubuntu1)
x11proto-xext-dev (7.0.2-5ubuntu1)
x11proto-xinerama-dev (1.1.2-4ubuntu1)
xlibmesa-gl-dev (1:7.2-5ubuntu13)
xtrans-dev (1.0.3-2)
zlib1g-dev (1:1.2.3.3.dfsg-5ubuntu2)

6) SDL:
libflac-dev (1.1.4-3ubuntu1)
libmikmod2 (3.1.11-a-6ubuntu3)
libmikmod2-dev (3.1.11-a-6ubuntu3)
libogg-dev (1.1.3-2ubuntu2)
libsdl-console (1.3-4)
libsdl-console-dev (1.3-4)
libsdl-gfx1.2-4 (2.0.13-3)
libsdl-gfx1.2-dev (2.0.13-3)
libsdl-image1.2 (1.2.5-3)
libsdl-image1.2-dev (1.2.5-3)
libsdl-mixer1.2 (1.2.6-3)
libsdl-mixer1.2-dev (1.2.6-3)
libsdl-net1.2 (1.2.5-7)
libsdl-net1.2-dev (1.2.5-7)
libsdl-sge (030809dfsg-1ubuntu1)
libsdl-sge-dev (030809dfsg-1ubuntu1)
libsdl-sound1.2 (1.0.1-12build2)
libsdl-sound1.2-dev (1.0.1-12build2)
libsdl-stretch-0-2 (0.2.3-7)
libsdl-stretch-dev (0.2.3-7)
libsdl-ttf2.0-0 (2.0.9-1)
libsdl-ttf2.0-dev (2.0.9-1)
libsdl1.2-dev (1.2.11-9ubuntu2)
libsmpeg-dev (0.4.5+cvs20030824-2)
libsmpeg0 (0.4.5+cvs20030824-2)
libspeex-dev (1.1.12-3)
libtiff4-dev (3.8.2-7ubuntu2)
libtiffxx0c2 (3.8.2-7ubuntu2)
libvorbis-dev (1.2.0.dfsg-1)

So, with this packages, I am now really able to easily create 'Hello World' programs for both QT4 and SDL.

I don't know exactly what I should think now. On one hand, I am happy that I was able to sort out the problems myself (well, with the help of Google). On the other hand, compared to VisualStudio this was a rather cumbersome and geeky start. I wonder if there are some super-developer-packages that can be selected by a novice, and it will just select all sub-packages automatically to make sure that the first impression will be truly painless?

Generally, not being afraid of facing whatever unexpected problem occurs and sort it all out step by step is definitely something essential in the Linux World. I don't really have a problem with that, except that it eats up so much precious time, and even after having sorted out all the problems the overall feeling of being stupid and not having understood everything in full detail remains...