美文网首页我爱编程
在Windows 7 64位系统上编译QT的MySQL driv

在Windows 7 64位系统上编译QT的MySQL driv

作者: 浪尖儿 | 来源:发表于2016-08-17 17:24 被阅读0次

起因

准备用Qt去连接MySQL数据库,看了一下Qt的文档,很惊讶的发现,原来默认是没有MySQL驱动的!原本以为Qt那么强大,竟然各种驱动要自己编译。不过,总比自己从头写要好多了。
关于这件事件,官方文档是这样描述的:

How to Build the QMYSQL Plugin on Windows

You need to get the MySQL installation files. Run SETUP.EXE and choose "Custom Install". Install the "Libs & Include Files" Module. Build the plugin as follows (here it is assumed that MySQL is installed in C:\MySQL):

 cd %QTDIR%\src\plugins\sqldrivers\mysql
 qmake "INCLUDEPATH+=C:\MySQL\include" "LIBS+=C:\MYSQL\MySQL Server <version>\lib\opt\libmysql.lib" mysql.pro
 nmake
If you are not using a Microsoft compiler, replace nmake with make in the line above.

Note: This database plugin is not supported for Windows CE.

Note: Including "-o Makefile" as an argument to qmake to tell it where to build the makefile can cause the plugin to be built in release mode only. If you are expecting a debug version to be built as well, don't use the "-o Makefile" option.

简简单单的三个命令,原本以为是件很容易的事,没想到折腾了几个小时的时间。在此记录一下其中的问题。

编译过程

编译需要安装MySQL和Qt,都很好找,官方下载就行。

  1. 安装MySQL Server 5.6使用的是官方的installer,装起来很方便,不过后来发现好多网友说路径中不能有空格,因为qmake使用的路径里面不能有空格。顿时觉得进坑了!但是仔细想想没有办法,一是因为windows上的“Program Files”中间有空格;再一个,就算用自己的安装路径,比如d:/mysql,那安装的时候“MySQL Server 5.6”中间还是有空格!这不是自己能控制的吧?好在要编译驱动,只需要用到里面的include和lib文件夹就够了,可以自己把他们放到d:/mysql下面。

  2. 安装qt-opensource-windows-x86-vs2010-4.8.6.exe,从官网上下载就行。用惯了Qt4,暂时还不想升级到Qt5。注意这里只有x86版本的。据网友说,需要64位的话得自己拿源代码编译。我也没较这个劲。安装完成之后需要配置几个环境变量,我配的是QTDIR=D:\Qt\4.8.6,QMAKESPEC=win32-msvc2010,%QTDIR%/bin也要在你的path变量中。

  3. 打开vs2010的命令行,cd %QTDIR%\src\plugins\sqldrivers\mysql,进入D:\Qt\4.8.6\src\plugins\sqldrivers\mysql。

  4. qmake "INCLUDEPATH+=D:/mysql/include" "LIBS+=d:/mysql/lib/libmysql.lib" mysql.pro

  5. nmake
    此时问题出现了,编译过程很顺利,但是连接出错了:

    Creating library debug\qsqlmysqld4.lib and object debug\qsqlmysqld4.exp
    

    qsql_mysql.obj : error LNK2019: unresolved external symbol _mysql_fetch_field@4 referenced in function "public: bool __thiscall QMYSQLResultPrivate::bindInValues(void)" (?bindInValues@QMYSQLResultPriv
    ate@@QAE_NXZ)
    qsql_mysql.obj : error LNK2019: unresolved external symbol _mysql_num_fields@4 referenced in function "public: bool __thiscall QMYSQLResultPrivate::bindInValues(void)" (?bindInValues@QMYSQLResultPriva
    te@@QAE_NXZ)
    qsql_mysql.obj : error LNK2019: unresolved external symbol _mysql_stmt_result_metadata@4 referenced in function "public: bool __thiscall QMYSQLResultPrivate::bindInValues(void)" (?bindInValues@QMYSQLR
    esultPrivate@@QAE_NXZ)
    qsql_mysql.obj : error LNK2019: unresolved external symbol _mysql_stmt_close@4 referenced in function "protected: void __thiscall QMYSQLResult::cleanup(void)" (?cleanup@QMYSQLResult@@IAEXXZ)
    qsql_mysql.obj : error LNK2019: unresolved external symbol _mysql_store_result@4 referenced in function "protected: void __thiscall QMYSQLResult::cleanup(void)" (?cleanup@QMYSQLResult@@IAEXXZ)
    qsql_mysql.obj : error LNK2019: unresolved external symbol _mysql_next_result@4 referenced in function "protected: void __thiscall QMYSQLResult::cleanup(void)" (?cleanup@QMYSQLResult@@IAEXXZ)
    qsql_mysql.obj : error LNK2019: unresolved external symbol _mysql_free_result@4 referenced in function "protected: void __thiscall QMYSQLResult::cleanup(void)" (?cleanup@QMYSQLResult@@IAEXXZ)
    qsql_mysql.obj : error LNK2019: unresolved external symbol _mysql_fetch_row@4 referenced in function "protected: virtual bool __thiscall QMYSQLResult::fetch(int)" (?fetch@QMYSQLResult@@MAE_NH@Z)
    qsql_mysql.obj : error LNK2019: unresolved external symbol _mysql_data_seek@12 referenced in function "protected: virtual bool __thiscall QMYSQLResult::fetch(int)" (?fetch@QMYSQLResult@@MAE_NH@Z)
    qsql_mysql.obj : error LNK2019: unresolved external symbol _mysql_stmt_fetch@4 referenced in function "protected: virtual bool __thiscall QMYSQLResult::fetch(int)" (?fetch@QMYSQLResult@@MAE_NH@Z)
    qsql_mysql.obj : error LNK2019: unresolved external symbol _mysql_stmt_data_seek@12 referenced in function "protected: virtual bool __thiscall QMYSQLResult::fetch(int)" (?fetch@QMYSQLResult@@MAE_NH@Z)
    .......................................................................
    (下面省略了)

这个链接问题很常见啊,就是在lib中找不到对应函数啊,但是怎么会这样呢?检查了几遍路径、命令都没问题啊???

解决问题

在网上google了半天,有很多网友遇到过同样的问题,答案也是各种各样,有的说用有的说加个x64的Project Platform,但是没有64位的Qt啊,一编译就会报错的!
最后终于找到了解决方法,原来MySQL的installer安装的是64位的Connector/C的库,没有32位的库。所以最终的解决办法有2个:

  1. 装32位的MySQL
  2. 用32位的Connector/C去编译
    显然,方法2是要简单一些的。可以去参考3下载一个32位的库,使用它的include和lib,重新编译一下就可以了!

测试

把编译好的qsqlmysqld4.lib,qsqlmysqld4.dll,qsqlmysql4.lib,qsqlmysql4.dll拷贝到D:\Qt\4.8.6\plugins\sqldrivers文件夹下面,里面已经有一些类似的库。然后新建个Qt Application,使用下面的代码就行了。

#include <QtCore/QCoreApplication>
#include <QSqlDatabase>
#include <QDebug>

int main(int argc, char *argv[])
{
    //QApplication a(argc, argv);
    //qtapp w;
    //w.show();
    //return a.exec();

    QCoreApplication a(argc, argv);
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("mysql");
    db.setUserName("root");
    db.setPassword("root");
    if (!db.open())
        qDebug() << "Failed to connect to root mysql admin";
    return a.exec();
}

参考

  1. http://stackoverflow.com/questions/5299087/problem-with-building-mysql-plugin-in-qt-4-7-2
  2. http://www.qtcentre.org/threads/57507-Building-MySql-driver
  3. http://dev.mysql.com/downloads/connector/c/

相关文章

网友评论

    本文标题:在Windows 7 64位系统上编译QT的MySQL driv

    本文链接:https://www.haomeiwen.com/subject/eywmsttx.html