未加载 QMYSQL 驱动程序,尝试了所有提示

Posted

技术标签:

【中文标题】未加载 QMYSQL 驱动程序,尝试了所有提示【英文标题】:QMYSQL driver not loaded, tried all hints 【发布时间】:2014-06-20 23:34:08 【问题描述】:

我正在使用 QT-Creator 作为 Mac os 10.8 上的 IDE 使用 QT 5.3 编写 C++ 应用程序。 当我尝试使用

加载 Qmysql 插件时出现问题
db = QSqlDatabase::addDatabase("QMYSQL");

结果

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7

所以我尝试了

loader.setFileName("/Users/kilian/Qt/5.3/clang_64/plugins/sqldrivers/libqsqlmysql_debug.dylib");
qDebug() << loader.load();
qDebug() << loader.errorString();

获得更好的输出

"Die Bibliothek /Users/foob/Qt/5.3/clang_64/plugins/sqldrivers/libqsqlmysql_debug.dylib kann nicht geladen werden:
(dlopen(/Users/foob/Qt/5.3/clang_64/plugins/sqldrivers/libqsqlmysql_debug.dylib, 5):
Library not loaded: libmysqlclient.18.dylib
Referenced from: /Users/foob/Qt/5.3/clang_64/plugins/sqldrivers/libqsqlmysql_debug.dylib
Reason: image not found)"

所以我尝试了otool

otool -L build-$appname-Desktop-Debug/$appname.app/Contents/MacOS/$appname 
build-$appname-Desktop-Debug/$appname.app/Contents/MacOS/$appname:
/usr/local/mysql/lib/libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
/Users/foob/Qt/5.3/clang_64/lib/QtWidgets.framework/Versions/5/QtWidgets (compatibility version 5.3.0, current version 5.3.0)
/Users/foob/Qt/5.3/clang_64/lib/QtGui.framework/Versions/5/QtGui (compatibility version 5.3.0, current version 5.3.0)
/Users/foob/Qt/5.3/clang_64/lib/QtCore.framework/Versions/5/QtCore (compatibility version 5.3.0, current version 5.3.0)
/Users/foob/Qt/5.3/clang_64/lib/QtSql.framework/Versions/5/QtSql (compatibility version 5.3.0, current version 5.3.0)
/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/AGL.framework/Versions/A/AGL (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

作为参考,我自己构建了 QMYSQL 插件

$cd Qt/5.3/Src/qtbase/src/plugins/sqldrivers/mysql/

$qmake "INCLUDEPATH+=/usr/local/mysql/include" "LIBS+=-L/usr/local/mysql/lib -lmysqlclient_r" mysql.pro
$ make
make -f Makefile.Release all
make[1]: Nothing to be done for `all'.
make -f Makefile.Debug all
make[1]: Nothing to be done for `all'.
$ make install
make -f Makefile.Release install
cp -f "../../../../plugins/sqldrivers/libqsqlmysql.dylib" "/Users/foob/Qt/5.3/clang_64/plugins/sqldrivers/libqsqlmysql.dylib"
cp -f /Users/foob/Qt/5.3/Src/qtbase/lib/cmake/Qt5Sql/Qt5Sql_QMYSQLDriverPlugin.cmake /Users/foob/Qt/5.3/clang_64/lib/cmake/Qt5Sql/
make -f Makefile.Debug install
cp -f "../../../../plugins/sqldrivers/libqsqlmysql_debug.dylib" "/Users/foob/Qt/5.3/clang_64/plugins/sqldrivers/libqsqlmysql_debug.dylib"
cp -f /Users/foob/Qt/5.3/Src/qtbase/lib/cmake/Qt5Sql/Qt5Sql_QMYSQLDriverPlugin.cmake /Users/foob/Qt/5.3/clang_64/lib/cmake/Qt5Sql/

Everythink 看起来不错,我在 /etc/profile 中设置了较早的 DYLD_LIBRARY_PATH 导出 DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

echo $DYLD_LIBRARY_PATH

结果

/usr/local/mysql/lib:

文件夹 /usr/local/mysql/lib :

$ls -lisa /usr/local/mysql/lib/
total 556840
2147230      0 drwxr-xr-x  12 root  wheel        408 21 Jun 00:01 .
2147093      0 drwxr-xr-x  17 root  wheel        578 21 Jun 00:01 ..
2147231   8384 -rwxr-xr-x   1 root  wheel    4289380  6 Mai 13:18 libmysqlclient.18.dylib
2147232  20744 -rw-r--r--   1 root  wheel   10620040  6 Mai 13:18 libmysqlclient.a
2147233      8 lrwxr-xr-x   1 root  wheel         23 21 Jun 00:01 libmysqlclient.dylib -> libmysqlclient.18.dylib
2147234      8 lrwxr-xr-x   1 root  wheel         23 21 Jun 00:01 libmysqlclient_r.18.dylib -> libmysqlclient.18.dylib
2147235      8 lrwxr-xr-x   1 root  wheel         16 21 Jun 00:01 libmysqlclient_r.a -> libmysqlclient.a
2147236      8 lrwxr-xr-x   1 root  wheel         20 21 Jun 00:01 libmysqlclient_r.dylib -> libmysqlclient.dylib
2147237 261360 -rw-r--r--   1 root  wheel  133814816  6 Mai 13:08 libmysqld-debug.a
2147239 266288 -rw-r--r--   1 root  wheel  136335512  6 Mai 13:18 libmysqld.a
2147240     32 -rw-r--r--   1 root  wheel      14544  6 Mai 13:18 libmysqlservices.a
2147241      0 drwxr-xr-x  17 root  wheel        578  6 Mai 13:21 plugin

我正在使用的.pro文件的相关部分

macx: 
INCLUDEPATH += /usr/local/mysql/include \
               /Users/foob/Qt/5.3/clang_64/include


macx: 
LIBS += -L/usr/local/mysql/lib -lmysqld -lmysqlclient -v

有没有人暗示我做错了什么?

附:出于隐私考虑,我更改了用户和应用名称。

【问题讨论】:

【参考方案1】:

修复了这个问题,不知道它也可以将 otool 和 install_name_tool 用于 dylib。 只需使用

install_name_tool -change libqsqlmysql_debug.dylib /Users/foob/Qt/5.3/Src/qtbase/plugins/sqldrivers/libqsqlmysql_debug.dylib Qt/5.3/clang_64/plugins/sqldrivers/libqsqlmysql_debug.dylib

现在可以了。有谁知道为什么一开始就找不到这些库?我检查了 qmake 生成的 Makefile ,看起来不错。奇怪的Mac ...

【讨论】:

以上是关于未加载 QMYSQL 驱动程序,尝试了所有提示的主要内容,如果未能解决你的问题,请参考以下文章

Qt5、C++:未加载 QMYSQL 驱动程序

qt5.1.1 mysql ubuntu QMYSQL驱动未加载

qt 5.8 sql 连接错误:Windows 10 上未加载 QMYSQL 驱动程序

同样,未加载 QT 5.12.2 QMYSQL 驱动程序。图书馆虽然

PyQt5 QMYSQL 驱动程序未加载

QSqlDatabase:Mac OS 上未加载 QMYSQL 驱动程序