c++ Qt开发学习记录【二】VS2022程序打包为setup.exe安装包


前言

从大佬feiyangqingyun的开源QT代码,转成了VS项目代码,成功编译运行,生成日历软件lunarcalendarwidget.exe。接下来想把这个项目打包成安装包,通过运行setup.exe可以进行安装,并且可以卸载该程序。在打包之前,一定要保证项目成功运行,打包才有意义。


一、VS安装install project扩展

点击【扩展】>【管理扩展】

安装扩展

搜索【install project】并下载扩展程序

install project

这里注意安装好之后关闭所有VS窗口,会跳出对话框,点击【Modify】

在这里插入图片描述

install project 扩展就安装好了

二、打包程序

1.创建打包项目

【文件】>【添加】>【新建项目】

在这里插入图片描述

选择【Setup Project】项目创建,并配置项目名称和位置

在这里插入图片描述
在这里插入图片描述

创建好之后是如下页面,接下来我们需要添加各种依赖文件,之后生成安装包。

在这里插入图片描述

2.添加依赖文件

1.主输出
2.动态库
3.一些附加文件
4.快捷方式
5.卸载程序
6.Qt 平台插件

添加主输出

右键【Application Folder】>【Add】>【项目输出】

在这里插入图片描述

添加主输出文件是这个框起来的文件,而上面的*.dll文件则是在自动检测出来的动态库。如果后续生成是提示缺少什么库还需手动添加。
在这里插入图片描述

附加文件

如果程序用到一些额外的文件,比如图片、字体,需要将其也加入到Application Folder中。这里我添加了image和font文件夹,再把文件添加到文件夹中,里面是程序用到的图片、软件图标.ico文件、字体文件。

在这里插入图片描述

快捷方式

这里我用的方法是在主输出文件上【右键】>【创建主输出的快捷方式】。然后重命名为lunarcalendar,再把它拖拽到【User’s Desktop】中。这样做的目的是:安装之后可以使用用户的桌面出现软件的快捷方式。

在这里插入图片描述

在这里插入图片描述
接着是可选项:修改快捷方式图标。这里图标文件的格式一定要是*.ico,我把他放在了【image】文件夹中。(注:图标文件需要去生成,我是用的这个图标转换网站,把PNG图片转成了图标文件)
方法:首先点击上图所示的快捷方式文件>VS右下角会出现属性栏>点击【ico】这一项后面的【None】,可选择浏览添加图标。然后点击对话框中的【Browse】,并一步步找到需要的*.ico文件,双击,之后就【OK】-【OK】即可。

在这里插入图片描述
在这里插入图片描述

卸载程序

目的是可以通过运行卸载程序来卸载软件。

首先,将卸载程序放在【Application Folder】目录下,右键【Application Folder】>【add】>【文件】,在系统盘下找到卸载程序文件,一般都在:
C:Windows\System32\Msiexec.exe
在这里插入图片描述
接着可以为卸载程序也创建一个桌面快捷方式,参考上面主输出快捷方式的创建方法。下面需要设置卸载参数,告诉卸载程序该卸载哪个程序。

首先,找到安装项目的【ProductCode】,在安装项目的属性面板中可以看到,
复制后面的代码,粘贴到msiexec.exe快捷方式的【Arguments】属性,前面加/x

3.生成安装包并安装/卸载

下面就可以生成项目安装包啦。右键项目,选择【生成】,成功生成是这样的
在这里插入图片描述

找到项目找到生成的路径,双击setup.exe即可安装。
安装成功后运行msiexec.exe即可卸载程序。

因为添加了桌面快捷方式,所以也可通过快捷方式【安装/卸载】
在这里插入图片描述

也可以在右键单击项目,选择【安装/卸载】,来安装和卸载程序。

三、遇到的问题

1.not compatible with the project’s target platform ‘x86’

生成安装包的设置与工程编译的位数不一致
在这里插入图片描述

Building file’C:\Users\14220\source\repos\QtStudy\Setup2\Release\Setup2.msi’…
WARNING: File ‘Qt5Core.dll’ targeting ‘x64’ is not compatible with the project’s target platform ‘x86’

解决:单击项目,在属性栏修改TargetPlatform值,将其改为x64即可。

在这里插入图片描述

2.生成时,在debug模式下成功,在release模式下失败

当时的报错是有个中间文件lunarcalendarwidget.ilk找不到,然而我能在【application Folder】中看到这个文件存在,在想打开这个文件的时候,显示他是不存在的。

Unable to find source file ‘C:\Users\14220\Desktop\bin\lunarcalendarwidget.ilk’ for file ‘lunarcalendarwidget.ilk’, located in ‘[TARGETDIR]’, the file may be absent or locked.

我尝试【重新生成】,还是失败。
解决:我在项目中将这个文件移除,再点【重新生成】,成功

这个.ilk文件是Visual Studio编译器生成的中间文件,它包含了与链接器相关的信息。在编译和链接过程中,编译器会生成这个文件来帮助链接器正确地组织和处理代码。这个文件在编译和链接过程中起到了重要的作用,但对于最终的运行程序来说并不是必需的。

3.成功生成的安装包在别人电脑上安装成功,但程序打开失败

Debug Error! This application failed to start because no Qt platform plugin could beinitialized Reinstalling the application may fix this problem.

这是由于他人电脑没有Qt平台插件,导致无法运行程序。

所以这里的解决方法是,将Qt所需的插件、库一起打包到程序中,即在添加依赖文件时,将Qt安装目录下的一些动态库,放到【application Folder】中。

我的安装地址是D:\SoftWare\Qt\5.12.12\msvc2017_64\plugins\platforms,把这些全复制进去就解决了。(实际上可能不需要这么多,但以防万一)
在这里插入图片描述


总结

以上就是我在VS2022中将一个程序打包成安装包,并解决了遇到的几个问题。不足之处,欢迎指正。

  • 19
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mastering Visual Studio 2019: Become proficient in .NET Framework and .NET Core by:using advanced coding techniques in Visual Studio, 2nd Edition eBook Details: Paperback: 374 pages Publisher: WOW! eBook (August 9, 2019) Language: English ISBN-10: 1789530091 ISBN-13: 978-1789530094 eBook Description: Mastering Visual Studio 2019, 2nd Edition: Explore the new features in Visual Studio 2019 and build apps using WPF, .NET Core, TypeScript, and Azure Visual Studio 2019 is the latest IDE from Microsoft for developers targeting Windows and other platforms to build stunning desktop, web and mobile applications. In this book, you’ll learn how to effectively use this IDE to build, debug, and test your applications to enhance productivity by simplifying your most common tasks and allowing more time to focus on other aspects of your project. Starting with an overview of the new features in Visual Studio 2019, you’ll understand what’s improved since its previous edition. This Visual Studio book will help you build applications for Windows using Windows Presentation Foundation (WPF), which provides a unified programming model for developing dynamic, rich, data-driven desktop applications. You’ll then learn about Microsoft’s implementation of cloud computing with Azure, followed by building applications using .NET Core and TypeScript. This book will also help you explore NuGet, the package manager for the Microsoft development platform, that enables you to create, publish, and consume packages from a central repository by managing a reference list of packages. Finally, you’ll delve into the debugging and live unit testing techniques that are built into the IDE. Increase your productivity with Visual Studio 2019’s new features Understand how the installation wizard works and create an offline installation package Build stunning applications using WPF, .NET Core, and TypeScript Explore NuGet packages in depth Accelerate cloud development with Azure Debug and test your applications efficiently Get to grips with integrating Visual Studio with Git repositories By the end of the Mastering Visual Studio 2019, Second Edition book, you’ll have become a more efficient Visual Studio 2019 user, and be able to use it for developing, testing, debugging and deploying your applications.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值