当前位置 > 首页 > Sql

Qt 4.5 and MySQL-plugin with Mingw on Windows XP

2011-10-5 13:36:00来源:Sql

?

This is meant to be a quick howto / tutorial on how to setup a build-enviroment for C++ / Qt with Qt Creator ( Trolltech / Nokias new Qt IDE) where you use Mingw to compile your applications and have a MySQL-plugin available.

Getting the required files

You will be needing three files to get started.

First get the latest GA-release of MySQL. Go to their download-section and find the “Windows MSI Installer” for your architecture (x86 or x64). Save the file to your desktop.

Then go to Trolltech / Nokias download-section to get the LGPL’ed Qt SDK. Save the file to your desktop.

Finally go to the Mingw download-section to get the Mingw Utilities. I downloaded version 0.3 (latest version as of this writing). Save the file mingw-utils-0.3.tar.gz to your desktop.

In addition to the three files mentioned above you might need WinRAR or a similar tool to unpack tar.gz-files.

Installing MySQL

I always start by installing MySQL. This is not a requirement and the order in which you install MySQL and Qt does not matter.

Start the MySQL-installer you have on your desktop. Choose the advanced install options. I usually remove the MySQL server instance from the install as I have several external database-servers I do tests against. No need to clutter the dev-box if you have it available other places.

What’s important here is that you choose to install “Development Components” and all sub-features. These are the ones that’ll enable Qt to know how to reach a given MySQL database.

Another important step here is to install directly to c:\mysql, not c:\Program Files\Mysql\MySQL Server\whatnot. Spaces in pathnames confuse certain of the items in the toolchain. If you do install somewhere with spaces in the path then you have to remember to always use the old style-pathnames: c:\Progra~1\etc .

Proceed with the install, and after completion verify that you have libmysql.dll under c:\mysql\bin, and that there is a directory c:\mysql\include with header-files (.h) in it.

Installing Qt SDK

Start the Qt SDK installer and choose to install it in c:\Qt\2009.01 or whatever path it propose, as long as it is a path without spaces in it. Choose to install all the components, including Mingw.

Installing Mingw Utilities

The Mingw Utilities is simply a collection of executables, plus a couple of dlls. Open the file you downloaded earlier with a suitable program like WinRAR and copy the _contents_ of the bin-directory into C:\Qt\2009.01\mingw\bin.

Fixing (?) QMake

It might just be me having this problem, but I had to rebuild QMake even before configure would run and not crash spewing something about not finding stuff in ‘c:\Progra~1\Trolltech..’. Anyway, it’s not exactly rocketscience. Locate the ‘Qt Command Prompt’ and start that. It will upon execution add a couple of locations to PATH so that you’ll have the tools you need easily available. From there you do the following.
 C:\Qt\2009.01> cd qt\qmake
C:\Qt\2009.01\qt\qmake> mingw32-make clean
C:\Qt\2009.01\qt\qmake> mingw32-make

After that QMake should start looking for mkspecs and other candy in the appropriate locations.

Preparing Qt for building

The next thing you want to do is run configure with a handful of arguments. For the sake of simplicity I’ll just include the arguments you need to have Qt build with the MySQL-plugin.
Again, in the Qt Command Prompt:
 C:\Qt\2009.01\qt> configure.exe -debug-and-release -plugin-sql-mysql
The above will create Makefiles and things for all the sub-projects in the Qt directory-tree. This might take a little while so you might want to have some knitting-needles or a book at hand.

See ‘configure.exe -help’ for more options.

Configuring the MySQL-plugin

The Qt Reference Documentation is one of the best pieces of documentation I’ve ever seen. It’s full of working examples of how to use the API Trolltech / Nokia provide and generally have a very high level of quality.

However when it comes to how to build Qt with certain plugins it can be a bit lacking. The documentation here states that you do the following to build the MySQL-plugin on Windows:
 cd %QTDIR%\src\plugins\sqldrivers\mysql
qmake "INCLUDEPATH+=C:\MySQL\include" "LIBS+=C:\MYSQL\MySQL Server\lib\opt\libmysql.lib" mysql.pro
nmake

That’s great, _if you use nmake_. Alot of us rely on Mingw to build our applications on Windows, and for us the above simply will not work. That is due to differences in object and lib-format between MSVC (which the mysql-dll’s are built with) and the GNU-toolchain. To solve this you have to do some magic on the dll and produce something that the GNU-toolchain can work with.

Proceed by doing this in the “Qt Command Prompt”:
 C:\Qt\2009.01\qt> cd src\plugins\sqldrivers\mysql
C:\Qt\2009.01\qt\src\plugins\sqldrivers\mysql> set MYSQL_PATH=c:mysql
C:\Qt\2009.01\qt\src\plugins\sqldrivers\mysql> reimp -d %MYSQL_PATH%\lib\opt\libmysql.lib
C:\Qt\2009.01\qt\src\plugins\sqldrivers\mysql> dlltool -k --input-def LIBMYSQL.def --dllname libmysql.dll --output-lib libmysql.a
C:\Qt\2009.01\qt\src\plugins\sqldrivers\mysql> qmake "INCLUDEPATH+=%MYSQL_PATH%include" "LIBS+=-L. -lmysql" mysql.pro

Building Qt

When all of the above has been done all you have to do is start the Qt build process. Again in the “Qt Command Prompt”:
 C:\Qt\2009.01\qt> mingw32-make sub-src


  • 施密特:手机和电视广告将是Google收入增长点

    2009-7-2 17:09:00

    北京时间7月2日消息:据国外媒体报道, Google 首席执行官施密特周三对媒体表示,搜索和广告仍然是 Google 的核心业务,但是 Google 未来将把网络广告的技术优势移植到电视和手机上,这

  • SQL Server 2008 R2帮助文档里关于CREATE TABLE

    2011-7-14 21:00:00

    先给出完整的CREATE TABLE 语法 CREATE TABLE [ database_name . [ schema_name ] . | schema_name . ] table

  • Google App Engine SDK 1.2.6发布 可删除应用

    2009-10-14 10:41:00

    51CTO 10月14日快讯】 Google 的App Engine小组今日发布了App Engine SDK 1.2.6(包括Python和Java)。此次更新包含了大量增强以及bug修复,外加若

  • Networking:Google承认盗用为何不向搜狐道歉

    2007-4-9 0:38:00

    Networking:Google承认盗用为何不向搜狐道歉...Networking)搜狐的声明之后,Google终于低下了自以为高贵的头...输入法只不过是一款工具,睿智的Google却一眼看穿了

  • 梅铎旗下新闻网 拟阻Google搜寻

    2009-11-10 17:02:00

    传媒大亨梅铎表示,为鼓励网民付款阅读网上文章,计划在 Google 搜寻索引中删除他的新闻集团旗下刊物的新闻报道文章。 《卫报》昨日报道,梅铎在接受澳洲天空电视台访问时表示,新闻集团旗下的英国《太

  • mysql IGNORE 屏敝错误

    2011-7-5 17:26:00

    前几天做一个邮件验证系统时遇到一个问题,一个用户表,用户名是邮箱地址,并且是唯一索引。如果用户在更改资料是,改成其他用户的邮箱,就mysql运行 sql 语句是会报错(确保用户邮箱的唯一性)。

  • Sql : 三种事务处理方式" href="http://blog.knowsky.com/253638.htm" target="_blank">Linq to Sql : 三种事务处理方式

    2011-8-3 5:21:00

    Linq to SQL 支持三种事务处理模型:显式本地事务、显式可分发事务、隐式事务。下面就结合实例来对此进行阐述。 测试环境 OS Windows Server 2008 Ente

  • Google Chrome Stable更新到4.0.249.78

    2010-1-26 11:23:00

    新闻来源: http://www.gogofly.com/ Google 发布Chrome Stable分支升级,最新版本:4.0.249.78(36714),因为无法访问 Google Ch

  • 新流传Chrome OS截图来自Google员工?

    2009-11-20 5:55:00

    软件频道11月20日 Google Chrome OS在美国总部的展示会时间已经敲定,届时还将在 Google 官方网上直播 ,现在大家不必再去费力猜测Chrome OS究竟是个什么样了,只需

  • C#中关于SQL字段null值的判断

    2010-11-24 18:41:00

    1.确定返回的DataTable的字段是字符串类型的!string.IsNullOrEmpty(dataset.table[i].Rows[i].is_nullvalue) (其中i可以替换成确定的