脚本自动编译打包qt程序(windows)

脚本自动编译打包qt程序(windows)

使用环境

本文的测试环境为win10 64位系统,工具使用qt5.7.1 msvc 2015版本,nsis ,cmd等

脚本实现及使用

脚本分为两部分,打包应用相关文件,包括qt动态库,应用lisence等等

代码部分

打包执行脚本

package.bat

1::@echo off 2echo Setting up environment for Qt usage... 3set PATH=C:\Qt\Qt5.7.1\5.7\msvc2015_64\bin;%PATH% 4echo qmake... 5::qmake编译pro文件,本脚本的pro文件在上季以目录 6qmake .. 7echo nmake... 8::qmake之后生成makefile 使用nmake编译生成可执行文件 9nmake release 10::下面这些是打包成安装文件的步骤 11echo set app reference information... 12::脚本第一参数是应用名称(注意不用带扩展) 13set app_name=%1 14::默认nmake生成的文件在当前脚本目录的同级release文件夹下 15set app_path=%~dp0\release 16::设置应用版本,格式为 v1.0.0 17set app_version=%2 18echo Copy Qt references... 19::使用qt自带程序拷贝qt相关文件 20windeployqt "%app_path%\\%app_name%.exe" 21echo Copy license etc. 22::拷贝许可文件,就是安装时候说的那堆责任神马的东西 23copy ..\apps\package\license.txt %app_path% 24::这是msvc的运行库安装文件,偷懒了,其实可以直接复制动态库的 25copy ..\apps\package\vcredist_x64.exe %app_path% 26::qt配置文件,我这里主要是为了配置一些高分辨率下字体大小的 27copy ..\apps\package\qt.conf %app_path% 28echo nsis package... 29::开始用nsis打包成安装文件 30"C:\Program Files (x86)\NSIS\makensis.exe" "D:\work\gitlab\apps\package\normal_package.nsi" 31 32

nsis脚本

normal_package.bat

1; Script generated by the HM NIS Edit Script Wizard. 2 3; HM NIS Edit Wizard helper defines 4!define DOLLAR "$" 5!define PRODUCT_NAME $%app_name% 6!define "CHECK_NAME_${PRODUCT_NAME}" 7!echo "${DOLLAR}%app_name%" 8!if ${PRODUCT_NAME} != "${DOLLAR}%app_name%" 9 !echo "app_name:${PRODUCT_NAME}" 10!else 11 !echo CHECK_NAME_$%%app_name% 12 !error "app_name not specified!" 13!endif 14 15!define PACKAGE_PATH $%app_path% 16 17!define "CHECK_NAME_${PACKAGE_PATH}" 18!if ${PACKAGE_PATH} != "${DOLLAR}%app_path%" 19 !echo "app_path:${PACKAGE_PATH}" 20!else 21 !error "app_path not specified!" 22!endif 23 24!define PRODUCT_VERSION $%app_version% 25!define "CHECK_NAME_${PRODUCT_VERSION}" 26!if ${PRODUCT_VERSION} != "${DOLLAR}%app_version%" 27 !echo "app_version:${PRODUCT_VERSION}" 28!else 29 !error "app_version not specified!" 30!endif 31 32!define PRODUCT_PUBLISHER "INNFOS, Inc." 33!define PRODUCT_WEB_SITE "http://www.innfos.com" 34!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\${PRODUCT_NAME}.exe" 35!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" 36!define PRODUCT_UNINST_ROOT_KEY "HKLM" 37 38; MUI 1.67 compatible ------ 39!include "MUI.nsh" 40 41; MUI Settings 42!define MUI_ABORTWARNING 43!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\Classic-install.ico" 44!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico" 45 46; Welcome page 47!insertmacro MUI_PAGE_WELCOME 48; License page 49!insertmacro MUI_PAGE_LICENSE "${PACKAGE_PATH}\license.txt" 50; Directory page 51!insertmacro MUI_PAGE_DIRECTORY 52; Instfiles page 53!insertmacro MUI_PAGE_INSTFILES 54; Finish page 55!define MUI_FINISHPAGE_RUN "$INSTDIR\${PRODUCT_NAME}.exe" 56!insertmacro MUI_PAGE_FINISH 57 58; Uninstaller pages 59!insertmacro MUI_UNPAGE_INSTFILES 60 61; Language files 62!insertmacro MUI_LANGUAGE "English" 63 64; MUI end ------ 65 66Name "${PRODUCT_NAME} ${PRODUCT_VERSION}" 67OutFile "${PRODUCT_NAME}_Setup.exe" 68;RequestExecutionLevel admin ;Require admin rights on NT6+ (When UAC is turned on) 69InstallDir "$PROGRAMFILES\${PRODUCT_NAME}" 70InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" "" 71ShowInstDetails show 72ShowUnInstDetails show 73;RequestExecutionLevel admin 74 75Section "MainSection" SEC01 76 SetOutPath "$INSTDIR" 77 SetOverwrite ifnewer 78 File "${PACKAGE_PATH}\*.dll" 79 File /r "${PACKAGE_PATH}\*.exe" 80 File /r "${PACKAGE_PATH}\*.conf" 81 File /nonfatal "${PACKAGE_PATH}\*.xml" 82 SetOutPath "$INSTDIR\imageformats" 83 File /r "${PACKAGE_PATH}\imageformats\*.dll" 84 SetOutPath "$INSTDIR\iconengines" 85 File /r "${PACKAGE_PATH}\iconengines\*.dll" 86 SetOutPath "$INSTDIR\platforms" 87 File /r "${PACKAGE_PATH}\platforms\*.dll" 88 89 CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}" 90 CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk" "$INSTDIR\${PRODUCT_NAME}.exe" 91 CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "$INSTDIR\${PRODUCT_NAME}.exe" 92 ;File "${PACKAGE_PATH}\D3Dcompiler_47.dll" 93 SetDetailsPrint textonly 94 DetailPrint 'add input rule ..' 95 ExecWait '"cmd.exe" /c netsh advfirewall firewall add rule name="${PRODUCT_NAME}" dir=in action=allow protocol=udp program="$INSTDIR\${PRODUCT_NAME}.exe"' 96 DetailPrint 'install VC++ library...' 97 SetOutPath $TEMP 98 File "${PACKAGE_PATH}\vcredist_x64.exe" 99 ExecWait '"$TEMP\vcredist_x64.exe" /Q' 100 Delete "$TEMP\vcredist_x64.exe" 101 ;DetailPrint 'install serial port driver...' 102 ;SetOutPath $TEMP 103 ;File "${PACKAGE_PATH}\driver.exe" 104 ;ExecWait '"$TEMP\driver.exe" /Q' 105 ;Delete "$TEMP\driver.exe" 106 SetDetailsPrint both 107SectionEnd 108 109Section -AdditionalIcons 110 WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}" 111 CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\Website.lnk" "$INSTDIR\${PRODUCT_NAME}.url" 112 CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk" "$INSTDIR\uninst.exe" 113SectionEnd 114 115Section -Post 116 WriteUninstaller "$INSTDIR\uninst.exe" 117 WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\${PRODUCT_NAME}.exe" 118 WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)" 119 WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe" 120 WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\${PRODUCT_NAME}.exe" 121 WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}" 122 WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}" 123 WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}" 124SectionEnd 125 126 127Function un.onUninstSuccess 128 HideWindow 129 MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) 已成功地从你的计算机移除。" 130FunctionEnd 131 132Function un.onInit 133 MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "你确实要完全移除 $(^Name) ,其及所有的组件?" IDYES +2 134 Abort 135FunctionEnd 136 137Section Uninstall 138 Delete "$INSTDIR\${PRODUCT_NAME}.url" 139 Delete "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk" 140 Delete "$SMPROGRAMS\${PRODUCT_NAME}\Website.lnk" 141 Delete "$DESKTOP\${PRODUCT_NAME}.lnk" 142 Delete "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk" 143 144 RMDir "$SMPROGRAMS\${PRODUCT_NAME}" 145 RMDir /r "$INSTDIR" 146 147 DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" 148 DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}" 149 SetAutoClose true 150SectionEnd 151 152 153

脚本就不过多解释了,都比较简单,如果哪里不明白可自行百度

使用方法

在这里插入图片描述

1.使用上图中的 64位环境的提示符

1cd your_qt_project_dir 2mkdir build 3cd build 4package.bat "your_app_name" "your_app_version" 5 6

回车,会在package.bat的同级目录生成安装文件,就可以安装使用了

代码交流 2021