Qt 项目不为 Qt SQL 模块提供 MySQL 数据库驱动程序,因此必须使用 ODBC 或手动编译插件。我正在尝试后者。我一直在使用带有 VS 2010 的预构建 Qt 4.8.1,但无法在“C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/”中找到插件的项目文件 (mysql.pro)正如许多教程使用的那样。有 qsql_mysql.pri,内容如下:
HEADERS += $$PWD/qsql_mysql.h
SOURCES += $$PWD/qsql_mysql.cpp
# modified! I added these:
#INCLUDEPATH += "C:/Program Files (x86)/MySQL/MySQL Server 5.6/include"
#LIBS += "C:/Program Files (x86)/MySQL/MySQL Server 5.6/lib/libmysql.lib"
unix {
isEmpty(QT_LFLAGS_MYSQL) {
!contains(LIBS, .*mysqlclient.*):!contains(LIBS, .*mysqld.*) {
use_libmysqlclient_r:LIBS += -lmysqlclient_r
else:LIBS += -lmysqlclient
}
} else {
LIBS *= $$QT_LFLAGS_MYSQL
QMAKE_CXXFLAGS *= $$QT_CFLAGS_MYSQL
}
} else {
!contains(LIBS, .*mysql.*):!contains(LIBS, .*mysqld.*):LIBS += -llibmysql
}
构建它会导致以下错误:
release\moc_qsql_mysql.cpp(45) : warning C4273: 'QMYSQLDriver::qt_static_metacall': Inconsistent DLL-binding.
C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h(104): See previous definition of 'qt_static_metacall'
release\moc_qsql_mysql.cpp(57) : warning C4273: 'staticMetaObjectExtraData': Inconsistent DLL-Binding.
C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h(104): See previous definition of 'private: static QMetaObjectExtraData const QMYSQLDriver::staticMetaObjectExtraData'
release\moc_qsql_mysql.cpp(57) : error C2491: 'QMYSQLDriver::staticMetaObjectExtraData': Definition of static member for dllimport not valid
release\moc_qsql_mysql.cpp(61) : warning C4273: 'staticMetaObject': Inconsistent DLL-binding.
C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h(104): See previous definition of 'public: static QMetaObject const QMYSQLDriver::staticMetaObject'
release\moc_qsql_mysql.cpp(61) : error C2491: 'QMYSQLDriver::staticMetaObject': Definition of static member for dllimport not valid
release\moc_qsql_mysql.cpp(71) : warning C4273: 'QMYSQLDriver::metaObject': Inconsistent DLL-binding.
C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h(104): See previous definition of 'metaObject'
release\moc_qsql_mysql.cpp(76) : warning C4273: 'QMYSQLDriver::qt_metacast': Inconsistent DLL-binding.
C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h(104): See previous definition of 'qt_metacast'
release\moc_qsql_mysql.cpp(84) : warning C4273: 'QMYSQLDriver::qt_metacall': Inconsistent DLL-binding.
C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h(104): See previous definition of 'qt_metacall'
c:\QtSDK\QtSources\4.8.1\src\sql\drivers\mysql\qsql_mysql.cpp(47) : fatal error C1083: File (Include) could not be opened: "qsqlerror.h": No such file or directory
当我尝试手动制作几乎相同的 mysql.pro 时也会发生这种情况:
LIBS += "C:/Program Files (x86)/MySQL/MySQL Server 5.6/lib/libmysql.lib"
INCLUDEPATH += "C:/Program Files (x86)/MySQL/MySQL Server 5.6/include/"
SOURCES += "C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.cpp"
HEADERS += "C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h"
这可能是更接近原始版本的版本。
关于这种方法的许可的另一个问题:使用 Qt 源构建此插件是否需要发布使用的源和/或使用模块作为动态库是否存在限制(Qt 开源不允许用于商业目的的静态链接)。