Categories

Qt 4.5 and MySQL-plugin with Mingw on Windows XP

This is meant to be a quick howto / tutorial on how to setup a build-enviroment for C++ / Qt with Qt Creator ( Trolltech / Nokias new Qt IDE) where you use Mingw to compile your applications and have a MySQL-plugin available.

Getting the required files

You will be needing three files to get started.

First get the latest GA-release of MySQL. Go to their download-section and find the “Windows MSI Installer” for your architecture (x86 or x64). Save the file to your desktop.

Then go to Trolltech / Nokias download-section to get the LGPL’ed Qt SDK. Save the file to your desktop.

Finally go to the Mingw download-section to get the Mingw Utilities. I downloaded version 0.3 (latest version as of this writing). Save the file mingw-utils-0.3.tar.gz to your desktop.

In addition to the three files mentioned above you might need WinRAR or a similar tool to unpack tar.gz-files.

Installing MySQL

I always start by installing MySQL. This is not a requirement and the order in which you install MySQL and Qt does not matter.

Start the MySQL-installer you have on your desktop. Choose the advanced install options. I usually remove the MySQL server instance from the install as I have several external database-servers I do tests against. No need to clutter the dev-box if you have it available other places.

What’s important here is that you choose to install “Development Components” and all sub-features. These are the ones that’ll enable Qt to know how to reach a given MySQL database.

Another important step here is to install directly to c:\mysql, not c:\Program Files\Mysql\MySQL Server\whatnot. Spaces in pathnames confuse certain of the items in the toolchain. If you do install somewhere with spaces in the path then you have to remember to always use the old style-pathnames: c:\Progra~1\etc .

Proceed with the install, and after completion verify that you have libmysql.dll under c:\mysql\bin, and that there is a directory c:\mysql\include with header-files (.h) in it.

Installing Qt SDK

Start the Qt SDK installer and choose to install it in c:\Qt\2009.01 or whatever path it propose, as long as it is a path without spaces in it. Choose to install all the components, including Mingw.

Installing Mingw Utilities

The Mingw Utilities is simply a collection of executables, plus a couple of dlls. Open the file you downloaded earlier with a suitable program like WinRAR and copy the _contents_ of the bin-directory into C:\Qt\2009.01\mingw\bin.

Fixing (?) QMake

It might just be me having this problem, but I had to rebuild QMake even before configure would run and not crash spewing something about not finding stuff in ‘c:\Progra~1\Trolltech..’. Anyway, it’s not exactly rocketscience. Locate the ‘Qt Command Prompt’ and start that. It will upon execution add a couple of locations to PATH so that you’ll have the tools you need easily available. From there you do the following.
C:\Qt\2009.01> cd qt\qmake
C:\Qt\2009.01\qt\qmake> mingw32-make clean
C:\Qt\2009.01\qt\qmake> mingw32-make

After that QMake should start looking for mkspecs and other candy in the appropriate locations.

Preparing Qt for building

The next thing you want to do is run configure with a handful of arguments. For the sake of simplicity I’ll just include the arguments you need to have Qt build with the MySQL-plugin.
Again, in the Qt Command Prompt:
C:\Qt\2009.01\qt> configure.exe -debug-and-release -plugin-sql-mysql
The above will create Makefiles and things for all the sub-projects in the Qt directory-tree. This might take a little while so you might want to have some knitting-needles or a book at hand.

See ‘configure.exe -help’ for more options.

Configuring the MySQL-plugin

The Qt Reference Documentation is one of the best pieces of documentation I’ve ever seen. It’s full of working examples of how to use the API Trolltech / Nokia provide and generally have a very high level of quality.

However when it comes to how to build Qt with certain plugins it can be a bit lacking. The documentation here states that you do the following to build the MySQL-plugin on Windows:
cd %QTDIR%\src\plugins\sqldrivers\mysql
qmake "INCLUDEPATH+=C:\MySQL\include" "LIBS+=C:\MYSQL\MySQL Server\lib\opt\libmysql.lib" mysql.pro
nmake

That’s great, _if you use nmake_. Alot of us rely on Mingw to build our applications on Windows, and for us the above simply will not work. That is due to differences in object and lib-format between MSVC (which the mysql-dll’s are built with) and the GNU-toolchain. To solve this you have to do some magic on the dll and produce something that the GNU-toolchain can work with.

Proceed by doing this in the “Qt Command Prompt”:
C:\Qt\2009.01\qt> cd src\plugins\sqldrivers\mysql
C:\Qt\2009.01\qt\src\plugins\sqldrivers\mysql> set MYSQL_PATH=c:mysql
C:\Qt\2009.01\qt\src\plugins\sqldrivers\mysql> reimp -d %MYSQL_PATH%\lib\opt\libmysql.lib
C:\Qt\2009.01\qt\src\plugins\sqldrivers\mysql> dlltool -k --input-def LIBMYSQL.def --dllname libmysql.dll --output-lib libmysql.a
C:\Qt\2009.01\qt\src\plugins\sqldrivers\mysql> qmake "INCLUDEPATH+=%MYSQL_PATH%include" "LIBS+=-L. -lmysql" mysql.pro

Building Qt

When all of the above has been done all you have to do is start the Qt build process. Again in the “Qt Command Prompt”:
C:\Qt\2009.01\qt> mingw32-make sub-src
That’s it! You should now have qmysql*.dll in pluginssqldrivers. To use it just make sure that you have the path where you find libmysql.dll (usually C:\mysql\bin) as a part of the environment variable PATH.

Questions and comments are as always welcome.

40 comments to Qt 4.5 and MySQL-plugin with Mingw on Windows XP

  • This is great. I have really been struggling with building the MySQL driver plugin using MinGW. Can I build the plugin with nmake and then build my application with mingw32-make? Or do both my application and the plugin need to be built the same way?

    • Glad I could help!

      As far as my understanding goes this is not possible. Are there any good reasons why you’d want to do that? Aside from producing smaller binaries, which are not as important today as it used to be back in the day, nmake does not give that much added value IMHO. Distribution / deployment of MingW-compiled binaries is also much less cumbersome than that of binaries produced with nmake (manifests and XML-hell anyone? :p ).

      Anyway, I’m interested in more reasons to use nmake, since I have not found that many myself, except reducing hairloss while configuring Qt.

  • David

    I am trying to follow your guide but in the command lines in this guide show no ‘/’ and ‘\’. Or at least on my webbrowsers (IE and google chrome). Could you re-type them in a comment.

    ps: what i see is the following:
    C:Qt2009.01> cd qtqmake
    C:Qt2009.01qtqmake> mingw32-make clean
    C:Qt2009.01qtqmake> mingw32-make
    not that there are no \ or /

    • Thanks David for the “heads-up”.

      It seems like this is caused by Wordpress’s way of parsing posts. It has now been corrected.

      While we are on that page; please note that you have to use a bit of “common sense” as to where lines are separated / terminated. Some lines are very long and have been “broken” over several lines on the page here. If someone would enlighten me as to how one can write multi-line commands on Windows then I’ll update the post by properly formatting the examples.

  • Rodrigo

    Thanks Christopher for this wonderful tutorial. Thanks to your help at last I was able to get mysql driver compiled with mingw. I’ve been stuck on this for a week now, and now I can go on with my work.

    Cheers!

  • Hi Christopher,

    Nice posting. Wish that I had found this site before I started getting Mysql working with QT. Question, though… I have done everything to:

    qmake “INCLUDEPATH+=%MYSQL_PATH%include” “LIBS+=-L. -lmysql” mysql.pro

    and so far, so good. However, whenever I do the qmake, I keep getting the error: Unable to find main.cpp.

    Any ideas?

  • This could do with all the \ putting in the right places and the extra paths to the mysql includes

    cd %QTDIR%\src\plugins\sqldrivers\mysql
    set MYSQL_PATH=c:\mysql
    C:\Qt\2009.03\mingw\bin\reimp -d %MYSQL_PATH%\lib\opt\libmysql.lib
    C:\Qt\2009.03\mingw\bin\dlltool -k –input-def LIBMYSQL.def –dllname libmysql.dll –output-lib libmysql.a
    qmake “INCLUDEPATH+=%MYSQL_PATH%\include” “LIBS+=-L. -lmysql” mysql.pro

    Also i had to get reimp as it is an additional package for mingw

    • Hi Dave,

      Thank you for the heads-up on missing /’s. I’ve had a few rounds with wordpress and /-substitution. It’s quite annoying. I hope I got the rest of them in now.

      About reimp: in the start of the blogpost I wrote that I downloaded ‘MinGW Utilities’ (mingw-utils-0.3-src.tar.gz). In that file you will find reimp. Perhaps it was not clear enough. I guess it would be easy to think that you should use that link to download MinGW itself. I’ll try to find a better wording for that section.

      Thanks for commenting.

  • reimp -d %MYSQL_PATH%\lib\opt\libmysql.lib didn’t work. ANy way around?
    its says, reimp not recognised. I am in hurry. Please reply soon.

    • You need to have reimp in your PATH. RIght-click on ‘My Computer’ on your desktop, select ‘Properties’ and navigate to ‘Environment variables’. Add the path to reimp (the directory reimp resides in). Save and exit. If you open a new command prompt then the PATH-change should be active.

  • Now I have another big problem. I did use the reimp by giving absolute path, but now with qt-creator I can’t use any library classes. It says no such files or directory if I include Standard Qt Classes libraries. What is happening?

    • To be frank I have no idea. Never run into the problem myself. Is it during compile you get the ‘no such file or directory’-warning? Have you made certain all include-paths are OK in the project-settings?

  • Joy

    I got the qsqlmysqld4.dll and qsqlmysql4.dll,but when i run my application ,it always say:QMYSQL driver not loaded. What can i do?thank you

    • Hi,

      That sounds like a search-path issue. You need to make sure that Qt’s plugin-system can find the dlls. You can do this easily by placing them in a folder called ’sqldrivers’ in the folder where the executable resides. The other way of doing it is putting the dll’s in one of the other places Qt searches for plugins. The manual says something about this.

      Also, on some occations you need to clear the plugin cache. The manual has something clever to say about this too.

      Good luck!

      • Joy

        Hi,
        Thank you.I’m sure that i have placed them in the folder ‘C:\Qt\2009.03\qt\plugins\sqldrivers’.I forgot to say, i got the dll by directly useing the mingw32-make.Is it the reason that qt can not read it?and another problem is that i have build the qt for three days,but when i call the mingw-32,it always show me some error.I build the qt by this step,
        first,C:\qt\2009.03\qt>configure -debug-and-release -no-webkit -no-qt3support -plugin-sql-mysql
        then,C:\qt\2009.03\qt>mingw32-make
        is it ok?
        Wish all the best withes for you!Thank you.

      • Joy

        Thanks for your help. I got it!And i find in the qt4.5.2, there is no need for reimp to convert the dll.Just do it like this:
        qmake “INCLUDEPATH+=C:\MySQL\include” “LIBS+=C:\MYSQL\MySQL Server\lib\opt\libmysql.lib” mysql.pro
        mingw32-make
        and then,i got the driver!

        • asdf

          Thanks Joy and Christopher

          I’ve just run the qmake with includepath and boom, there it’s.
          All, just look for the %QTDIR%\src\plugins\sqldrivers\mysql and compile it with mingw and those includepaths like Joy said.
          Anyway, is there any option to compile it into release dll? because it compiles debug dll. thanks a lot.

  • I have reached till here “”"Fixing (?) QMake”"” SECTION
    and got this message(see below). why so, help.

    C:\Qt\2009.03\qt>cd qmake

    C:\Qt\2009.03\qt\qmake>mingw32-make clean
    mingw32-make: *** No rule to make target `clean’. Stop.

  • no there is no makefile in that directory. Is makefile an application or a g++ file.
    ————-
    Just to infom chris, that i am working on this since a month and no hope found. I hope you are there till the conversation. cause i m not a proper programmer and i might ask you many questions. I hope I might finally get the mysql compile successfully. please help.
    ————-
    I have :
    -qt 2009.03
    -mingw bin dlls(binutils-2.18.50-20080109-2.tar.gz)
    -also have mingw 5.1.4 installed.
    I don’t have visual basic c++(i’ve read in some sites that some make commands doesn’t work if visual basic is not installed.)

  • Jon

    I down loaded and instatalled the QT SDK into the default location C:\QT

    MySql was down loaded and installed into C:\MySQL\MySQLServer5.1\
    A compromise between the recommended location of C:\MYSQL and the default

    I then went through the following incantations:

    C:\Qt\2009.03\qt> configure.exe -debug-and-release -plugin-sql-mysql
    C:\Qt\2009.03\qt> cd src\plugins\sqldrivers\mysql
    C:\Qt\2009.03\qt\src\plugins\sqldrivers\mysql> set MYSQL_PATH=C:\MySQL\MySQLServer5.1
    C:\Qt\2009.03\qt\src\plugins\sqldrivers\mysql> qmake
    “INCLUDEPATH+=C:\MySQL\MySQLServer5.1\include” “LIBS+=C:\MySQL\MySQLServer5.1\lib\opt\libmysql.lib” mysql.pro
    ————–
    Originally I tried the
    qmake “INCLUDEPATH+=%MYSQL_PATH%\include” “LIBS+=-L. -lmysql” mysql.pro
    but this did not work
    ————–
    C:\qt\2009.03\qt>configure -debug-and-release -no-webkit -no-qt3support -plugin-sql-mysql
    C:\qt\2009.03\qt>mingw32-make

    -only to end up a mysql.h error

    Inspecting the compiler output immediately before the error showed that C:\MySQL\MySQLServer5.1\include was not being searched.
    I am new to QT and qmake so I do not know why.

    I got around this problem by copying
    mysql.h
    mysql_com.h
    mysql_time.h
    mysql_version.h
    my_list.h
    typelib.h
    from C:\MySQL\MySQLServer5.1\include to C:\Qt\2009.03\qt\include\QtSql

    and

    C:\MySQL\MySQLServer5.1\lib\opt\libmysql.lib to C:\Qt\2009.03\qt\include\QtSql\

    Where the compiler could find them.

    A quick and dirty fix I know – but I can now link to my database!

    Thanks for the help this is thh only site I’ve found that gives instructions for mere mortals.

  • yaayaat

    Great tutorial, i would like to adopt your tutorial into Bahasa Indonesia in my blog, there are many QT Designer Entushiast like me that didnt have any clue about Lingking MySQL and QT Designer on Windows, thanks

    • If you want to trranslate it then feel free to do so. Just remember to link back here so that the people who have english as their native / second language can get to it if they find your blog first. :)

      Also, it is Qt Creator which is bundled in the Qt SDK that I’ve talked about here, not Qt Designer. It is a little important to differentiate between those two, as they are two completely different things.

  • deven

    Thanks for your tutorial.really really thanks but still it shows drivers not loaded why so.i have qmysql4 and qmysqld4.dll both files in qt\plugins\sqldrivers
    and mysql server installed in c:\mysql directory
    plz tell all important settings, steps and a sample code for just connecting mysql server with qt program. i have a project to be submitted before 25 oct plz help me bro.i did not find reimp.and only once configured mingw32-make compiles only once correctly.i hve to re configure it all time it takes a long time.plz help me plz……

    • After a successful compile of Qt with the mysql-plugin you need to have both the qt-library files available in PATH, as well as the libmysql.dll (comes with MySQL). It is hard to infer from your post exactly what has gone wrong. You say you didn’t find reimp, so I’m guessing you were not able to compile it at all. You should read through the tutorial once more. I have provided links to all the tools you need, and provided a step by step guide to how you get this up and running. I followed it myself last weekend and it worked just as it should, so you’ve probably just missed a step or something.

  • deven

    the procedure you told i executed and it completed successfully now will u please help me to build a project for just connect mysql database and see wether connected or not:
    which type of project should i chose “empty” “gui” or “qt consol” type application
    what lines should i add in *.pro file.
    and what are the essential code lines in main file to connect database “student” with user “root” password “password” and host as “localhost”. I have a code may i send it to you so that u can tell me the errors and lines which should be added.
    Please bro help me i m in a big trouble and i have only hope with you……..
    please help.

  • Deven

    hi can you tell me how to create a application with qt which can be run on other computers where qt is not installed or how to make an installer file independent of qt.

  • Jeremiah

    Make sure to get version 0.3 of the mingw utilities.

    The version 0.4-1 available for download does not work work and reports:

    reimp: libmysql.lib: invalid or corrupt import library

  • Kan

    THanks again Chris for this tutorial. I have done everything and my qt application can connect to MySql server successfully. I have three drivers loaded: QMYSQL3 QMYSQL and QSQLITE.But i have one problem. I am able to run all INSERT, CREATE TABLE, UPDATE … queries. But when I connect and try to run a SELECT query using QSqlQuery, i get an error which says:

    QMYSQL3: Unable to fetch data

    I thought Qt should be using the QMYSQL driver and not the QMYSQL3 driver. Do you know what could be causing this?
    I am using the 2009.04 version of Qt with mysql 5.0.45

    • I have seen this myself, but can’t for the life of me remember what caused it or how I fixed it. Perhaps you could have a look at which files Qt loads for the various plugins and see if that gives you some clues? If you set environment variable QT_DEBUG_PLUGINS=1. The program should then spew out a whole lot of information when it loads the plugins.

      Good luck!

  • andy

    Hi, is there a big difference, when trying this on windows 7?

    Because I tried and well, didn’t work for me. I did everything as described, except the part where to fix qmake, because I got:
    “mingw32-make: *** No rule to make target `clean’. Stop.”

    So I did everything else as described and got many undefined references:

    undefined reference to ‘vtable for WebCore::FormDataIODevice’
    undefined reference to ‘vtable for WebCore::QNetworkReplyHandler’
    undefined reference to ‘QWebPage::unsupportedContent(QNetworkReply*)’
    undefined reference to ‘QWebPage::downloadRequested(QNetworkRequest &const)’

    Funny thing, I got the MySQL driver, back when i had WinXP, but with another tutorial, unfortunately I can’t find it anymore.
    I have no idea what to do now.
    Maybe someone here could help, thx a lot.

  • Yonathan

    hai Christ, i’m from Indonesia and i’ve follow your instruction in this tutorial.I got same problem with abdulrc.

    When running command C:\Qt\2009.05\qt\qmake>mingw32-make clean
    there is message : mingw32-make: *** No rule to make target `clean’. Stop.

    In qmake directory there is Makefile.unix,Makefile.win32, Makefile.win32-g++,Makefile.win32-g++-sh

    What should i do. Thank u in advance

  • Reece

    Thanks a lot for this post. It has helped something I’ve been working on for a few days now.

    Everything works splendidly until the end, when I try and rebuild qt.

    Upon running mingw32-make sub-src, I get normal building messages until it comes up with over 1000 errors along the lines of:
    Undefined refernce to QByteArray

    I have scoured the Internet for a solution, but I can find none. It seems to be a linker error. Any ideas?

  • ForestDweller

    Thank you Thank you Thank you

    It’s 4:00AM and I can now go to bed. Aside from the missing slashes, your tutorial for building the MySQL drivers worked perfectly. However, I had to find a windoze box with the MySQL server installed in order to find the libmysql.dll (the piece that I was missing on the target machine which has kept me up so late). I really appreciate you efforts!

    Great job!

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Spam protection by WP Captcha-Free