安装qt:
- 官网下载qt5.2.1:qt-opensource-linux-x64-5.2.1.run
- 直接命令行运行:./qt-opensource-linux-x64-5.2.1.run
- 选择安装路径:(我选择的是/home/myname/Qt)默认为/home/yourPCname/Qt5.2.1
- 一步一步安装,选择组建时记得勾选“Source Components”,否则后续重新编译sql的plugin可能需要重装qt
安装mysql,编写简单测试程序:
#include <QApplication> #include <QtSql> #include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "connA"); db.setHostName("localhost"); db.setDatabaseName("mysql"); db.setUserName("root"); db.setPassword("zymysql"); if(db.open()) { qDebug() << "connected!" << endl; exit(0); } else { qDebug() << " failed!" << endl; exit(0); } return a.exec(); }
编译运行,提示错误:
QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
1.查看sqldriver的依赖关系:
$cd ~/Qt/5.2.1/gcc_64/plugins/sqldrivers $ldd libqsqlmysql.so #得到以下: linux-vdso.so.1 => (0x00007ffe607ac000) libmysqlclient_r.so.16 => not found libQt5Sql.so.5 => /home/sharon/Qt/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libQt5Sql.so.5 (0x00007f4862374000) libQt5Core.so.5 => /home/sharon/Qt/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libQt5Core.so.5 (0x00007f4861ca6000) libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f48619a2000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f48615dd000) libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f48613bf000) libicui18n.so.51 => /home/sharon/Qt/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libicui18n.so.51 (0x00007f4860fa6000) libicuuc.so.51 => /home/sharon/Qt/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libicuuc.so.51 (0x00007f4860c20000) libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4860a1c000) libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007f486081a000) librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f4860612000) libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f486030a000) libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4860004000) libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f485fdee000) /lib64/ld-linux-x86-64.so.2 (0x00007f48627c6000) libicudata.so.51 => /home/sharon/Qt/5.2.1/gcc_64/plugins/sqldrivers/./../../lib/libicudata.so.51 (0x00007f485e6a5000) libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f485e467000)
2,查看安装的mysql的版本:
$ dpkg --get-selections | grep mysql #得到以下: ctions | grep mysql libdbd-mysql-perl install libmysqlclient-dev install libmysqlclient18:amd64 install mysql-client install mysql-client-5.5 install mysql-client-core-5.5 install mysql-common install mysql-server install mysql-server-5.5 install mysql-server-core-5.5 install
错误点在于:libmysqlclient_r.so.16 => not found,需要的是版本为16,安装的版本为18.
解决方案:为其重新编译sqldriver。
#进入到qt的源码目录下的此目录: $cd ~/Qt/5.2.1/Src/qtbase/src/plugins/sqldrivers/mysql #使用qmake编译: $ ~/Qt/5.2.1/gcc_64/bin/qmake "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lmysqlclient_r" mysql.pro $make $make install #再进入到gcc目录下查看libqsqlmysql.so的依赖关系: $cd ~/Qt/5.2.1/gcc_64/plugins/sqldrivers $ldd libqsqlmysql.so #依赖关系成功建立: linux-vdso.so.1 => (0x00007fff193b2000) libmysqlclient.so.18 => /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 (0x00007fc367f2a000) libQt5Sql.so.5 => /home/sharon/Qt/5.2.1/gcc_64/lib/libQt5Sql.so.5 (0x00007fc367cea000) libQt5Core.so.5 => /home/sharon/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5 (0x00007fc36761c000) libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fc367318000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc366f53000) libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fc366d3a000) libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc366b36000) libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc366918000) libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc366612000) libicui18n.so.51 => /home/sharon/Qt/5.2.1/gcc_64/lib/libicui18n.so.51 (0x00007fc3661f9000) libicuuc.so.51 => /home/sharon/Qt/5.2.1/gcc_64/lib/libicuuc.so.51 (0x00007fc365e73000) libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007fc365c71000) librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fc365a69000) libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fc365761000) libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fc36554b000) /lib64/ld-linux-x86-64.so.2 (0x00007fc368674000) libicudata.so.51 => /home/sharon/Qt/5.2.1/gcc_64/lib/libicudata.so.51 (0x00007fc363e02000) libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fc363bc4000)
重新编译运行,成功!
时间: 2024-10-05 21:06:12