Linux 将Qt程序打包为AppImage包

前言

在 Linux 环境下,开发完 Qt 程序后,也需要制作为一个安装包或者可执行文件进行分发。这里介绍使用 linuxdeployqt 将 Qt 程序打包为 .AppImage 应用程序(类似于 Windows 的绿色免安装软件)

环境配置

配置 Qt 环境变量

这一步是为了能让 linuxdeployqt 工具识别到 Qt 的环境。编辑 bashrc 文件

vim ~/.bashrc

在最后一行后面加入自己的 Qt 环境

#QT ENV
export QTDIR=/home/leo/Qt/5.15.2/gcc_64

export PATH=$QTDIR/bin:$PATH
export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH
export QT_PLUGIN_PATH=$QTDIR/plugins:$QT_PLUGIN_PATH
export QML2_PATH=$QTDIR/qml:$QML2_PATH

立即生效

source ~/.bashrc

验证

qmake -v

Linux 将Qt程序打包为AppImage包_第1张图片

配置 linuxdeployqt

前往 linuxdeployqt 下载 release 发布的 linuxdeployqt-continuous-x86_64.AppImage

然后授予可执行权限,并改个名方便使用,放入 /usr/local/bin 路径下,方便随地使用

chmod 777 linuxdeployqt-continuous-x86_64.AppImage
mv linuxdeployqt-continuous-x86_64.AppImage linuxdeployqt
sudo mv linuxdeployqt /usr/local/bin

测试一下

linuxdelpoyqt --version

piMs7NQ.png

配置 AppImageKit

前往 AppImageKit 下载 appimagetool-x86_64.AppImage

同样地,添加执行权限:

chmod 777 appimagetool-x86_64.AppImage

安装 patchelf

直接 apt 安装 patchelf 即可

sudo apt install patchelf

打包

将应用拷贝至 output 目录下,运行命令(这里以 QClipboard 可执行程序为例):

leo@leo-VirtualBox:~/Desktop/output$ linuxdeployqt QClipboard -appimage
linuxdeployqt  (commit 6fcaf74), build 55 built on 2023-09-23 13:33:41 UTC
Not using FHS-like mode
app-binary: "/home/leo/Desktop/output/QClipboard"
appDirPath: "/home/leo/Desktop/output"
relativeBinPath: "QClipboard"
ERROR: Desktop file missing, creating a default one (you will probably want to edit it)
ERROR: Icon file missing, creating a default one (you will probably want to edit it)
qmakePath 3= ""
appimagetool, continuous build (commit 8bbf694), build <local dev build> built on 2020-12-31 11:48:33 UTC
fatal: 不是 git 仓库(或者任何父目录):.git
Failed to run 'git rev-parse --short HEAD: Child process exited with code 128 (code 128)
Desktop file: /home/leo/Desktop/output/default.desktop
Categories entry not found in desktop file
.desktop file is missing a Categories= key

然后就可以看到,output 目录下生成了一些文件

lrwxrwxrwx 1 leo leo     10 11月  3 21:31 AppRun -> QClipboard
-rw-rw-r-- 1 leo leo    123 11月  3 21:31 default.desktop
-rw-rw-r-- 1 leo leo      0 11月  3 21:31 default.png
drwxrwxr-x 6 leo leo   4096 11月  3 21:31 doc
drwxrwxr-x 2 leo leo   4096 11月  3 21:32 lib
drwxrwxr-x 7 leo leo   4096 11月  3 21:32 plugins
-rwxrwxr-x 1 leo leo 157512 11月  3 21:30 QClipboard
-rw-rw-r-- 1 leo leo    145 11月  3 21:32 qt.conf
drwxrwxr-x 2 leo leo   4096 11月  3 21:32 translations

修改 desktop 文件

我们需要修改默认生成的 desktop 文件

leo@leo-VirtualBox :~/Desktop/output$ cat default. desktop 
[Desktop Entry]
Type=Application
Name=Application
Exec=AppRun %F
Icon=default
Comment=Edit this default file
Terminal=true

可以查看 desktop 文件规范进行修改

这里将 default. desktop 文件重命名为 QClipboard. desktop 文件,并修改为以下内容:

[Desktop Entry]
Categories=System; Office;
Type=Application
Keywords=clip; clipboard;
Name=QClipboard
Exec=AppRun %F
Icon=logo
Comment=A cross-platform clipboard tool that allows selecting items from clipboard history to paste.
Terminal=true

然后删除了多余的文件夹,最终效果如下:
Linux 将Qt程序打包为AppImage包_第2张图片

生成 appimage 应用

使用 appimagetool 生成 appimage 应用。

执行 ./appimagetool-x 86_64. AppImage output/ 后面的output就是我们的 output 目录。

注意:由于下载的appimagetool-x 86_64. AppImage应用并没有放入/usr/local/bin路径下,所以不能直接执行它。

leo@leo-VirtualBox :~/Downloads$ ./appimagetool-x 86_64. AppImage ~/Desktop/output/
appimagetool, continuous build (commit 8 bbf 694), build <local dev build> built on 2020-12-31 11:48:33 UTC
/home/leo/Desktop/output/QClipboard. desktop: hint: value "System; Office;" for key "Categories" in group "Desktop Entry" contains more than one main category; application might appear more than once in the application menu
Using architecture x 86_64
/home/leo/Desktop/output should be packaged as QClipboard-x 86_64. AppImage
WARNING: AppStream upstream metadata is missing, please consider creating it
         in usr/share/metainfo/QClipboard. appdata. xml
         Please see https://www.freedesktop.org/software/appstream/docs/chap-Quickstart.html#sect-Quickstart-DesktopApps
         for more information or use the generator at http://output.jsbin.com/qoqukof.
Generating squashfs...
Parallel mksquashfs: Using 6 processors
Creating 4.0 filesystem on QClipboard-x 86_64. AppImage, block size 131072.
[========================================================================/] 530/530 100%

Exportable Squashfs 4.0 filesystem, gzip compressed, data block size 131072
	compressed data, compressed metadata, compressed fragments,
	compressed xattrs, compressed ids
	duplicates are removed
Filesystem size 25011.83 Kbytes (24.43 Mbytes)
	39.56% of uncompressed filesystem size (63223.96 Kbytes)
Inode table size 2316 bytes (2.26 Kbytes)
	55.20% of uncompressed inode table size (4196 bytes)
Directory table size 861 bytes (0.84 Kbytes)
	52.25% of uncompressed directory table size (1648 bytes)
Number of duplicate files found 0
Number of inodes 69
Number of files 59
Number of fragments 12
Number of symbolic links  2
Number of device nodes 0
Number of fifo nodes 0
Number of socket nodes 0
Number of directories 8
Number of ids (unique uids + gids) 1
Number of uids 1
	root (0)
Number of gids 1
	root (0)
Embedding ELF...
Marking the AppImage as executable...
Embedding MD 5 digest
Success

Please consider submitting your AppImage to AppImageHub, the crowd-sourced
central directory of available AppImages, by opening a pull request
at https://github.com/AppImage/appimage.github.io

然后就可以在appimagetool-x 86_64. AppImage同级路径下,看到生成的QClipboard-x 86_64. AppImage包了

执行测试

QClipboard 应用成功执行

注意,分发给别人时,需要加上执行权限才可运行

总结

以上就是在 Linux 系统下,打包 Qt 程序为 AppImage 程序的全部过程。

同时欢迎对跨平台剪贴板工具加star:https://github.com/L-Super/QClipboard

https://blog.csdn.net/zyhse/article/details/106381937

https://www.cnblogs.com/linuxAndMcu/p/11016322.html

UnityLaunchersAndDesktopFiles - Community Help Wiki (ubuntu.com)

desktop文件规范 | DeepinWiki

icon-theme-spec

你可能感兴趣的:(linux,qt,AppImage,linuxdeployqt)