Qt静态库编译指南

推荐使用作者最新文章《Qt 5.13.2版本静态库编译》中介绍的方法进行编译,更加简单。


一、准备

  • 安装VS2013及下载Qt5.5.1源码(也可以使用其他版本的VS来编译)。
  • 安装ActivePerl,并且将安装目录添加到PATH环境变量。
  • 安装Python,并且将安装目录添加到PATH环境变量。
  • 安装GPerf,并且将安装目录/gnuwin32/bin目录添加到PATH环境变量。

下载地址:http://gnuwin32.sourceforge.net/downlinks/gperf.php

  • 安装Win flex-bison

下载地址:https://sourceforge.net/projects/winflexbison/

  • OpenSSL头文件和静态库,可以自己编译,也可以从https://slproweb.com/products/Win32OpenSSL.html下载。
  • 将MD修改为MT。如需修改,则将qtbase\mkspecs\common\msvc-desktop.conf文件中的MDd或MD修改为对应的MTd或MT。

二、编译

2.1 qtvars.cmd

将如下内容保存到qtvars.cmd文件:

REM Set up \Microsoft Visual Studio 2017, where <arch> is \c amd64, \c x86, etc.
CALL "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86
SET _ROOT=D:\Qt\Qt5.5.1
SET PATH=%_ROOT%\5.5\Src\qtbase;%_ROOT%\5.5\Src\qtbase\bin;%_ROOT%\gnuwin32\bin;%PATH%
REM Uncomment the below line when using a git checkout of the source repository
REM SET PATH=%_ROOT%\qtrepotools\bin;%PATH%
SET QMAKESPEC=win32-msvc2013
SET _ROOT=

以管理员权限启动Windows命令行执行qtvars.cmd

2019.9.10日备注:vs2017编译Qt 5.13.1版本,qtvats.cmd文件内容如下,

REM Set up \Microsoft Visual Studio 2017, where <arch> is \c amd64, \c x86, etc.
CALL "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x86
SET _ROOT=C:\Qt\Qt5.13.1
SET PATH=%_ROOT%\5.13.1\Src\qtbase;%_ROOT%\5.13.1\Src\qtbase\bin;%_ROOT%\gnuwin32\bin;%PATH%
REM Uncomment the below line when using a git checkout of the source repository
REM SET PATH=%_ROOT%\qtrepotools\bin;%PATH%
SET _ROOT=

2.1 configure

将命令行切换到Qt源码所在目录,执行下面configure命令:

configure -static -debug-and-release -force-debug-info -nomake examples -nomake tests -skip qtwebengine -qt-pcre -openssl-linked -no-opengl -no-angle -opensource -confirm-license -I D:\Qt\Qt5.5.1\OpenSSL-Win32\include -L D:\Qt\Qt5.5.1\OpenSSL-Win32\lib -prefix D:\Qt\Qt5.5.1\Static

2019.9.10日备注:vs2017编译Qt 5.13.1版本,configure命令如下,

configure -static -debug-and-release -force-debug-info -nomake examples -nomake tests -skip qtwebengine -qt-pcre -openssl-linked -no-opengl -no-angle -opensource -confirm-license -I C:\Qt\Qt5.13.1\OpenSSL-Win32\include -L C:\Qt\Qt5.13.1\OpenSSL-Win32\lib -prefix C:\Qt\Qt5.13.1\msvc2017_x86_static

2.2 make and install

configure完成之后,执行nmake命令开始编译,待编译完之后再执行nmake install命令安装。

如果修改了configure参数,可以执行nmake distclean命令进行清理,然后再次configure、编译。

三、使用Qt静态库

如果结合Visual Studio的Qt Visual Studio Tools插件来开发,可以不用自己添加头文件的引用。
Qt Visual Studio Tools插件的用法参考:Visual Studio + Qt开发环境搭建

下面分别列出了Debug和Release版需要引用的最少的Qt的静态库,如果使用到了Qt的其他功能模块(如网络),则需要引用其他的静态库(如Qt5Network.lib)。

如果使用了OpenSSL,需要额外引用:

Crypt32.lib
OpenSSL-Win32\lib\libeay32.lib
OpenSSL-Win32\lib\ssleay32.lib

3.1 Debug版

Ws2_32.lib
Winmm.lib
Imm32.lib
qtmaind.lib
Qt5Cored.lib
Qt5Guid.lib
Qt5Widgetsd.lib
Qt5PlatformSupportd.lib
qtharfbuzzngd.lib
qtpcred.lib
qtfreetyped.lib
qwindowsd.lib

3.2 Release版

Ws2_32.lib
Winmm.lib
Imm32.lib
qtmain.lib
Qt5Core.lib
Qt5Gui.lib
Qt5Widgets.lib
Qt5PlatformSupport.lib
qtharfbuzzng.lib
qtpcre.lib
qtfreetype.lib
qwindows.lib

四、常见问题

4.1 提示缺少arch.exe

在使用vs2015编译时,可能会提示"Could not find output file ‘arch.exe’ or ‘arch’ in D:/Qt/Qt5.5.1/config.tests/arch : No such file or directory"这样的错误,解决方案:将对应运行时库的rc.exe所在的目录添加到PATH环境变量,如"C:\Program Files (x86)\Windows Kits\8.1\bin\x86"。

本文参考:https://doc.qt.io/qt-5/windows-building.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

china_jeffery

你的鼓励是我前进的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值