Sunday, December 5, 2010

Installing Qt4 with MySQL plugin (QMYSQL) in Windows (gcc)

Note: If you feel like you already tried everything, check the last highlighted paragraph, may be useful : )

I needed Qt4 with MySQL support under windows to develop my high school final project. The thing is i had some trouble setting it up, but i found a way eventually : )

The same way it worked for me, i hope it works for you too! ^^

So, to begin download a qt sdk package: http://qt.nokia.com/downloads
Install it somewhere and make sure it is ready to use without the mysql support.

Open the Qt Command Prompt, that comes within the SDK installation, it will be used for getting the mysql library compatible with mingw32, then feeding qt the needed files, and finnaly recompile the package.

Next thing in the list, download the mysql library: http://www.mysql.com/downloads/mysql/ install it in somewhere easy to go in the console. Make sure you install the header files, in the installation options.
Download the mingw-utils: https://olex.openlogic.com/packages/mingw-utils You will need a tool contained in it.

Take the reimp tool from the mingw-utils package, and place it in mingw bin folder, same place you will see a dlltool executable. This should be in the Qt installation, in a mingw subfolder, look for it : )

In the console, go to the mysql library folder you installed a bit ago. Then change directory to /lib/opt and run the following commands as shown in: http://www.rag.com.au/linux/qt4howto.html

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


By now, you have everything Qt4 needs to build MySQL support but Qt does not know where to find it yet. Just move that libmysql.a file you generated with the previous command and put it in the mingw directory, under the lib subdirectory. Then copy all the files in %mysqlinstall%/include to the mingw directory, under include subdirectory. The libraries are all set up for qt installation.


Now just do the traditional configure step to rebuild. In the console, go to qt default directory, where configure.exe is.


Run:
configure -platform win32-g++ -qt-sql-mysql (Note: you can add whatever options you would like, to complement your rebuild, your choice)
mingw32-make


This will take a lot of time, probably more than 2 hours. Be patient : )


While you wait, there is one more thing to solve. When i did all this, my Qt4 was still not able to load the QMYSQL plugin, and since everything was installed correctly, it was a runtime issue. In theory, it would run just fine by adding the libmysql.dll to the qt bin directory, or to the application directory. but this wouldn't help. 


[ Luckily, i found the way around with the dependency walker. The application was requesting libmysql.a instead of libmysql.dll, so the final step:
Copy from the mysql installation, in the bin directory, the libmysql.dll, paste it into the qt bin directory, where you will find a lot of other dll files. After this, rename the libmysql.dll to libmysql.a.]


After all these steps, your Qt4 should be working as charm. Hope it helps you. Have fun ! : )

1 comment: