Thursday, November 7, 2013

Building MariaDB driver for Qt v5.2 (beta) on Windows

Due to licensing limitations Qt can't ship the MySQL driver (MariaDB is a drop in replacement for MySQL) with its distribution and so the developer is required to build it from source. Although the base documentation is "ok" for doing this, it wasn't something that I was able to read and get right the first time through and so I figured I'd go ahead and share my experience to hopefully help alleviate any issues others might have in relation to this procedure.

The steps I followed were:

1. Navigate to the plugin project code that ships with Qt (for the v5.2 beta default install this was located at C:\Qt\Qt5.2.0\5.2.0-beta1\Src\qtbase\src\plugins\sqldrivers\mysql)

2. Open the mysql.pro project within QtCreator

3. Right-click on the top level mysql project within the Project explorer pane and select the "Add Library..." option

4. Select the "External library" option in the dialog that pops up

5. Select the "Browse..." button next to the Library file text box

6. Navigate to the MariaDB lib directory (I installed the 64-bit version of MariaDB and so on my system this is located at C:\Program Files\MariaDB 5.5\lib)

7. Select the "libmysql.lib" library file

8. After selecting the library file in step 7, by default, the include path will have a value of "C:\Program Files\MariaDB 5.5\include" which is not correct. To fix this, you can either use the "Browse..." button next to the Include path text box or you can simply edit the text box to append "mysql" to the end of the path

9. Select your additional options - I'm statically linking this on Windows for production and so my settings look like the screenshot:


10. The project file should now have the following appended to the end:

win32: LIBS += -L$$PWD/../../../../../../../../../Program Files/MariaDB 5.5/lib/ -llibmysql

INCLUDEPATH += $$PWD/../../../../../../../../../Program Files/MariaDB 5.5/include/mysql
DEPENDPATH += $$PWD/../../../../../../../../../Program Files/MariaDB 5.5/include/mysql

win32: PRE_TARGETDEPS += $$PWD/../../../../../../../../../Program Files/MariaDB 5.5/lib/libmysql.lib

Because windows and unix environments, in general, don't play well together this addition does not work as it stands since qmake cannot deal with the space in the path. To resolve this, simply surround each path with quotes to end up with the following:

win32: LIBS += -L"$$PWD/../../../../../../../../../Program Files/MariaDB 5.5/lib/" -llibmysql
INCLUDEPATH += "$$PWD/../../../../../../../../../Program Files/MariaDB 5.5/include/mysql"
DEPENDPATH += "$$PWD/../../../../../../../../../Program Files/MariaDB 5.5/include/mysql"

win32: PRE_TARGETDEPS += "$$PWD/../../../../../../../../../Program Files/MariaDB 5.5/lib/libmysql.lib"

11. Run qmake and then rebuild the project and you should have a working MariaDB driver

The contents of the final qt project file for this project is below. If you've installed everything in its default location, you should be able to simply copy this to your project file and build with no other modifications.

TARGET = qsqlmysql

SOURCES = main.cpp
OTHER_FILES += mysql.json
include(../../../sql/drivers/mysql/qsql_mysql.pri)

PLUGIN_CLASS_NAME = QMYSQLDriverPlugin
include(../qsqldriverbase.pri)

win32: LIBS += -L"$$PWD/../../../../../../../../../Program Files/MariaDB 5.5/lib/" -llibmysql

INCLUDEPATH += "$$PWD/../../../../../../../../../Program Files/MariaDB 5.5/include/mysql"
DEPENDPATH += "$$PWD/../../../../../../../../../Program Files/MariaDB 5.5/include/mysql"

win32: PRE_TARGETDEPS += "$$PWD/../../../../../../../../../Program Files/MariaDB 5.5/lib/libmysql.lib"


One final note about the output: I'm not entirely sure why, but the output is not put in the place defined by the project file. This may be a bug with QtCreator 3.0 (it is still beta, after all), but regardless, the output on my machine is placed at c:\plugins\sqldrivers. I had to do a full hard drive scan to find this. Seeing as how you'll only need to build this if you upgrade Qt or MariaDB, I didn't invest the time to figure out what was going on here?




No comments:

Post a Comment