Building QT4 MySQL Drivers with MinGW


This is a short howto for building MySQL drivers for QT4 on Windows using MinGW. The TrollTech recipe does not work directly with the MinGW suite because MySQL is compiled using the Microsoft compiler, while MinGW is a port of the GNU *nix compilation tools.

There was a discussion on the QT forums about the difficulties encountered with this combination of tools. I will simply expand on this and fill in some gaps. Acknowledgements are given to the people who did the legwork in the above discussion.

You should have installed QT4. I recommend compiling it for debug-and-release so that it will work smoothly with IDEs such as Eclipse (the installation binary appears only to be compiled for release).

MySQL Installation

Download the MySQL package for Windows. The latest stable release is 5.1.35 at the time of writing. The "Windows Essentials
(x86)" package includes the basic client libraries and server. It is worthwhile reading the platform notes for MySQL.

The
"Windows (x86) ZIP/Setup.EXE" package claims to contain the full installation including libraries for the embedded server. This package however seems to have omitted the Embedded server libraries in version 5 (the non-installer package has an empty directory for the embedded libraries). These libraries are present in version 4 so you may need to grab an earlier version from the archives.

There are at least three ways of making a QT application work with MySQL:

   1. Access a MySQL server over a network, typically on a *nix or Windows server.
   2. Access a MySQL server running on the same machine as the application.
   3. Incorporate an embedded MySQL server (provided by the MySQL package).

The latter allows an application to be compiled stand-alone with a MySQL server incorporated, avoiding the need for end-users to setup a full MySQL server. You need the full installer for this.

Choose a custom installation, and install to a suitable location. You will be presented with a number of optional parts of the package to install. The following are typical of the Essentials package, but there are more in the full packages:

   1. MySQL Server
   2. Client Programs
   3. C Include Files/Lib Files

Unless you need a MySQL server running on the same machine as the application, you only need to select part 3 of the package to install. It is recommended that part 2 also be installed as the client library mysql.dll is sometimes unavailable in part 3.

Assume that you have selected the location C:\mysql for installation of the files, although you can choose any location and change the paths accordingly in the following instructions. You should avoid using directories with spaces in them, such as Program Files, as it makes it difficult for compilation under MinGW.

Creating the MySQL client library

The client libraries (libmysql.dll and libmysql.lib) that are shipped with MySQL are compiled with the Microsoft compiler and cannot be linked using the MinGW compiler. Therefore, we first need to create a MinGW compatible library out of libmysql.dll.

This can be done from the command line as follows:

# cd c:\mysql\lib\opt
# reimp -d libmysql.lib
# dlltool -k --input-def libmysql.def --dllname libmysql.dll --output-lib libmysql.a

reimp is a tool to convert Microsoft's new-style (short) import libraries to be compatible with the Win32 ports of the GNU tools (MinGW, Cygwin). It reads the MS import library and writes all the library imports to a corresponding
libmysql.def file used by dlltool to create the import library.

dlltool takes the
libmysql.def file that was created by reimp, and creates the GNU compatible library libmysql.a.

Both these tools are found in mingw-utils.

Building the QMYSQL plugin dll

Build the Qt MySQL plugin based on the above MinGW compatible library using the following steps. This assumes that QT has been installed at C:\qt. Substitute
the path that you have selected into the following commands. You need the MySQL C Include Files/Lib Files package for this step.

# cd c:\qt\src\plugins\sqldrivers\mysql
# qmake -o Makefile "INCLUDEPATH+=C:\MYSQL\INCLUDE"  "LIBS+=-LC:\MYSQL\LIB\OPT -lmysql"  mysql.pro
# mingw32-make

The files
qsqlmysql.dll, qsqlmysqld.dll, libqsqlmysql.a, libqsqlmysqld.a will appear in your c:\qt\plugins\sqldrivers directory.

Make sure that libmysql.dll is in your path. This is located in the C:\mysql\bin directory which can be placed in the path environment variable and is provided by the MySQL client libraries (it is also included in the Include Files/Lib Files package in the full version of MySQL5).

MySQL Embedded Server

This is a useful way of providing a stand-alone application that incorporates a database server. As noted above, the embedded server libraries seem to have disappeared from the MySQL 5 releases, so you will need to go back to MySQL 4 or compile MySQL 5 from source with the embedded server option. To keep it simple, obtain the
"Windows (x86) ZIP/Setup.EXE" package for MySQL 4.1.22 and install only the Developer Components.

Locate the libmysqld.lib file in c:\mysql\Embedded\DLL\release and make the conversion of the library file to the GNU compatible form:


# cd c:\mysql\Embedded\DLL\release
# reimp -d libmysqld.lib
# dlltool -k --input-def libmysqld.def --dllname libmysqld.dll --output-lib libmysqld.a

Change the code in the source file c:\qt\src\sql\drivers\mysql\qsql_mysql.cpp to the lines below (this is near the bottom of the file) since the mysql_get_client_version() function call appears to have changed or disappeared from the MySQL API.

//#if MYSQL_VERSION_ID >= 40108
// d->preparedQuerysEnabled = mysql_get_client_version() >= 40108
// && mysql_get_server_version(d->mysql) >= 40100;
//#else
// d->preparedQuerysEnabled = false;
//#endif
d->preparedQuerysEnabled = true;

At the top of this file is a section that needs to be commented out as follows:

//#ifdef Q_OS_WIN32
// comment the next line out if you want to use MySQL/embedded on Win32 systems.
// note that it will crash if you don't statically link to the mysql/e library!
//# define Q_NO_MYSQL_EMBEDDED
//#endif

The advice is to be taken seriously. We will need to statically link the libmysqld.a file. If MySQL server is to be incorporated into an application, then it is likely that Qt would also be statically compiled with the application, as Qt is not common in the Windows world.

Now do the following for static compilation of Qt and MySQL Embedded:

# cd c:\qt
# mingw32-make clean
# configure -debug-and-release -static -qt-sql-mysql -l mysqld -I c:\mysql\include -L c:\mysql\embedded\dll\release
# mingw32-make sub-src

The third option -l specifies the libmysqld.a library to use, the second (capital i) specifies the directory of the MySQL include files and the final -L specifies the directory of the MySQL library file. The make sub-src line ensures that the examples are not built. This not only cuts down the compilation time, but also the disk space as each example is built with the full Qt static linkage.

This compiles without errors, but I have not yet attempted to compile an application to make use of the embedded MySQL server. It appears that it may be necessary to compile MySQL from source to generate the necessary libraries.



Contact: My email address can be constructed from the username "ksarkies" and the domain internode.on.net in the usual way.


First created 11 January 2007
Last modified
13 January 2007
Minor changes 22 June 2009
Ken Sarkies 2007