Compile Qt Statically with MySQL

The version of Qt that comes as a download from Trolltech does not have MySQL support out of the box. This can be compiled as a module very easily. There are instructions for that in their documentation. My goal was to be able to distribute an application that depended on Qt, MySQL, and Qwt without needing to install anything on the client computer. So I needed to compile the whole thing statically. Additionally, I’m a newb at C++, so I wanted the debug versions of all my libraries lying around so that I could more easily fix my bugs. Figuring out how to get static, debug versions of this whole stack created involved piecing together a lot of info on the internet, and a lot of trial and error. I’m going to put up my “recipe” in the hopes that it will help someone else.

Note that the full build of Qt with both release and debug static libraries will require something on the order of 6 GB of disk space. While this isn’t a big deal by today’s disk sizes, I was doing this in a virtual machine, and ran out of space. In the examples below, I’ve created a secondary disk, mounted as E:, where I manage all of this software. I typically install the various pieces of software in version-numbered subdirectories, so that I can easily have several versions sitting side-by-side.

First, install the Qt SDK for Windows. Also get the mingw-utils archive and unzip it into the MinGW subdirectory created under the Qt SDK installation location. The current versions at the time of this writing are qt-sdk-win-opensource-2009.02.1.exe and mingw-utils-0.3.tar.gz.

Second, get the MySQL download — without the installer — and extract it someplace on disk. At this point, you have to modify a couple of the libraries to work with mingw compiler. (I’ll readily admit that I am very foggy on why or what this accomplishes, but this is what needs doing.) Open a regular command prompt and do the following:

E:\MySQL\5.0.67\lib\opt> set PATH=%PATH%;E:\Qt2009.02.1\MinGW\bin
E:\MySQL\5.0.67\lib\opt> reimp -d libmysql.lib
E:\MySQL\5.0.67\lib\opt> dlltool --input-def libmysql.def --dllname libmysql.dll --output-lib libmysql.a -k
E:\MySQL\5.0.67\lib\debug> reimp -d libmysql.lib
E:\MySQL\5.0.67\lib\debug> dlltool --input-def libmysql.def --dllname libmysql.dll --output-lib libmysql.a -k
E:\Qt\4.4.3\> configure -platform win32-g++ -static -debug-and-release -no-accessibility -no-sql-sqlite -qt-sql-mysql -I e:\mysql\5.0.67\include -L e:\mysql\5.0.67\lib\opt -l mysql
E:\Qt\4.4.3> mingw32-make sub-src

Get the Qwt source files. Edit the qwtconfig.pri file and comment out the QwtDll line in order to make a static library, and change the “release” keywords to “debug.” Then, use a Qt command prompt and do the following:

E:\Qwt\5.1.1> qmake
E:\Qwt\5.1.1> make sub-src

Now you can edit your application’s .pro file like this:

CONFIG += static
INCLUDEPATH +=  E:/Qwt/5.1.1/src
LIBS += E:/Qwt/5.1.1/lib/libqwt.a

Note that if the library is built as shared, and Qwt is actually installed, it’ll wind up going in something like C:\QWT-5.1.1, the INCLUDEPATH would point at C:\QWT-5.1.1\include, and the LIBS would point at C:\QWT-5.1.1\lib\qwt.dll.

In my case, I have to distribute mingwm10.dll and libmysql.dll with my application. I’ve seen some writeups that talk about how disabling threads will allow the MinGW runtime to be compiled into the application statically, but it would require rebuilding MinGW from source, which seems really difficult. Also, it should be possible to rebuild the MySQL library as static, and have it get built into the application during the process above. Again, that seems really difficult, and would — I’m sure — require loads of time and disk space. At the end of the day, this method ultimately needs but two libraries to be distributed along with the application binary, avoiding the need to install Qt and Qwt on the end-user’s computer, and that’s good enough for me.

The default build command in the Qt command window will build the debug version of the application by default. The `make release’ command is needed to build the non-debug version via the default-created makefile.

Shared Setup (for reference)

Alternately, the MySQL plugin can be produced to work with the default install version of Qt. This will then require that Qt be installed on every machine that will run the program, and that it be included in the machine’s PATH. To compile this plugin, MySQL will still have to be extracted to the filesystem, and the library reconfigured, all as above.

Use a Qt Comand Prompt:

C:\Qt\4.4.3\src\plugins\sqldrivers\mysql> qmake "INCLUDEPATH+=C:\MySQL\5.0.67\include" "LIBS+=C:\MySQL\5.0.67\lib\opt\libmysql.a" mysql.pro

Note that the “libmysql.a” above differs from Qt’s documentation on this process. In their official instructions, this is a .lib file, but I have found that this does not work.

To go along with using a shared setup of Qt, Qwt only needs to have its qwtconfig.pri file edited to reflect the desired installation path, and then one can `qmake’, `make’, and `make install’.

In the shared configuration, the Windows PATH environment variable will need to be set to include the following (examples): C:\MinGW\bin;C:\Qt4.4.3\bin;C:\MySQL\5.0.67\lib\opt;C:\Qwt\5.1.1

  • #1 written by David  2 years ago

    New page about compiling Qt statically with MySQL support.

  • You may use these HTML tags: <a> <abbr> <acronym> <b> <blockquote> <cite> <code> <del> <em> <i> <q> <strike> <strong>

  • Comment Feed for this Post
Go to Top