ubuntu qt5 编译libqsqlmysql.so并测试(能运行)
ubuntu qt5 编译libqsqlmysql.so
首先确保已经安装了mysql,
然后打开
/opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql
目录查看是否存在
如果存在,进行libmysql.so文件的编译
root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# pwd /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2 root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# vim mysql.pro root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# cd ../ root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# vim qsqldriverbase.pri root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# vim qsqldriverbase.pri root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers#
root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# cd mysql2 root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2#
root@luo-ThinkPad-W540:mysql2#
root@luo-ThinkPad-W540:mysql2#
root@luo-ThinkPad-W540:mysql2# sudo /opt/Qt5.12.0/5.12.0/gcc_64/bin/qmake -- MYSQL_PREFIX=/opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2
root@luo-ThinkPad-W540:mysql2#
root@luo-ThinkPad-W540:mysql2#
root@luo-ThinkPad-W540:mysql2#
root@luo-ThinkPad-W540:mysql2# ls
main.cpp Makefile mysql.json mysql.pro qsql_mysql.cpp qsql_mysql_p.h README
root@luo-ThinkPad-W540:mysql2#
root@luo-ThinkPad-W540:mysql2#
root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# cat mysql.pro TARGET = qsqlmysql HEADERS += $$PWD/qsql_mysql_p.h SOURCES += $$PWD/qsql_mysql.cpp $$PWD/main.cpp #QMAKE_USE += mysql OTHER_FILES += mysql.json unix:LIBS += -L/usr/lib/x86_64-linux-gnu/ -lmysqlclient PLUGIN_CLASS_NAME = QMYSQLDriverPlugin include(../qsqldriverbase.pri) root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# cp /usr/include/mysql/mysql.h /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2 root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# cp /usr/include/mysql/mysql_version.h /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2 root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# cp /usr/include/mysql/mysql_com.h /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2 root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# cp /usr/include/mysql/binary_log_types.h /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2 root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# cp /usr/include/mysql/mysql_time.h /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2 root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# cp /usr/include/mysql/my_list.h /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2 root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# mkdir mysql root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# cp /usr/include/mysql/mysql/client_plugin.h /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2/mysql/client_plugin.h root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# cp /usr/include/mysql/plugin_auth_common.h /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2 root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# cp /usr/include/mysql/typelib.h /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2 root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# cp /usr/include/mysql/my_alloc.h /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2 root@luo-ThinkPad-W540:mysql2#
root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2# sudo make g++ -c -pipe -O2 -g -std=c++1y -fvisibility=hidden -fvisibility-inlines-hidden -fno-exceptions -Wall -W -Wvla -Wdate-time -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_NO_EXCEPTIONS -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_NO_DEBUG -DQT_PLUGIN -DQT_SQL_LIB -DQT_CORE_LIB -I. -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql/5.12.0 -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql/5.12.0/QtSql -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore/5.12.0 -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore/5.12.0/QtCore -I/opt/Qt5.12.0/5.12.0/gcc_64/include -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore -I.moc -I/opt/Qt5.12.0/5.12.0/gcc_64/mkspecs/linux-g++ -o .obj/qsql_mysql.o qsql_mysql.cpp g++ -pipe -O2 -g -std=c++1y -fvisibility=hidden -fvisibility-inlines-hidden -fno-exceptions -Wall -W -Wvla -Wdate-time -dM -E -o .moc/moc_predefs.h /opt/Qt5.12.0/5.12.0/gcc_64/mkspecs/features/data/dummy.cpp /opt/Qt5.12.0/5.12.0/gcc_64/bin/moc -DQT_DEPRECATED_WARNINGS -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_NO_EXCEPTIONS -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_NO_DEBUG -DQT_PLUGIN -DQT_SQL_LIB -DQT_CORE_LIB --include /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2/.moc/moc_predefs.h -I/opt/Qt5.12.0/5.12.0/gcc_64/mkspecs/linux-g++ -I/opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2 -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql/5.12.0 -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql/5.12.0/QtSql -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore/5.12.0 -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore/5.12.0/QtCore -I/opt/Qt5.12.0/5.12.0/gcc_64/include -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore -I/usr/include/c++/7 -I/usr/include/x86_64-linux-gnu/c++/7 -I/usr/include/c++/7/backward -I/usr/lib/gcc/x86_64-linux-gnu/7/include -I/usr/local/include -I/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed -I/usr/include/x86_64-linux-gnu -I/usr/include main.cpp -o .moc/main.moc g++ -c -pipe -O2 -g -std=c++1y -fvisibility=hidden -fvisibility-inlines-hidden -fno-exceptions -Wall -W -Wvla -Wdate-time -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_NO_EXCEPTIONS -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_NO_DEBUG -DQT_PLUGIN -DQT_SQL_LIB -DQT_CORE_LIB -I. -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql/5.12.0 -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql/5.12.0/QtSql -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore/5.12.0 -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore/5.12.0/QtCore -I/opt/Qt5.12.0/5.12.0/gcc_64/include -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore -I.moc -I/opt/Qt5.12.0/5.12.0/gcc_64/mkspecs/linux-g++ -o .obj/main.o main.cpp /opt/Qt5.12.0/5.12.0/gcc_64/bin/moc -DQT_DEPRECATED_WARNINGS -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_NO_EXCEPTIONS -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_NO_DEBUG -DQT_PLUGIN -DQT_SQL_LIB -DQT_CORE_LIB --include /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2/.moc/moc_predefs.h -I/opt/Qt5.12.0/5.12.0/gcc_64/mkspecs/linux-g++ -I/opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/mysql2 -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql/5.12.0 -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql/5.12.0/QtSql -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore/5.12.0 -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore/5.12.0/QtCore -I/opt/Qt5.12.0/5.12.0/gcc_64/include -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore -I/usr/include/c++/7 -I/usr/include/x86_64-linux-gnu/c++/7 -I/usr/include/c++/7/backward -I/usr/lib/gcc/x86_64-linux-gnu/7/include -I/usr/local/include -I/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed -I/usr/include/x86_64-linux-gnu -I/usr/include qsql_mysql_p.h -o .moc/moc_qsql_mysql_p.cpp g++ -c -pipe -O2 -g -std=c++1y -fvisibility=hidden -fvisibility-inlines-hidden -fno-exceptions -Wall -W -Wvla -Wdate-time -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_NO_EXCEPTIONS -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_NO_DEBUG -DQT_PLUGIN -DQT_SQL_LIB -DQT_CORE_LIB -I. -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql/5.12.0 -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql/5.12.0/QtSql -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore/5.12.0 -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore/5.12.0/QtCore -I/opt/Qt5.12.0/5.12.0/gcc_64/include -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore -I.moc -I/opt/Qt5.12.0/5.12.0/gcc_64/mkspecs/linux-g++ -o .obj/moc_qsql_mysql_p.o .moc/moc_qsql_mysql_p.cpp rm -f libqsqlmysql.so g++ -Wl,--no-undefined -Wl,--enable-new-dtags -Wl,-z,origin -Wl,-rpath,\$ORIGIN/../../lib -Wl,-rpath,\$ORIGIN/../../lib -shared -o libqsqlmysql.so .obj/qsql_mysql.o .obj/main.o .obj/moc_qsql_mysql_p.o -L/usr/lib/x86_64-linux-gnu/ -lmysqlclient -L/opt/Qt5.12.0/5.12.0/gcc_64/lib -lQt5Sql -lQt5Core -lpthread mv -f libqsqlmysql.so ../plugins/sqldrivers/libqsqlmysql.so objcopy --only-keep-debug ../plugins/sqldrivers/libqsqlmysql.so ../plugins/sqldrivers/libqsqlmysql.so.debug && objcopy --strip-debug ../plugins/sqldrivers/libqsqlmysql.so && objcopy --add-gnu-debuglink=../plugins/sqldrivers/libqsqlmysql.so.debug ../plugins/sqldrivers/libqsqlmysql.so && chmod -x ../plugins/sqldrivers/libqsqlmysql.so.debug root@luo-ThinkPad-W540:mysql2# root@luo-ThinkPad-W540:mysql2#
输出在
root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# ls libqsqlmysql.so libqsqlmysql.so.debug root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# pwd /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/plugins/sqldrivers root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers#
拷贝到:/opt/Qt5.12.0/5.12.0/gcc_64/plugins/sqldrivers/
root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# cp /opt/Qt5.12.0/5.12.0/Src/qtbase/src/plugins/sqldrivers/plugins/sqldrivers/* /opt/Qt5.12.0/5.12.0/gcc_64/plugins/sqldrivers/ root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers#
测试
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QSqlDatabase> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QStringList drivers = QSqlDatabase::drivers(); //获取现在可用的数据库驱动 foreach(QString driver, drivers) qDebug() << driver; QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); //主机名称,如localhost db.setDatabaseName("mask_rcnn"); //数据库名称 db.setPort(3306); //数据库端口号 db.setUserName("root"); //用户名称 db.setPassword("123456"); //用户密码 //bool open(const QString& user, const QString& password) //db.open(); bool isConnect = db.open("root","123456"); qDebug()<<"connect state:"<<isConnect; //close connect db.close(); } MainWindow::~MainWindow() { delete ui; }
连接运行日志
16:05:15: Starting /home/luo/Desktop/MyFile/QtProject/build-CheckConnectMySql-Desktop_Qt_5_12_0_GCC_64bit-Debug/CheckConnectMySql... "QSQLITE" "QMYSQL" "QMYSQL3" "QPSQL" "QPSQL7" connect state: true 16:05:18: /home/luo/Desktop/MyFile/QtProject/build-CheckConnectMySql-Desktop_Qt_5_12_0_GCC_64bit-Debug/CheckConnectMySql exited with code 0
参考:https://blog.csdn.net/weixin_43790050/article/details/106162868
########################################