Windows安装包种类:
1、MSI 一般用于驱动程序,安装时直接覆盖已有文件和程序
2、EXE 常用的安装程序,缺点是卸载时会有残留文件
3、APPX uwp应用推出时推出的一种安装包,只能用于windows8之后,之前的应用打包为此种格式比较复杂,不流行
4、MSIX 最新推出的格式支持windows所有平台(桌面、手机、平板),类似于苹果的安装包,需要签名,安全,做到干净卸载。windows 10后推出,且微软也提供了工具将上面四种安装包转换为MSIX格式安装包
虽然MSIX是windows推荐的安装包,也具有安全,无残留的有点,不过对于桌面端程序目前还是exe占主流。
Windows下exe安装包打包流程
- Power Shell环境配置
执行如下命令更改Power Shell执行策略(否则无法运行脚本)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Windows的可执行文件引用的三方动态库(dll)描述只有相对路径一种方式(相对于可执行文件的路径),所以拷贝三方库后也就不需要MacOS中的步骤(4)了,而且exe安装包也不需要签名,Qt官方也给出了步骤(3)的工具windeployqt
构建脚本
将 install_win.iss 文件与脚本放置于同一个目录下,install_win.iss内容如下:
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
#define MyAppName "MytestApp"
#define MyAppVersion "1.5"
#define MyAppPublisher "My Company, Inc."
#define MyAppURL "https://www.example.com/"
#define MyAppExeName "MytestApp.exe"
[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{46760BD9-178F-48D7-9585-C27EE30A9573}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppName}
DisableProgramGroupPage=yes
; Uncomment the following line to run in non administrative install mode (install for current user only.)
;PrivilegesRequired=lowest
OutputDir=.\Result\out
OutputBaseFilename=MytestAppInstaller
Compression=lzma
SolidCompression=yes
WizardStyle=modern
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
[Files]
Source: "C:\Users\zsz\CLionProjects\MytestApp\deploy\Result\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\zsz\CLionProjects\MytestApp\deploy\Result\out\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
@echo off
setlocal
:Help
echo Usage: win_build.bat [options]
echo Options:
echo -h: show this help message
echo -t: build type, release or debug
echo -qt: qt path
echo -vcpkg: vcpkag path
echo -new: build version
REM 构建类型
set BUILD_TYPE=Release
REM 构建编译号
set BUILD_VERSION=1
REM Qt 安装路径
set QT_DIR=C:\Qt\5.15.2\msvc2019_64
REM vcpkg路径
set VCPKG_PATH=C:\Users\zsz\Developer\vcpkg
:Loop
if "%1"=="" goto EndLoop
if "%1"=="-t" set BUILD_TYPE=%2
if "%1"=="-qt" set QT_DIR=%2
if "%1"=="-vcpkg" set VCPKG_PATH=%2
if "%1"=="-new" set BUILD_VERSION=%2
if "%1"=="-h" goto Help
shift
goto Loop
:EndLoop
REM 获取当前目录
set CUR_DIR=%cd%
set PROJECT_NAME=MytestApp
REM cmake路径
set CMAKE_PATH=%QT_DIR%\..\..\Tools\CMake_64\bin\cmake.exe
REM ninja路径
set NINJA_PATH=%QT_DIR%\..\..\Tools\Ninja\ninja.exe
REM C/C++编译器路径;注意这里要用"/"而不是"\"
set CLANG_PATH=C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe
REM vcvarsall.bat路径; 用于设置环境变量;
set VCVARSALL="C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat"
REM 编译目录
set BUILD_DIR=%CUR_DIR%\Result
REM 清理上一次构建
if exist %BUILD_DIR% rmdir /s /q %BUILD_DIR%
REM 创建生成目录
mkdir %BUILD_DIR%
REM 使用 CMake 生成项目文件
call %VCVARSALL% x86_x64
call %CMAKE_PATH% -G Ninja ^
"-DCMAKE_BUILD_TYPE=%BUILD_TYPE%" ^
"-DCMAKE_PREFIX_PATH=%QT_DIR%" ^
"-DCMAKE_MAKE_PROGRAM=%NINJA_PATH%" ^
"-DCMAKE_TOOLCHAIN_FILE=%VCPKG_PATH%\scripts\buildsystems\vcpkg.cmake" ^
"-DCMAKE_C_COMPILER=%CLANG_PATH%" ^
"-DCMAKE_CXX_COMPILER=%CLANG_PATH%" ^
-DABS_CLOSE_LANGUAGE=0 ^
-S .. ^
"-B %BUILD_DIR%"
call %CMAKE_PATH% --build %BUILD_DIR% --target %PROJECT_NAME% -j 10
REM 安装目录
set INSTALL_DIR=%BUILD_DIR%\out
if exist %INSTALL_DIR% rmdir /s /q %INSTALL_DIR%
mkdir %INSTALL_DIR%
REM 使用 windeployqt 部署 Qt 依赖
call %QT_DIR%\bin\windeployqt --no-compiler-runtime --dir %INSTALL_DIR% %BUILD_DIR%\%PROJECT_NAME%.exe
set ISS_SCRIPT="install_win.iss"
set ISCC_PATH="C:\Program Files (x86)\Inno Setup 6\ISCC.exe"
call %ISCC_PATH% %ISS_SCRIPT%
echo Build complete!
endlocal
-
最后构建目录如下:
脚本下载地址:
https://gitee.com/nldxrz/ffmpeg-build-scripts/tree/master/qt