Installing Qt 4 and MySQL.


A guide to installing Trolltech Qt 4 with MySQL on Linux and Windows.


Disclaimer (sort of..):
This document is based on the experiences I had installing Qt 4 on my HP Compaq nx6120 laptop. It is presented here as a reference guide to help anyone who wishes to install Qt 4 on either platform. It is not to be taken as gospel, but rather as an example of how one individual went about the process. There are no guarantees this method will work, although it worked for me, it may not work for you. Also note, I am note an expert in writing code and I am self taught. It is also assumed the reader has sufficient knowledge of the target OS and procedures required to compile source code. Some steps taken assume existing knowledge.

Overview:
My aim was to be able to write a database package that would work on any platform and to use the same environment on any platform. To achieve this I decided to use Trolltechs Qt 4 and MySQL. To keep the development environment the same across multiple platforms I chose to use Monkey Studio as the IDE. Note these are not the only cross platform environments available, just the ones I chose to use. I use mainly Linux, however, I am required to target the Windows platform for work related programs. A secondary aim was to be able to make static or shared libraries without recompiling Qt. This article will describe how I installed the packages to make the cross platform environment and the choice of libraries work. Note: the Qt 4 and Monkey Studio packages are available for Mac OS X as well. However, I don't have access to a suitable machine to test them on OS X so this guide does not cover OS X. One day......

Requirements:
Linux:
I use Slackware as my preferred distribution. For this exercise I used a full install of Slackware 11. Slackware has all the packages required already installed except for Qt and the IDE package. Well, that's not quite right, it has the Qt 3 development environment installed.

You will need to download the following packages:

Qt 4 packages from Trolltech (Version 4.2.3).

IDE package from Monkey Studio (Version 1.5.3).

You may need to update the "glib2" packages as well if you are doing a static build. My default install of Slackware 11 was missing the "libglib-2.0.a" and "libgthread.a" files from the "/usr/lib" directory. I downloaded the "glib2-2.10.3-i486-2.tgz" package fromthe Slackware packages site and did an "upgradepkg" to remedy the situation. This package needs to be installed before installing Qt. Don't forget to run "ldconfig".

Windows:
I use Windows 2000 as my preferred Windows OS. You will need to download the the following packages:

Qt 4 from Trolltech (Version 4.2.3).

MySQL database server (Version 5.0.27).

The MinGW Utilities (Version 0.3). We require "reimp" from this package.

IDE package from Monkey Studio (Version 1.5.3).



Linux Install:
First I installed the static version. Unpack the sources into a temporary directory, change into that directory and then at the bash prompt run the following commands:

# ./configure -platform linux-g++ -prefix /usr/lib/qt-4.2.3-static -static -release -qt-sql-mysql -qt-sql-sqlite -I /usr/include/mysql -L /usr/lib/mysql

# make

# su

# make install


Note: You may require different options passed to configure so try running "./configure --help" to see all the options available.

After you enter "make" would be a good time to have a preferred caffeinated beverage.

Once the Qt static package is installed, you can move onto the IDE. Again, unpack the source into a temporary directory and change into that directory. Run the following command:

# /usr/local/qt-4.2.3/bin/qmake && make


Note: The full path to qmake is given because we want to use the Qt4 qmake not the Qt3 qmake that comes with Slackware 11.

This will create the binary in the subdirectory called "binary". I just copied the compiled binary into "/usr/local/ide" and created a link on the desktop.

You should now have a working Qt 4 static installation with a working IDE. Note, you will have to set the default paths within Monkey Studio to use the Qt 4 packages. You will also have to set the library and includes paths for MySQL (if its used) in each project file as required.

Now you can create the shared version of Qt. Change back into the Qt sources directory and do the following:

# make clean

# ./configure -platform linux-g++ -prefix /usr/lib/qt-4.2.3 -release -qt-sql-mysql -qt-sql-sqlite -I /usr/include/mysql -L /usr/lib/mysql

# make

# su

# make install


To choose which libraries to use when compiling your program is simply done by changing the path to Qt4 under “Edit/Options” in Monkey Studio.

A Pizza about now sounds good.....

Windows Install:
Windows is not as straight forward as Linux as it does not come with many packages preinstalled. Firstly install the Qt 4 package. This will install Qt 4 and the MinGW enviroment. Defaults will do at this stage except I selected the install path to be “c:\qt\4.2.3-static”. The reason will become obvious soon. At this point I did the “mingwm10.dll” as described below under "Optional Extra".

Next, install the MinGW tools. A default installation will be fine as we only want one file from this, "reimp". Copy it into the c:\mingw\bin directory.

Now install MySQL. Make sure you install the development libraries as well. We have to modify some library files in MySQL before we can compile Qt. Change into the "c:\mysql\lib\opt" directory and do the following from the command prompt:

c:\> cd mysql\lib\opt

c:\mysql\lib\opt> reimp -d libmysql.lib

c:\mysql\lib\opt> dlltool --input-def libmysql.def --dllname libmysql.dll --output-lib libmysql.a -k


Then we do the debug libraries:

c:\> cd mysql\lib\debug

c:\mysql\lib\debug> reimp -d libmysql.lib

c:\mysql\lib\debug> dlltool --input-def libmysql.def --dllname libmysql.dll --output-lib libmysql.a -k


Note: You may have to give the full path to "reimp" and "dlltool".

Next is modifying Qt 4 for our requirements. Change into the Qt 4 root directory and then at the command prompt run the following commands:

c:\qt\4.2.3-static> configure -platform win32-g++ -static -release -qt-sql-sqlite -qt-sql-mysql -l mysql -I c:\mysql\include -L c:\mysql\lib\opt

c:\qt\4.2.3-static> mingw32-make


Note: You may require different options passed to configure so try running "configure --help" to see all the options available.

After you enter "mingw32-make" would be a good time to have another preferred caffeinated beverage.

Once the Qt package is installed, you can move onto the IDE. Again, unpack the source into a temporary directory and change into that directory. Run the following commands:

c:\temp\ide> c:\qt\4.2.3-static\bin\qmake && make


Note: This assumes you have not set the "path" environment variable.

This will create the binary in the subdirectory called "binary". I then just copied the compiled program into "c:\ide" and created a desktop shortcut.
You should now have a working installation of Qt 4 with static libraries, MySQL 5 and the Monkey Studio IDE. You will have to set the library and includes paths for MySQL (if its used) in each project file as required.

Now to have both shared and static libraries for Qt you need install a second copy of Qt. So go back to the original Qt download and reinstall to a new directory, for this example, “c:\qt\4.2.3”. Again I did the “mingwm10.dll” mod. Run configure as follows:

c:\qt\4.2.3> configure -platform win32-g++ -debug-and-release -qt-sql-sqlite -qt-sql-mysql -l mysql -I c:\mysql\include -L c:\mysql\lib\opt -L                     c:\mysql\lib\debug
c:\qt\4.2.3> mingw32-make


You now have both shared and static libraries installed. To use the appropriate libraries when compiling your program simply change the windows environment path from one version to the other. This is why the IDE was created as a static compile. If it was shared, changing the enviroment path would break it. Don't forget you will need to “clean” your projects before building again with the new libraries.

Note: The QT Designer program does not work when compiled statically. I believe this is due to its need to dynamically load modules which is not possible in a static build. I develop using the shared version and compile the release version statically when my code finally works. Another solution is to copy the “shared” version over the “static” version. Crude, but seems to work.

Optional Extra:
If you wish to remove the dependency on mingwm10.dll as well, here are the steps I followed. Note: there is some discussion around the forums as to whether this is a valid solution. It seems to work for me so far.

Edit c:\qt\qt4.2.3\mkspecs\win32-g++\qmake.conf and remove all occurrences of “-mthreads”. Recompile Qt as above. Compile your project and the dependency on mingwm10.dll will be no longer.

Must be Pizza time again....

Additional Packages:
An additional package that I have found very useful is QextSerialPort. This is a cross paltform serial port class library written in and for Qt. I downloaded the sources and installed them under "/usr/lib/qt/qextserialport" in Linux and "c:\qt\qextserialport" under Windows. Just add the relevant path to your project file and you should be away.


Thats all there is to it. This is probably the easy bit. Now the hard part is writing all that C++ code you have always wanted to write. Good Luck!
I hope you have found this reference useful.

Last Updated on 01/05/2007.