Qt建议安装动态链接Dbug版和编译安装静态链接release版
前提:
先安装动态链接Dbug版,方法:QT5.10开发(1)安装QT5.10
地址:http://blog.csdn.net/qq_15304853/article/details/79165301
然后编译静态链接release版,方法:
1 准备工作
1.1 编译环境
Windows 10 (Multiple Editions), Version 1703 (Updated July 2017) (x64)
1.2 建立文件夹结构
假设之前已安装Qt存放在D:pg-Qt目录下,该目录此时内容应如下:

进入5.10.0目录,新建目录:mingw53_32_static 和 qt_build_mingw53_32_static,结果如下

1.3 准备源码
如果之前安装时选择连源码一起安装,则此时源码已经在:D:\pg-Qt\5.10.0\Src目录下了,参见上图。
如果为选择连源码一起安装,则可从Qt官网下载:http://download.qt.io/official_releases/qt/5.10/5.10.0/single/ 下载的qt源码包“qt-everywhere-src-5.10.0.tar.xz”,解压后,放在硬盘的根目录。例如:“F:\qt-everywhere-src-5.10.0 ”
注:如果qt源码目录的路径比较长会导致编译qtimageformats时出现 无法打开编译器生成的文件“”:Invalid argument的错误,所以放在根目录下,尽量减少路径长度。
2 安装编译所需的工具(直接在360软件宝库搜索安装)
1、python 2 ,官网“https://www.python.org/downloads/” 本次测试编译版本为python-2.7.13。
不安装在配置qt时会提示:WARNING: Python version 2 (2.7.5 or later) is required to build QtWebEngine.
2、Ruby ,官网“http://www.ruby-lang.org/en/downloads/” 本次测试编译版本为Ruby-1.9.2。
3 ActivePerl,官网“https://www.activestate.com/activeperl/downloads”本次测试编译版本ActivePerl-5.24.1.2402
如果不编译qtwebkit,就不用安装ActivePerl了。
注意:安装时必须选择“建立环境变量”选项。,也可以手动建立,我的如下:

3 配置Qt
3.1准备openssl
下载别人专门为Qt编译好的openssl,
地址 http://www.cnblogs.com/findumars/p/6372223.html
,下载后解压OpenSSL的zip压缩包(openssl-1.0.1c_static_w32_mingw.zip)后放置:D:pg-Qt目录下,此时路径为:D:pg-Qt\openssl-1.0.1c_static_w32_mingw
也可以自己编译,参考:
静态编译OpenSSL并整合到Qt
http://blog.csdn.net/lixiaoxin1989/article/details/78421573
Windows 下编译 OpenSSL
http://blog.csdn.net/liang19890820/article/details/51658574
openSSL1.1.1的编译
http://blog.csdn.net/lostspeed/article/details/69492531
3.2
打开终端,开始==》程序==》Qt5.10.0==》Qt 5.10.0 for Desktop (MinGW 5.3.0 32 bit)进入命令窗口
3.3 检查命令执行情况
以管理员用户运行windows 命令行。
1)运行命令“gcc –v” 确保编译器能够正常运行;
2)运行命令“perl –v”确保perl能够正常运行;
3)运行命令“ruby –v”确保ruby能够正常运行;
4)运行命令“python”确保python能够正常运行,按“Ctrl+Z”结束运行;
5)运行命令“sh”确保该命令不能执行,其会影响ming32-make的执行。
3.4使用shadow-build配置Qt
输入命令:
D :
cd D:\pg-Qt\Qt5.10.0\qt_build_mingw53_32_static
在该目录下运行下面命令:
F:\qt-everywhere-src-5.10.0\configure.bat -static -release -confirm-license -opensource -opengl desktop -platform win32-g++ -prefix "D:\pg-Qt\5.10.0\mingw53_32_static" -sql-sqlite -sql-odbc -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -make libs -nomake tools -nomake examples -nomake tests
-openssl-linked OPENSSL_LIBS="-llibssl -llibcrypto -lgdi32" -L D:\pg-Qt\5.10\openssl-1.0.1c_static_w32_mingw -I D:\pg-Qt\5.10\openssl-1.0.1c_static_w32_mingw\include
-skip qt3d -skip qtcanvas3d -skip qtdatavis3d -skip qtlocation -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtwebview -skip qtwinextra
1)如果不需要包含OpenSSL,去掉-openssl-linked OPENSSL_LIBS="-llibssl -llibcrypto -lgdi32" -L D:\pg-Qt\5.10\openssl-1.0.1c_static_w32_mingw -I D:\pg-Qt\5.10\openssl-1.0.1c_static_w32_mingw\include
2)我跳过了一下包,如果你需要这些,去掉-skip qt3d -skip qtcanvas3d -skip qtdatavis3d -skip qtlocation -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtwebview -skip qtwinextra
参数说明:
-confirm-license -opensource :使用开源许可
-opengl desktop :桌面版
-platform win32-g++ :生成的编译器类型为:windows C++编译器
-release :生成的编译器可编译产品: 发布版
-static :生成的编译器为 静态链接编译器
-static-runtime:生成的编译器使用静态运行时库
-nomake tools:不编译tools
-prefix "5.10.0-desktop-mingw53-rel-static" :生成的编译器安装位置
注:
如果出错,将D:\pg-Qt\5.10.0\qt_build_mingw53_32_static下文件全部删除,重新configure
1)通过shadow-build方法配置可实现源码和编译产生的文件如Makefiles, object files和其他中间文件的彻底分离。编译文件存放在:D:\pg-Qt\5.10.0\qt_build_mingw53_32_static
2) 也可以直接切换到F:\qt-everywhere-src-5.10.0\目录,
3) 然后执行:configure.bat -static -release -confirm-license -opensource -opengl desktop -platform win32-g++ -prefix "D:\pg-Qt\5.10.0\mingw53_32_static " -sql-sqlite -sql-odbc -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -make libs -nomake tools -nomake examples -nomake tests
-openssl-linked OPENSSL_LIBS="-llibssl -llibcrypto -lgdi32" -L D:\pg-Qt\5.10\openssl-1.0.1c_static_w32_mingw -I D:\pg-Qt\5.10\openssl-1.0.1c_static_w32_mingw\include
-skip qt3d -skip qtcanvas3d -skip qtdatavis3d -skip qtlocation -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtwebview -skip qtwinextra
4) 但产生编译文件在F:\qt-everywhere-src-5.10.0\中
配置没有错误后的输出结果类似下图,不过最好检查自己需要的模块是否为yes:

4 编译Qt库
运行下面命令编译Qt库
mingw32-make
过程较长,我安装时没有跳过东西,用了8小时!
注:若前面编译过Qt库,需执行下面命令,mingw32-make clean清除(该方法为官方文档推荐方法,本人推荐直接删除“D:\pg-Qt\\5.10.0\qt_build_mingw53_32_static”文件夹下的全部内容重新配置,这样更彻底、快速)。
5 安装Qt
mingw32-make install
6 配置Qt库
1. 把编译的静态Qt库放到 动态的Qt库所在目录,直接复制动态Qt库\bin目录下的qt.conf文件,或在静态库\bin目录下建立qt.conf文件,里面输入以下内容:
[Paths]
Prefix=..
2. 打开D:\pg-Qt\5.10.0\mingw53_32_static\mkspecs\win32-g++\qmake.conf,
找到 QMAKE_LFLAGS_DLL
将后面都改成 -static
再加一行 QMAKE_LFLAGS
后面也改成 -static
结果:
QMAKE_LFLAGS = -static
QMAKE_LFLAGS_EXCEPTIONS_ON = -mthreads
QMAKE_LFLAGS_EXCEPTIONS_OFF =
QMAKE_LFLAGS_RELEASE = -Wl,-s
QMAKE_LFLAGS_DEBUG =
QMAKE_LFLAGS_CONSOLE = -Wl,-subsystem,console
QMAKE_LFLAGS_WINDOWS = -Wl,-subsystem,windows
QMAKE_LFLAGS_DLL = -static
3. 复制动态库中的bin/qmlscene.exe到静态库的bin文件夹中。否则在Qt Creator中添加Qt库时会报qmlscene未安装的错误。
4. 从动态链接版的Qt库的bin文件夹中复制Qt5Core.dll到静态链接版的lib文件夹中。
注:① 如果不执行此步,在Creator的Qt Version中将会显示“x86-windows-unknown-pe-32bit”。
5. 拷贝qml文件
因为一些我还没了解的原因,编译出来的Qt,在D:\pg-Qt5.10.0\mingw53_32-static\qml\QtQuick\Controls目录下是没有.qml文件,如下图,左边为普通的版本,右边为静态编译版本Qt。右边qml文件一个都没有。所以,这些qml文件都需要手动拷贝下。
7 集成到Qt Creator 中
工具--选项--构建和运行
在Qt Versions选项卡中添加D:\pg-Qt\5.10.0\mingw53_32_static\bin\qmake.exe静态配置后,点击“Apply”按钮。
在Kit选项卡中按下图配置。
8使用该 “windows静态链接库 ”套件编译出项目的release版静态链接软件
创建或打开项目-进入项目-点击“项目”-配置使用该库


点击-运行。

参考资料
(1) http://doc.qt.io/qt-5/windows-support.html
(2) http://doc.qt.io/qt-5/windows-requirements.html
(3) http://doc.qt.io/qt-5/windows-building.html
(4) http://doc.qt.io/qt-5/configure-options.html
(5) http://blog.csdn.net/lixiaoxin1989/article/details/53838277
(6) http://blog.csdn.net/wsj18808050/article/details/72862478
(7) Qt for Windows:Qt 5.9.0 MinGW 静态编译版本(包含OpenSSL)
http://blog.csdn.net/wsj18808050/article/details/72862478