软件编写调试好之后最重要的就是打包和发布,这样软件才好传播。
qt软件的打包发布一个难点是必备dll文件的识别。不少日志说用dependencyWalker,今天我试了一下发现查找dll不完全,比如辨识不了qwindows.dll(会报错could not find or load the qt platform plugin windows)。并且还要自己手动添加不是很方便。一番查找后发现qt内置一个现成的dll依赖性工具windeployqt十分好用,该工具在qt的bin/目录下。
参考官方文档:http://doc.qt.io/qt-5/windows-deployment.html#application-dependencies
第一步是添加qt的bin/目录进系统path变量。很简单,在系统属性里设置,添加D:\Qt\5.4\msvc2010_opengl\bin;到系统path环境变量。(qt安装目录不尽相同)
第二部,在任意目录新建文件夹,把编译完成后的qt exe可执行程序拷贝过去。从命令行模式进入该文件夹,执行语句windeployqt xxxx.exe. 之后该工具会分析程序依赖性,自动添加必要dll文件至该目录。运行时命令行提示如下
- D:\TVS\Qt\LGTVSerialControl>windeployqt terminal.exe --release
- D:\TVS\Qt\LGTVSerialControl\terminal.exe 32 bit, release executable
- Adding Qt5Svg for qsvgicon.dll
- Direct dependencies: Qt5Core Qt5Gui Qt5SerialPort Qt5Widgets
- All dependencies : Qt5Core Qt5Gui Qt5SerialPort Qt5Widgets
- To be deployed : Qt5Core Qt5Gui Qt5SerialPort Qt5Svg Qt5Widgets
- Warning: Cannot find Visual Studio installation directory, VCINSTALLDIR is not s
- et.
- Updating icuin53.dll.
- Updating icuuc53.dll.
- Updating icudt53.dll.
- Updating Qt5Core.dll.
- Updating Qt5Gui.dll.
- Updating Qt5SerialPort.dll.
- Updating Qt5Svg.dll.
- Updating Qt5Widgets.dll.
- Creating directory iconengines.
- Updating qsvgicon.dll.
- Creating directory imageformats.
- Updating qdds.dll.
- Updating qgif.dll.
- Updating qicns.dll.
- Updating qico.dll.
- Updating qjp2.dll.
- Updating qjpeg.dll.
- Updating qmng.dll.
- Updating qsvg.dll.
- Updating qtga.dll.
- Updating qtiff.dll.
- Updating qwbmp.dll.
- Updating qwebp.dll.
- Creating directory platforms.
- Updating qwindows.dll.
- Creating qt_ca.qm...
- Creating qt_cs.qm...
- Creating qt_de.qm...
- Creating qt_fi.qm...
- Creating qt_hu.qm...
- Creating qt_it.qm...
- Creating qt_ja.qm...
- Creating qt_ru.qm...
- Creating qt_sk.qm...
- Creating qt_uk.qm...
- D:\TVS\Qt\LGTVSerialControl>
这样必要的库文件就自动添加完毕。
后面就是打包形成安装包了。QT官方推荐了3种软件(https://qt-project.org/wiki/BuildingQtInstallers+&cd=1&hl=en&ct=clnk&gl=us),这里举例第二种软件NSIS说明使用方法。
下载安装NSIS。安装后启动界面如下:
最简单的方法是使用第二个选项Installer based on zip file(由zip文件构建安装包)
把刚才使用了windeployqt的整个目录打包成zip文件,用NSIS加载该zip文件,设置好默认安装目录,点generate就可以打包成安装包了。