Qt静态库制作
1、安装
1、Python 2、ActivePerl 3、Qt(包含源码和插件) 4、配置环境变量
Qt5.8.0配置gcc环境变量:
a.变量名:MINGW_PATH
变量值:E:\Qt\Tools\mingw530_32
b.变量名:C_INCLUDE_PATH
变量值:%MINGW_PATH%\include;%MINGW_PATH%\lib\gcc\i686-w64-mingw32\5.3.0
c.变量名:CPLUS_INCLUDE_PATH
变量值:%MINGW_PATH%\include;%MINGW_PATH%\lib\gcc\i686-w64-mingw32\5.3.0\include
d.变量名:LIBRARY_PATH
变量值:%MINGW_PATH%\lib;%MINGW_PATH%\lib\gcc\i686-w64-mingw32\5.3.0
e.变量名:Patch
变量值:%MINGW_PATH%\libexec\gcc\i686-w64-mingw32\5.3.0;%MINGW_PATH%\bin
2、编译/安装
打开Qt5.8 for Desktop (MinGW 5.3.0 32 bit)
2.1、检查
输入以下命令看看 gcc 编译器是否配置正确
gcc-v
再输入以下命令看看 perl 解释器是否配置正确
perl -v
输入以下命令看看 python 是否配置正确
python
按Ctrl + Z 回车,可以退出 python
在mingw53_32同目录下,新建一个文件夹mingw53_32_static
2.2、配置Qt
切换到Qt的源码目录(-prefix后跟的是编译完成后的安装目录,替换成自己的目录即可)
Qt 5.7.1:
configure -confirm-license -opensource -debug-and-release -static -force-debug-info -prefix "D:\msvc2015_static" -qt-sql-sqlite -qt-sql-odbc -plugin-sql-sqlite -plugin-sql-odbc -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -qt-freetype -no-qml-debug -no-angle -nomake tests -nomake examples
Qt 5.8.0:
configure -confirm-license -opensource -debug-and-release -static -force-debug-info -prefix "E:\msvc2015_static" -qt-sqlite -qt-pcre -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -qt-freetype -nomake tests -nomake examples
Qt5.9.0(msvc版)
configure -confirm-license -opensource -platform win32-msvc -debug-and-release -static -static-runtime -force-debug-info -prefix "E:\msvc2015_static" -qt-sqlite -qt-pcre -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -qt-freetype -nomake tests -no-compile-examples -nomake examples
Qt 5.9.0(mingw版)
configure -confirm-license -opensource -platform win32-g++ -debug-and-release -static -static-runtime -force-debug-info -prefix "E:\Qt\5.9.1\mingw53_32_static" -qt-sqlite -qt-pcre -qt-zlib -qt-libpng -qt-libjpeg -opengl desktop -qt-freetype -nomake tests -no-compile-examples -nomake examples
注:【Windows PowerShell默认不会从当前位置加载命令。如果信任此命令,请在前面加入".\"。】
2.3、编译
(msvc版)
执行nmake命令
(mingw版)
执行mingw32-make命令
2.4、安装
(msvc版)
执行nmakeinstall命令
(mingw版)
执行mingw32-makeinstall命令
3、配置
3.1、配置Qt库
1. qt.conf文件指明qt库目录的前缀、文档目录和示例目录,如果将安装目录挪到别处则需要修改或新建。在bin文件夹下新建qt.conf文件,并输入以下内容:
1. [Paths]
2. Documentation=C:/Qt/Qt5.7.1/Docs/Qt-5.7
3. Examples=C:/Qt/Qt5.7.1/Examples/Qt-5.7
4. Prefix=D:/msvc2015_static [Paths]Documentation=C:/Qt/Qt5.7.1/Docs/Qt-5.7
Examples=C:/Qt/Qt5.7.1/Examples/Qt-5.7
Prefix=D:/msvc2015_static
注:① 文档和示例目录此处使用了官方的msvc2015动态编译版安装包中的目录,也可以删掉这两项。Prefix是存放Qt库的目录。
② 建议把静态的Qt库和动态的Qt库放在同一目录,这样可以直接复制动态Qt库目录下的qt.conf文件,或输入以下内容:
1. Documentation=../../Docs/Qt-5.7
2. Examples=../../Examples/Qt-5.7
3. Prefix=.. [Paths]
Documentation=../../Docs/Qt-5.7
Examples=../../Examples/Qt-5.7
Prefix=..
2. 复制动态库中的bin/qmlscene.exe到静态库的bin文件夹中。否则在Qt Creator中添加Qt库时会报qmlscene未安装的错误。
3. 此步是Qt 5.7.1的msvc版需要执行的步骤:从源码目录中复制pdb文件到安装目录的lib文件夹。
将下面文本复制到批处理文件中,然后放在lib文件夹中执行:
for /r C:\qt_5.7.1_src %%a in (*.pdb) do if /i "%%~xa"==".pdb" copy "%%~a"
pause
for /r C:\qt_5.7.1_src %%a in (*.pdb) do if /i "%%~xa"==".pdb" copy "%%~a"
pause
有些pdb文件不是放在lib文件夹的,此时需要按照pdb的名字进行搜索,将同名的pdb和lib放在一起。
最后删除批处理文件。
注:① 如果不执行此步,编译时会有“未找到PDB文件”的警告,且调试时无法跟踪到Qt的源码。
4. 从动态链接版的Qt库的bin文件夹中复制Qt5Core.dll到静态链接版的lib文件夹中。
注:① 如果不执行此步,在Creator的QtVersion中将会显示“x86-windows-unknown-pe-32bit”。
3.2、添加一个Qt版本
打开Qt Creator -> 工具 -> 选项 -> 构件和运行 -> Qt Versions
按照下图添加Qt版本,然后点Apply
3.3 设置套件
构建套件:克隆一个,修改Qt版本为刚添加的静态版,其他选项与动态版相同即可。
3.4 配置编译器
(msvc版)
修改源码中\qtbase\mkspecs\common\msvc-desktop.conf文件,
QMAKE_CFLAGS_RELEASE = -O2 -MD
QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO +=-O2 -MD -Zi
QMAKE_CFLAGS_DEBUG = -Zi -MDd
改为:
QMAKE_CFLAGS_RELEASE = -O2 -MT
QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO +=-O2 -MT -Zi
QMAKE_CFLAGS_DEBUG = -Zi -MTd
(mingw版)
用记事本打开 D:\Qt\Qt5.8.0\5.8\mingw53_32_static\mkspecs\win32-g++\qmake.conf
找到QMAKE_LFLAGS 和 QMAKE_LFLAGS_DLL
将后面都改成 -static
4. 新建工程选择 静态编译套件,编译出来就可以直接运行exe程序了。编译过程很慢,发布的时候最好选用release的,release的程序体积比debug小很多。