一般来讲,编译生成 Qt5 应用程序后,直接双击应用程序,会跳出缺少这个 dll 缺少那个 dll 的错误。那么怎么来找齐这些 dll 呢?
一、蛮力大法
顾名思义,就是一个一个dll找。先把exe复制到一个文件夹里,双击运行,根据提示,一个一个从Qt的bin文件夹里找到这些dll文件。
可是这种方法很不优雅啊?
二、设置路径法
这种方法,需要在目标机里再重新安装一次Qt,然后将Qt的bin路径设置到环境变量里面。倒也很省事,只是如果目标机不方便安装Qt,也就不好办了。
三、使用 windeployqt 工具
打开Qt程序目录里面的命令行,敲入 windeployqt –help 命令,就会蹦出他的详细用法
C:\Qt\Qt5.3.2\5.3\mingw482_32>windeployqt --help Usage: windeployqt [options] [file] Qt Deploy Tool 5.3.2 The simplest way to use windeployqt is to add the bin directory of your Qt installation (e.g. <QT_DIR\bin>) to the PATH variable and then run: windeployqt <path-to-app-binary> If ICU, ANGLE, etc. are not in the bin directory, they need to be in the PATH variable. If your application uses Qt Quick, run: windeployqt --qmldir <path-to-app-qml-files> <path-to-app-binary> 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. --debug Assume debug binaries. --release Assume release binaries. --force Force updating files. --dry-run Simulation mode. Behave normally, but do not copy/update any files. --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. --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. Qt libraries can be added by passing their name (-xml) or removed by passing the name prepended by --no- (--no-xml). Available libraries: bluetooth clucene concurrent core declarative designer designercomponents enginio gui qthelp multimedia multimediawidgets multimediaquick network nfc opengl positioning printsupport qml quick quickcompilerruntime quickparticles quickwidgets script scripttools sensors serialport sql svg test webkit webkitwidgets websockets widgets winextras xml xmlpatterns Arguments: [file] Binary or directory containing the binary.
比如 LT 编译出来的应用程序路径为 C:\Users\liangtao\Documents\Qt Projects\build-test_hmi-Desktop_Qt_5_3_MinGW_32bit-Release\release\test_hmi.exe,那么输入命令
C:\Qt\Qt5.3.2\5.3\mingw482_32>windeployqt "C:\Users\liangtao\Documents\Qt Projec ts\build-test_hmi-Desktop_Qt_5_3_MinGW_32bit-Release\release\test_hmi.exe" C:\Users\liangtao\Documents\Qt Projects\build-test_hmi-Desktop_Qt_5_3_MinGW_32bi t-Release\release\test_hmi.exe 32 bit, release executable Skipping plugin qtaccessiblequick.dll due to disabled dependencies. Adding Qt5Svg for qsvgicon.dll Direct dependencies: Qt5Core Qt5Widgets All dependencies : Qt5Core Qt5Gui Qt5Widgets To be deployed : Qt5Core Qt5Gui Qt5Svg Qt5Widgets Updating icuin52.dll. Updating icuuc52.dll. Updating icudt52.dll. Updating Qt5Core.dll. Updating Qt5Gui.dll. Updating Qt5Svg.dll. Updating Qt5Widgets.dll. Updating libgcc_s_dw2-1.dll. Updating libstdc++-6.dll. Updating libwinpthread-1.dll. Creating directory accessible. Updating qtaccessiblewidgets.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_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...
可以看到它自动把一大堆dll搬运到这个目录里面,运行看看,可以运行了!
源码下载
四、扩展阅读
- Qt for Windows – Deployment
http://doc.qt.io/qt-5/windows-deployment.html#application-dependencies - This Linux Deployment Tool
https://github.com/probonopd/linuxdeployqt