我的版本是qt-everywhere-opensource-src-4.8.0.zip,其实一开始我用的是4.7.4,但是碰到了一些
问题,干脆换4.8.0结果也碰到了一样的问题,下面会总结一下,我不说太多的废话,论坛已经有帖子,而且官方文档也不难看懂,那些英文比较浅显。
网上有很多是在已经
安装好了qt-win-opensource-4.x.x-mingw.exe的情况下
编译数据库驱动的,我一开始也是这样做的,也顺利做好了。我的毕业论文就是托Qt的福做好的。
但是如果是直接编译源码的话,会有很多问题,例如这一步Now the following libraries are ready in
C:\Qt\4.x.x\plugins\sqldrivers. 这个
文件夹下并
没有生成我想要的驱动文件,而且连文件夹都都没有生成。还会发生Perl not found in environment - cannot run syncqt
错误。
接下来,我会直接在官方文档的相应英文文字处做解释。
主要参考资料如下: 1.Qt assistant参考文档: SQL Database Drivers 2. 论坛的某大神的帖子Qt4 OpenSource for mingw中编译MySQL驱动 http://www.qtcn.org/bbs/read-htm-tid-4210.html 3.QT 4.8 静态库编译方法(问度娘) 4.谷歌老外 按如下的步骤一步步来。
The following steps have been used successfully for WinXP SP3. In this example, Qt 4.6.2 is shown.
- Download the following components:MinGW-5.1.6.exe
- mingw-utils-0.3.tar.gz
- Qt sources, e.g. qt-everywhere-opensource-src-4.6.2.zip
- mysql-5.1.35-win32.msi
Install
MinGW-5.1.6.exe in, e.g.
C:\MinGW.Extract
mingw-utils-0.3.tar.gz into, e.g.
C:\MinGW.Add the path for
MinGW-5.1.6.exe to your
PATH variable, e.g.
C:\MinGW\bin; Extract the Qt sources, (
qt-everywhere-opensource-src-4.6.2.zip), into, e.g.
C:\Qt.Add the path for the eventual Qt binary to your
PATH variable, e.g.
C:\Qt\4.6.2\bin;.
这一步,解压缩
qt-everywhere-opensource我是把qt-everywhere-opensource这一级也跳过去,把下一级的所有文件解压缩在C:\Qt\4.x.x\下 - Install MySQL (mysql-5.1.35-win32.msi), customizing the components. Select only the headers and libraries. Install in, e.g.C:\MySQL\MySQL51.
- Open the DOS prompt, go to C:\MySQL\MySQL51\lib\opt, and run the following commands:reimp -d libmysql.lib
- dlltool -k -d libmysql.def -l libmysql.a
这一步最好是输入cmd,打开DOS prompt,我做这一步的时候,已经装好了qt-win-opensource-4.8.0-mingw.exe.所以我事先把环境变量全部修改了,换成qt-everywhere-opensource的路径。例如D:\Qt\4.8.0\bin;D:\Qt\4.8.0\qmake;都修改成C:\Qt\4.8.0\bin;C:\Qt\4.8.0\qmake;- Open the DOS prompt, go to C:\Qt\4.6.2 and run the following commands:configure.exe -debug-and-release -platform win32-g++ -qt-sql-mysql -l mysql -I C:\MySQL\MySQL51\include -L C:\MySQL\MySQL51\lib\opt
- mingw32-make sub-src
This step takes a long time.
这一步,我的是这样的,要以自己的实际情况为准:
configure.exe -debug-and-release -platform win32-g++ -qt-sql-mysql -l mysql -I D:\MySQL\include -L D:\MySQLlib\opt 然后会出现Perl not found in environment - cannot run syncqt 错误,那么就要这样, 删除源码包中bin目录下的syncqt和syncqt.dat文件,也可将其改名或剪切到别的位置,我就改成syncqt1和syncqt1.dat,最好谷歌老外的做法,这些都是抄的老外的。 Open the DOS prompt, go to
C:\Qt\4.6.2\src\plugins\sqldrivers\mysql and run the following command:
- qmake "INCLUDEPATH+=C:\MySQL\MySQL51\include" "LIBS+=-L. mysql" mysql.pro
这一步的话,我把LIBS+=-L. mysql" mysql.pro改成LIBS+=D:\MySQLlib\opt\libmysql.a" mysql.pro qmake "INCLUDEPATH+=C:\MySQL\MySQL51\include" "LIBS+=D:\MySQL\lib\opt\libmysql.a" mysql.pro 接下来不用再输入make了,已经自动做好了,否者,会提示Nothing to be done。如果是在装好了qt-win-opensource-4.x.x-mingw.exe的文件夹下编译驱动,肯定要自己make一下的,嘿嘿。 结果,接下来最后一步就顺利的做好了- Now the following libraries are ready in C:\Qt\4.6.2\plugins\sqldrivers.libqsqlmysql4.a
- libqsqlmysqld4.a
- qsqlmysql4.dll
- qsqlmysqld4.dll
To use the SDK and QtCreator directly, copy these libraries to your
C:\Qt\...\qt\plugins\sqldrivers\, and copy
C:\MySQL\MySQL51\lib\opt\libmysql.dll to your
C:\Qt\...\qt\bin\.
还要注意要把
C:\MySQL\MySQL51\lib这个目录添加进环境变量PATH,我的是C:\MySQL\lib,否者当你的程序运行时还会发生驱动没有加载的错误。发生了这个错误,那就添加环境变量,接下来就好乐。 我目前也只是知其然而不知其所以然,还要继续学习,大家共勉。套用一句话,这些玩意不会比英文更难学。 附图:我的记录,发生错误的图我当时没截下来,呵呵。 当qmake之后,不用再make了,这一步已经生成了C:\Qt\4.8.0\plugins ,plugins这个文件夹,然后里面有4个我们想要的东西,然后继续。。。