推荐使用作者最新文章《Qt 5.13.2版本静态库编译》中介绍的方法进行编译,更加简单。
一、准备
- 安装VS2013及下载Qt5.5.1源码(也可以使用其他版本的VS来编译)。
- 安装
ActivePerl
,并且将安装目录添加到PATH环境变量。 - 安装
Python
,并且将安装目录添加到PATH环境变量。 - 安装
GPerf
,并且将安装目录/gnuwin32/bin
目录添加到PATH环境变量。
- 安装
Win flex-bison
- 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"。