用Inno Setup打包QT程序输出安装包

InnoSetup打包编译好的QT程序

介绍

Inno Setup:用于打包安装程序

具体步骤

  1. 首先打开inno setup compiler
    在这里插入图片描述
  2. 第二步
    在这里插入图片描述
  3. 接下来按照图示进行
    image-20240828052907975
    image-20240828053018576

image-20240828053136559

image-20240828053223326

image-20240828053258606

image-20240828053318992

image-20240828053416824

image-20240828053452282

image-20240828053614290

一路next,会弹出一个编译脚本,然后点击是

image-20240828053648236
4. 确认脚本无误后,单击运行,输出安装包
在这里插入图片描述

自定义脚本

在配置完成后,生成的脚本中,就是以script的形式写出了配置内容,可以自行更改。

image-20240828100924952

一个完整的示例

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "qosbrowser"
#define MyAppVersion "0.0.1"
#define MyAppPublisher "XiaoDouLaoShi"
#define MyAppURL "https://www.example.com/"
#define MyAppExeName "qosbrowser.exe"
#define MyEnv "prod"
#define MyRes "static"
#define MyBuild "..\build\build-qos-Desktop_Qt_5_15_2_MSVC2019_64bit-Release\qosbrowser\release"


; 基本信息设置
[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.)
; 它表示一个产品安装包的唯一id,如果做另外一个新产品则需要重新生成一个appid
AppId={{95827DF0-FC44-439C-9225-6CB9BCC43A55}
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=.\output
OutputBaseFilename={#MyAppName}_setup_{#MyEnv}_{#MyAppVersion}
SetupIconFile=.\static\images\logo.ico
Compression=lzma
SolidCompression=yes
WizardStyle=modern
WizardImageFile={#MyRes}\images\setup.bmp
WizardSmallImageFile={#MyRes}\images\setup_small.bmp

; 多语言安装
[Languages]
Name: "chinesesimplified"; MessagesFile: "compiler:Languages\ChineseSimplified.isl"

; 本节是可选的。它定义了安装程序在安装期间将执行的所有用户自定义任务。
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

; 指定需要打包的文件
[Files]
Source: "{#MyBuild}\*"; DestDir: "{app}"; Excludes: "*.cpp,*.h,*.obj,*.res"; Flags: recursesubdirs ignoreversion
Source: "{#MyRes}\tools\VC_redist.x64.exe"; DestDir: "{tmp}/tools"; Flags: ignoreversion

; NOTE: Don't use "Flags: ignoreversion" on any shared system files


[registry]
;本段处理程序在注册表中的键值
Root:HKCU;Subkey:Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers;ValueType: string; ValueName:{app}\qosbrowser.exe;ValueData:~ RUNASADMIN;


; 本节是可选的。定义了安装程序在开始菜单和/或其他位置(如桌面)创建的任何快捷方式。
[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

; 部分是可选的,它指定在程序成功安装之后,但在安装程序显示最终对话框之前要执行的任意数量的程序。
[Run]
Filename: "{tmp}\tools\VC_redist.x64.exe"; Parameters: /q;
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent

更改引入配置文件/动态库路径

这里指定需要打包的文件的路径

; Excludes表示打包时排除哪些文件
[Files]
Source: "{#MyBuild}\*"; DestDir: "{app}"; Excludes: "*.cpp,*.h,*.obj,*.res"; Flags: recursesubdirs ignoreversion

申请管理员权限

如果安装在C盘program files下,则需要管理员权限

;以下内容是修改程序,使其默认以管理员权限运行
[registry]
;本段处理程序在注册表中的键值
Root:HKCU;Subkey:Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers;ValueType: string; ValueName:{app}\qosbrowser.exe;ValueData:~ RUNASADMIN;

设置安装过程界面的图标和图片

; 下面这个static目录要和output(也就是安装包放置目录)同级
#define MyRes "static"
WizardImageFile={#MyRes}\images\setup.bmp
WizardSmallImageFile={#MyRes}\images\setup_small.bmp

C++程序依赖运行库

需要在脚本中添加安装运行库:VC_redist.x64.exe(有些项目依赖这个运行库,但用户电脑可能没有这个库,所以需要在安装软件时同时配置)

image-20240828095410614

1、在Files 小节下添加运行库路径,(后面的{tmp}目录是生成的临时文件夹)

2、不需要在安装过程中弹出提示安装运行库,在[Run]里添加静默安装:Parameters:/q

[Files]
Source: "{#MyRes}\tools\VC_redist.x64.exe"; DestDir: "{tmp}/tools"; Flags: ignoreversion

[Run]
Filename: "{tmp}\tools\VC_redist.x64.exe"; Parameters: /q;

参考:
[1] 慕课网课程

### 使用 Inno Setup 设置 Windows 系统服务 创建和配置 Windows 系统服务可以通过编写特定的脚本来实现,在 Inno Setup 中主要通过 `[Run]` 和 `[UninstallRun]` 部分来管理这些操作。下面介绍一种常见的方式,即利用 `sc.exe` 命令行工具来进行系统服务的操作。 #### 编写安装脚本以添加服务 为了在安装过程中注册一个新的 Windows 服务,可以在 Inno Setup 的脚本文件里加入如下所示的内容: ```pascal [Files] Source: "myservice.exe"; DestDir: "{app}"; Flags: ignoreversion [Run] Filename: "cmd.exe"; Parameters: "/C sc create MyService binPath= ""{app}\myservice.exe"" start= auto"; StatusMsg: "Creating service..."; Flags: runhidden waituntilterminated; OnlyBelowVersion: 0,6.2; // For Windows 8 and above use the following line instead of the one above. // Filename: "powershell.exe"; Parameters: "-Command New-Service -Name 'MyService' -BinaryPathName '{app}\myservice.exe' -StartupType Automatic"; StatusMsg: "Creating service..."; Flags: runasadmin; ``` 这段代码会尝试使用命令提示符下的 `sc` 工具或者 PowerShell 来创建名为 `MyService` 的新服务,并将其启动类型设为自动。注意对于不同版本的 Windows 可能需要采用不同的方法[^1]。 #### 删除已有的服务 当卸载应用程序时同样也需要移除之前建立的服务,这通常是在 `[UninstallRun]` 段落中完成: ```ini [UninstallRun] Filename: "cmd.exe"; Parameters: "/C net stop MyService && sc delete MyService"; StatusMsg: "Removing service..."; Flags: runhidden skipifdoesntexist; ``` 这里先停止再删除指定名称的服务实例。如果该服务不存在,则跳过此步骤而不报错[^3]。 #### 自定义服务行为 除了基本的创建与删除外,还可以进一步定制化服务的行为模式,比如修改描述信息、调整依赖关系等。这类更复杂的设定往往涉及到更多参数传递给 `sc config` 或者其他类似的命令接口[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值