Tag Archives: QT

Recompiling Qt’s driver for MySQL and for MinGW64

I’m currently working on some project developed with the Qt framework.

In this project I need to connect to a MySQL database, but Qt doesn’t include the libraries for licensing problems, so you need to compile them yourself.

In the Qt documentation is told how to do it but I found it inaccurate, and I had to do lots of research to manage and here is how I did it.

I have Qt in the path C:\Qt and the version 6.4.0. you may have other versions though. There are the versions 6.3.2, 6.2.4 or even 5.15.2.
Of whatever version you might have you need also the sources.

In my case the sources are in C:\Qt\6.4.0\Src\

you will also need to download the mysql-connector-c.6.1.11 from https://downloads.mysql.com/archives/c-c/?version=6.1.11&os=src

In the Qt’s documentation they use mysql-8.0.22-winx64. In my example I choose mysql-connector-c-6.1.11-winx64. You can pick the 32 or 64 versions.

It’s important that you download the zip file, and you unzip it in a path without spaces. In my case the path is C:\mysql-connector-c-6.1.11-winx64

If you installed it from the .msi it won’t probably work because it’s in C:\Program Files\MySQL\MySQL Connector C 6.1 which contains spaces.

Now you can go to C:\Qt\6.4.0\Src\qtbase\src\plugins\sqldrivers and create a building folder and configure, build and install the drivers with the following commands:

cd C:\Qt\6.4.0\Src\qtbase\src\plugins\sqldrivers
mkdir build-sqldrivers
cd build-sqldrivers
qt-cmake ^
	-D CMAKE_MAKE_PROGRAM="C:\Qt\Tools\Ninja\ninja.exe" ^
	-G Ninja C:\Qt\6.4.0\Src\qtbase\src\plugins\sqldrivers ^
	-D CMAKE_INSTALL_PREFIX="C:\Qt\6.4.0\mingw_64" ^
	-D MySQL_INCLUDE_DIR="C:\mysql-connector-c-6.1.11-winx64\include" ^
	-D MySQL_LIBRARY="C:\mysql-connector-c-6.1.11-winx64\lib\libmysql.lib"

If all goes well, you will see this screen:

Then you just need to run these two commands:

cmake --build .
cmake --install .

Obviously, you need to set your own paths.

The Qt documentation doesn’t seem to include the param
-D MAKE_MAKE_PROGRAM=”C:\Qt\Tools\Ninja\ninja.exe“.
This is key because otherwise it won’t find the Ninja program.

Once this is done, you need to do something else and that is to copy the file C:\mysql-connector-c-6.1.11-winx64\lib\libmysql.dll either in the folder where you generate your exe or in the path C:\Qt\6.4.0\mingw_64\bin

Also, you need to distribute your app along this file.

I hope it helps.