Qt-windeployqt部署发布浅谈

本文最后更新于:2022年10月12日 晚上

前情提要

众所周知,我们可以在Qt Creator中运行项目,或者是在release/debug文件夹中双击exe运行应用程序

有些同学可能对后者存在些许疑惑,时常会看到找不到dll等错误

这是由于未在环境变量中加入msvc/bin | mingw/bin等目录(环境变量可以看做是搜索目录)

bin目录中存放的正是项目中依赖的各种Qt-dll

exe的运行依赖各种dll,这是常识

但是具体依赖哪些dll,以及哪几类dll

从来源上,可以分为两种:Windows dll & Qt dll

  • Windows dll通常位于C:\Windows\System32文件夹下(以x64为例),一般系统自带,无需特殊处理
  • Qt dll一般位于msvc/bin | mingw/bin(包含版本号),一般用户系统无Qt,大概率缺失,需要程序自带

所以,为了将应用程序打包发布到一般PC上,我们需要搜集依赖的Qt-dll并与exe置于同级目录下,方便寻址

But How

正片(叠底

我们可以通过

  • Dependencies软件
  • 任务管理器 - 性能 - 资源监视器 - 模块

查看exedll依赖

但是一个个复制实在是,情けない,丢程序员的脸!

Qt为我们提供了命令行工具windeployqt来自动化部署(这名字:win + deploy +qt 秀)

当然,我们不能在cmd中直接使用该命令

而是应该在Qt提供的命令行环境 Qt 5.15.2 (MSVC 2019 64-bit) | Qt 5.15.2 (MinGW 8.1.0 64-bit)中进行(选择对应的编译器和版本)

详细原因请参考我的另一篇blog:windeployqt发布项目 为何总是缺少dll - MrBeanC-Blog (mrbeancpp.github.io)

我们这次的重点并不在于此

windeployqt

//该工具可以帮助我们将exe所需要的Qt-dll拷贝到exe同级目录下

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
36
37
38
39
40
41
42
Usage: windeployqt [options] [files]

Options:
-?, -h, --help Displays this help.
-v, --version Displays version information.
--dir <directory> Use directory instead of binary directory.
--libdir <path> Copy libraries to path.
--plugindir <path> Copy plugins to path.
--debug Assume debug binaries.
--release Assume release binaries.
--pdb Deploy .pdb files (MSVC).
--force Force updating files.
--dry-run Simulation mode. Behave normally, but do not
copy/update any files.
--no-patchqt Do not patch the Qt5Core library.
--no-plugins Skip plugin deployment.
--no-libraries Skip library deployment.
--qmldir <directory> Scan for QML-imports starting from directory.
--no-quick-import Skip deployment of Qt Quick imports.
--no-translations Skip deployment of translations.
--no-system-d3d-compiler Skip deployment of the system D3D compiler.
--compiler-runtime Deploy compiler runtime (Desktop only).
--no-compiler-runtime Do not deploy compiler runtime (Desktop only).
--webkit2 Deployment of WebKit2 (web process).
--no-webkit2 Skip deployment of WebKit2.
--json Print to stdout in JSON format.
--angle Force deployment of ANGLE.
--no-angle Disable deployment of ANGLE.
--no-opengl-sw Do not deploy the software rasterizer library.
--list <option> Print only the names of the files copied.
Available options:
source: absolute path of the source files
target: absolute path of the target files
relative: paths of the target files, relative
to the target directory
mapping: outputs the source and the relative
target, suitable for use within an
Appx mapping file
--verbose <level> Verbose level.

Arguments:
[files] Binaries or directory containing the binary.

只要在命令行中改变(cd·Change Directory)工作目录至exe所在目录

并执行windeployqt test.exe

便会自动将依赖文件(包括dll和图标等众文件)拷贝至exe所在目录

此时,打包为压缩包并一齐发布即可

MSVC背刺

如果是MSVC编译器,要注意一点

MSVC依赖于VS,可能会用到VS下的一些dll如:MSVCP140.dllC++运行时环境

不属于Qt,所以需要自行拷贝(虽然多数电脑上包含)

More

如果你认为这篇博客到此为止,那就太年轻了

那和一般的CV blog有什么区别

咳咳

windeployqt直接打包发布,运行并无大碍,但问题在于默认复制的依赖多数冗余,需要手动精简

何がいる

libEGL libGLESV2 opengl32sw

libEGL.dll,libGLESV2.dll这两个文件是 ANGLE 的文件,可以去掉

opengl32sw.dll是软件模拟 OpenGL,除非用户的系统连 DirectX 支持都不完整(例如虚拟机),不然也没用

可以用--no-angle & --no-opengl-sw这俩参数去除

svg

如果没有使用svg的话,iconengines\qsvgicon.dll, imageformats\qsvg.dll, Qt5Svg.dll 这三个文件也可以删掉

这是矢量图格式,大部分程序应该都不需要

translations

如果没有国际化支持的话,translations文件夹可以整个删掉

或者使用参数--no-translations

Final

综上,推荐使用:

1
windeployqt --no-angle --no-opengl-sw --no-translations app.exe

Peace

Ref

QT使用windeployqt部署发布及其精简_itas109的博客-CSDN博客

QT MSVC编译项目打包_易日的博客-CSDN博客

windeployqt发布项目 为何总是缺少dll - MrBeanC-Blog (mrbeancpp.github.io)


Qt-windeployqt部署发布浅谈
https://mrbeancpp.github.io/2022/10/12/Qt-windeployqt部署发布浅谈/
作者
MrBeanC
发布于
2022年10月12日
许可协议