简体   繁体   中英

Qt C++ QMYSQL driver not loaded on MacOS - Tutorial for building QMYSQL Plugin

I just spent a day figuring out how to build the drivers for MySQL support in a Qt-Application, because the explanation given in the Qt-documentation was not clear enough for me (even misleading, actually) so I thought I'd share the steps that worked for me.

I did this on MacOS 10.15.3, with mysql 8.0.19 and Qt 5.14.1.

First, you probably need to install mysql. I ran these three commands, not sure if all of them are necessary:

brew install mysql
brew install mysql-client
brew install mysql-connector-c

This installed mysql into /usr/local/Cellar/mysql/8.0.19 and the client into /usr/local/Cellar/mysql-client/8.0.18 .

After installing the client, my terminal prompted me to set compiler flags:

  export LDFLAGS="-L/usr/local/opt/mysql-client/lib"
  export CPPFLAGS="-I/usr/local/opt/mysql-client/include"

So I added these two lines to my .bash_profile-file. (This file sets environment variables every time you open a terminal. You can edit it by simply navigating to your home directory ( cd ~ ) and typing vim .bash_profile .)

I also added the mysql and mysql-client paths to the PATH-variable by adding these two lines to the .bash_profile:

export PATH=${PATH}:/usr/local/Cellar/mysql/8.0.19
export PATH=${PATH}:/usr/local/Cellar/mysql-client/8.0.18

And while we're at it, I also added the path to Qt's qmake-tool:

export PATH=${PATH}:/Users/USERNAME/Qt/5.14.1/clang_64/bin (replace USERNAME with your username)

Now we're all set up. Navigate to the sqldrivers-folder in your Qt-installation, for me that's /Users/USERNAME/Qt/5.14.1/Src/qtbase/src/plugins/sqldrivers . (btw, installing Qt via brew didn't work for me, I had to download the installer from their website.)

In that folder, you need to run qmake with the full path to your mysql-dylib-files, like so:

qmake -- MYSQL_PREFIX=/usr/local/Cellar/mysql/8.0.19/lib

(note how this is different from the Qt-documentation, where they tell you that the prefix is /usr/local - this is why I called it misleading.) Then, still in the sqldrivers-folder, run make . Then, navigate into the mysql-subfolder ( cd mysql ) and run make install . I was then able to compile and run my project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM