Qt项目里用到了mysql,运行后报错:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
查阅资料知道需要配置mysql驱动,本以为几分钟解决的事情,没想到开启了恶魔经历,整整两天时间,mmp!
Stop! 废话别说啦,入正题:
首先,Qt & mysql的安装和环境配置就略了,一搜一大把。
不过注意两点:
- Qt在安装的时候 选择组件的时候 除了选择macosx 之外 还要选择resource 选项!!!切记,已安装的如果没选择,就重新安装吧
- 我这里的mysql是官网下载的dmg安装,安装后会在/usr/local/下生成如:
mysql
mysql-8.0.20-macos10.15-x86_64
两个,后续我们主要用到mysql这个
第一步
先进入到/Users/ing/Qt5.14.2/5.14.2/Src/qtbase/src/plugins/sqldrivers目录下,执行:
/Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -- MYSQL_PREFIX=/usr/local/mysql
这里确保qmake用的qt里的就行
其实你可以配置下qt相关的环境变量,在bash_profile里添加:
export QTDIR=/Users/ing/Qt5.14.2/5.14.2/clang_64
export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH
export PATH=$QTDIR/bin:$PATH
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/mysql/lib
然后,source ~/.bash_profile ,还有别忘了 source ~/.zshrc
执行成功的话会输出:
Running configuration tests...
Done running configuration tests.
Configure summary:
Qt Sql Drivers:
DB2 (IBM) .............................. no
InterBase .............................. no
MySql .................................. yes
OCI (Oracle) ........................... no
ODBC ................................... no
PostgreSQL ............................. no
SQLite2 ................................ no
SQLite ................................. yes
Using system provided SQLite ......... no
TDS (Sybase) ........................... no
Qt is now configured for building. Just run 'make'.
Once everything is built, you must run 'make install'.
Qt will be installed into '/Users/ing/Qt5.14.2/5.14.2/clang_64'.
Prior to reconfiguration, make sure you remove any leftovers from
the previous build.
根据提示 依次执行:
make sub-mysql
cd mysql/ && ( test -e Makefile || /Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -o Makefile /Users/ing/Qt5.14.2/5.14.2/Src/qtbase/src/plugins/sqldrivers/mysql/mysql.pro ) && /Applications/Xcode.app/Contents/Developer/usr/bin/make -f Makefile
make[1]: Nothing to be done for `first'.
make
cd mysql/ && ( test -e Makefile || /Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -o Makefile /Users/ing/Qt5.14.2/5.14.2/Src/qtbase/src/plugins/sqldrivers/mysql/mysql.pro ) && /Applications/Xcode.app/Contents/Developer/usr/bin/make -f Makefile
make[1]: Nothing to be done for `first'.
cd sqlite/ && ( test -e Makefile || /Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -o Makefile /Users/ing/Qt5.14.2/5.14.2/Src/qtbase/src/plugins/sqldrivers/sqlite/sqlite.pro ) && /Applications/Xcode.app/Contents/Developer/usr/bin/make -f Makefile
make[1]: Nothing to be done for `first'.
make install
cd mysql/ && ( test -e Makefile || /Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -o Makefile /Users/ing/Qt5.14.2/5.14.2/Src/qtbase/src/plugins/sqldrivers/mysql/mysql.pro ) && /Applications/Xcode.app/Contents/Developer/usr/bin/make -f Makefile install
/Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -install qinstall -exe ../plugins/sqldrivers/libqsqlmysql.dylib /Users/ing/Qt5.14.2/5.14.2/clang_64/plugins/sqldrivers/libqsqlmysql.dylib
/Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -install qinstall /Users/ing/Qt5.14.2/5.14.2/Src/qtbase/src/plugins/sqldrivers/plugins/sqldrivers/libqsqlmysql.dylib.dSYM/Contents/Info.plist /Users/ing/Qt5.14.2/5.14.2/clang_64/plugins/sqldrivers/libqsqlmysql.dylib.dSYM/Contents/Info.plist
/Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -install qinstall /Users/ing/Qt5.14.2/5.14.2/Src/qtbase/src/plugins/sqldrivers/plugins/sqldrivers/libqsqlmysql.dylib.dSYM/Contents/Resources/DWARF/libqsqlmysql.dylib /Users/ing/Qt5.14.2/5.14.2/clang_64/plugins/sqldrivers/libqsqlmysql.dylib.dSYM/Contents/Resources/DWARF/libqsqlmysql.dylib
/Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -install qinstall /Users/ing/Qt5.14.2/5.14.2/Src/qtbase/src/plugins/sqldrivers/lib/cmake/Qt5Sql/Qt5Sql_QMYSQLDriverPlugin.cmake /Users/ing/Qt5.14.2/5.14.2/clang_64/lib/cmake/Qt5Sql/Qt5Sql_QMYSQLDriverPlugin.cmake
cd sqlite/ && ( test -e Makefile || /Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -o Makefile /Users/ing/Qt5.14.2/5.14.2/Src/qtbase/src/plugins/sqldrivers/sqlite/sqlite.pro ) && /Applications/Xcode.app/Contents/Developer/usr/bin/make -f Makefile install
/Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -install qinstall -exe ../plugins/sqldrivers/libqsqlite.dylib /Users/ing/Qt5.14.2/5.14.2/clang_64/plugins/sqldrivers/libqsqlite.dylib
/Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -install qinstall /Users/ing/Qt5.14.2/5.14.2/Src/qtbase/src/plugins/sqldrivers/plugins/sqldrivers/libqsqlite.dylib.dSYM/Contents/Info.plist /Users/ing/Qt5.14.2/5.14.2/clang_64/plugins/sqldrivers/libqsqlite.dylib.dSYM/Contents/Info.plist
/Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -install qinstall /Users/ing/Qt5.14.2/5.14.2/Src/qtbase/src/plugins/sqldrivers/plugins/sqldrivers/libqsqlite.dylib.dSYM/Contents/Resources/DWARF/libqsqlite.dylib /Users/ing/Qt5.14.2/5.14.2/clang_64/plugins/sqldrivers/libqsqlite.dylib.dSYM/Contents/Resources/DWARF/libqsqlite.dylib
/Users/ing/Qt5.14.2/5.14.2/clang_64/bin/qmake -install qinstall /Users/ing/Qt5.14.2/5.14.2/Src/qtbase/src/plugins/sqldrivers/lib/cmake/Qt5Sql/Qt5Sql_QSQLiteDriverPlugin.cmake /Users/ing/Qt5.14.2/5.14.2/clang_64/lib/cmake/Qt5Sql/Qt5Sql_QSQLiteDriverPlugin.cmake
第二步
进入/Users/ing/Qt5.14.2/5.14.2/clang_64/plugins/sqldrivers下执行,查看链接库:
otool -L libqsqlmysql.dylib
输出:
libqsqlmysql.dylib:
libqsqlmysql.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/QtSql.framework/Versions/5/QtSql (compatibility version 5.14.0, current version 5.14.2)
@rpath/QtCore.framework/Versions/5/QtCore (compatibility version 5.14.0, current version 5.14.2)
/System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
@rpath/libmysqlclient.21.dylib (compatibility version 21.0.0, current version 21.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.4)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.250.1)
此时,运行qt程序 依旧报错。
根据查阅的资料,问题出在
@rpath/libmysqlclient.21.dylib (compatibility version 21.0.0, current version 21.0.0)
网友们所说的“诡异”的路径那一行(你的输出可能和我的不一样的,但问题应该一样) ,也就是有libmysqlclient.21.dylib 这行。
另外查了下@repath 的含义 意思是说 不能明确的指向。。。所以 这里就需要将libmysqlclient.21.dylib 指向扳到正确的道路。
方法就是利用 install_name_tool -change
第三步
执行
install_name_tool -change @rpath/libmysqlclient.21.dylib /usr/local/mysql/lib/libmysqlclient.21.dylib /Users/ing/Qt5.14.2/5.14.2/clang_64/plugins/sqldrivers/libqsqlmysql.dylib
这里一定要注意顺序,刚开始我就是顺序搞错了,死活就是不行,最后跑到官方论坛里用蹩脚的英语发帖求教也无果,最后还是一遍的重试,一遍的检查才发现的。一天就这么浪费了。
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL
此刻在运行qt程序 ,就不会再报上面的错误了。
撒花,礼花搞起~~~
网友评论