Win10下编译最小的QT5静态库

前言

  1. Qt Modules:可以直接进入QT源码目录,排除coin,gnuwin32,qtbase后其余目录都这样 -skip 目录名 跳过

  2. Qt Features:可以通过 configure --list-features 来查看功能列表后使用 -no-feature-<feature> 来关闭相关功能

  3. Qt Parts:想用Qt自带的第三方库可用通过 -qt-库名 配置。想用系统中的第三方库可用通过 -system-库名 配置。不用对应库时使用 -no-库名 去除。例如: -qt-libpng -system-xcb -no-zlib

  4. 这几个选项添加后编译会报错:-no-feature-accessibility -no-feature-itemmodel -no-pcre -no-doubleconversion

编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 设置MinGW的开发环境
set PATH=D:\QT5\Tools\mingw810_64\bin;%PATH%
set QT_SRC=D:\QT5\qt-everywhere-src-5.15.3
cd %QT_SRC% & mkdir build & cd build

# 配置QT编译内容
..\configure.bat -static -release -platform win32-g++ -no-opengl -prefix "D:\QT5\Qt5.15.3_MinGW8.1_x64_Static" ^
-opensource -confirm-license -make libs -nomake tools -nomake examples -nomake tests ^
-skip qt3d -skip qtactiveqt -skip qtandroidextras -skip qtcharts -skip qtconnectivity -skip qtdatavis3d ^
-skip qtdeclarative -skip qtdoc -skip qtgamepad -skip qtgraphicaleffects -skip qtimageformats -skip qtlocation ^
-skip qtlottie -skip qtmacextras -skip qtmultimedia -skip qtnetworkauth -skip qtpurchasing -skip qtquick3d ^
-skip qtquickcontrols -skip qtquickcontrols2 -skip qtquicktimeline -skip qtremoteobjects -skip qtscript ^
-skip qtscxml -skip qtsensors -skip qtserialbus -skip qtserialport -skip qtspeech -skip qtsvg -skip qttools ^
-skip qttranslations -skip qtvirtualkeyboard -skip qtwayland -skip qtwebchannel -skip qtwebengine ^
-skip qtwebglplugin -skip qtwebsockets -skip qtwebview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns ^
-no-feature-texthtmlparser -no-feature-textodfwriter -no-feature-concurrent -no-feature-effects ^
-no-feature-sharedmemory -no-feature-systemsemaphore -no-feature-im -no-feature-process ^
-no-feature-dom -no-feature-filesystemmodel -no-feature-filesystemwatcher -no-feature-graphicsview ^
-no-feature-graphicseffect -no-feature-sizegrip -no-feature-calendarwidget -no-feature-printpreviewwidget ^
-no-feature-keysequenceedit -no-feature-colordialog -no-feature-filedialog -no-feature-fontdialog ^
-no-feature-printpreviewdialog -no-feature-progressdialog -no-feature-errormessage -no-feature-wizard ^
-no-feature-datawidgetmapper -no-feature-imageformat_bmp -no-feature-imageformat_ppm -no-feature-imageformat_xbm ^
-no-feature-imageformat_png -no-feature-imageformat_jpeg -no-feature-image_heuristic_mask -no-feature-image_text ^
-no-feature-colornames -no-feature-cups -no-feature-freetype -no-feature-translation -no-feature-codecs ^
-no-feature-big_codecs -no-feature-iconv -no-feature-ftp -no-feature-udpsocket -no-feature-networkproxy ^
-no-feature-socks5 -no-feature-networkdiskcache -no-feature-bearermanagement -no-feature-completer ^
-no-feature-fscompleter -no-feature-desktopservices -no-feature-mimetype -no-feature-systemtrayicon ^
-no-feature-undocommand -no-feature-undostack -no-feature-undogroup -no-feature-undoview ^
-no-feature-statemachine -no-feature-gestures -no-feature-dbus -no-feature-cssparser -no-feature-sqlmodel ^
-no-feature-sessionmanager -no-feature-textmarkdownreader -no-feature-textmarkdownwriter -no-feature-itemmodeltester ^
-no-libmd4c -no-zlib -no-ico -no-gif -no-libjpeg -no-libpng -no-xcb -no-xkbcommon -no-harfbuzz

# 使用MinGW64来编译
mingw32-make -j12 # I9 9700K需要10分钟,build目录下共占用了834MB大小
mingw32-make install # 需要2分钟,安装目录下共占用了103MB大小

测试

  1. 在目录Test中新建如下内容文件 HelloWorld.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <QApplication>
#include <QLabel>

int main(int argc,char **argv)
{
QApplication app(argc,argv);
QLabel label("Hello World!");
label.setAlignment(Qt::AlignCenter);
label.resize(400, 300);
label.show();
return app.exec();
}
  1. 使用命令提示符进入目录Test中
1
2
3
4
5
6
7
8
9
10
11
set PATH=D:\QT5\Tools\mingw810_64\bin;D:\QT5\Tools\Qt5.15.3_MinGW8.1_x64_Static\bin;%PATH%

# 命令行编译工程源文件
qmake -project QT+=widgets # 生成.pro工程文件
qmake # 生成Makefile文件
mingw32-make # 调用MinGW编译器来编译程序

# qmake -project 后面没有接额外的参数将递归遍历当前目录,寻找例如源,头文件和UI文件包含在.pro文件中

# 命令行编译ui界面文件
uic –o xxx.h xxx.ui # 用.ui文件生成.h文件
  1. 最后执行 Test\release\HelloWorld.exe 可打开生成的QT程序

  2. 如果软件使用MinGW静态编译后如果还是需要 libgcc_s_xxx.dll 等库,将如下文件修改或添加两行参数即可解决:

1
2
3
4
5
6
7
8
# QT源码编译前:将这个文件添加如下两行:
D:\QT5\qt-everywhere-src-5.15.3\qtbase\mkspecs\win32-g++\qmake.conf

# 该程序编译前:将这个文件添加如下两行:
D:\Qt\Qt5.12.5_MinGW_Static\mkspecs\win32-g++\qmake.conf

QMAKE_LFLAGS = -static
QMAKE_LFLAGS_DLL = -static

其它

MinGW里没有 ldd 工具,因为Windows不使用 .so 共享库文件。如果要查看Windows里可执行文件的依赖库,需要使用微软自家的 Dependency Walker 工具。MinGW可以通过 dlltool 来生成用于创建和使用动态链接库需要的文件,如 .def 和 .lib。

Linux/Unix 系统里静态库扩展名一般是 .a,动态库扩展名一般是 .so 。Windows 系统里用的静态库扩展名一般是 .lib,动态库扩展名一般是 .dll 。

MinGW比较特殊,是将GNU工具集和链接库从Linux/Unix系统移植到Windows里,所以使用的静态库扩展名为 .a ,而其动态库扩展名则为 .dll.a 仅在生成目标程序过程中使用,.dll 则是在目标程序运行时使用。