使用linuxdeployqt工具在Linux上部署Qt项目

使用linuxdeployqt工具在Linux上部署Qt项目

linuxdeployqt 依赖

linuxdeployqt 工具介绍

https://github.com/probonopd/linuxdeployqt

linuxdeployqt 是一个用于在 Linux 平台上打包 Qt 应用程序的工具。它的主要功能是自动处理 Qt 应用程序的依赖关系,生成一个独立的、可移植的应用程序包。它能够将应用程序所依赖的 Qt 库、插件和其他运行时依赖项一起打包,并为应用程序生成启动脚本和其他必要的文件。

使用 linuxdeployqt,您可以将 Qt 应用程序打包为一个单独的目录,其中包含了应用程序所需的所有文件和依赖项。这使得应用程序可以在不安装 Qt 运行时的情况下运行,因为所有的依赖关系都被捆绑在应用程序包中。此外,linuxdeployqt 还可以生成 AppImage 格式的应用程序包,这是一种流行的 Linux 应用程序打包和分发格式。

linuxdeployqt 是一个命令行工具,它的使用方式类似于在终端中运行其他命令。您可以指定要打包的 Qt 应用程序的路径,然后 linuxdeployqt 会自动分析应用程序的依赖关系,并生成一个打包好的应用程序包。

总结来说,linuxdeployqt 是一个方便的工具,用于将 Qt 应用程序打包成独立、可移植的应用程序包,使其在 Linux 平台上更容易分发和运行。

linuxdeployqt 安装

# 安装 curl 工具
apt-get install curl -y

# 从 github 中下载 linuxdeployqt 程序
sudo curl -L -o /usr/local/bin/linuxdeployqt https://github.com/probonopd/linuxdeployqt/releases/download/continuous/linuxdeployqt-continuous-x86_64.AppImage

# 给 linuxdeployqt 程序添加可执行权限
sudo chmod a+x linuxdeployqt-continuous-x86_64.AppImage

“chmod a+x” 是一个用于更改文件或目录权限的命令。下面是对该命令的解释:

  • “chmod” 是 “change mode” 的缩写,用于更改文件或目录的访问模式(权限)。
  • “a” 表示 “all”,指代所有用户的权限。
  • “+” 表示添加权限。
  • “x” 表示 “execute”,即执行权限。
    因此,“chmod a+x” 命令的意思是将文件或目录的执行权限添加给所有用户。执行权限允许用户执行该文件(对于可执行文件)或进入该目录(对于目录)。

linuxdeployqt 使用

详细使用方法可以执行以下命令行

linuxdeployqt -h

输出内容如下:

linuxdeployqt  (commit 8428c59), build 47 built on 2023-04-23 17:29:33 UTC

Usage: linuxdeployqt <app-binary|desktop file> [options]

Options:
   -always-overwrite        : Copy files even if the target file exists.
   -appimage                : Create an AppImage (implies -bundle-non-qt-libs).
   -bundle-non-qt-libs      : Also bundle non-core, non-Qt libraries.
   -exclude-libs=<list>     : List of libraries which should be excluded,
                              separated by comma.
   -ignore-glob=<glob>      : Glob pattern relative to appdir to ignore when
                              searching for libraries.
   -executable=<path>       : Let the given executable use the deployed libraries
                              too
   -extra-plugins=<list>    : List of extra plugins which should be deployed,
                              separated by comma.
   -no-copy-copyright-files : Skip deployment of copyright files.
   -no-plugins              : Skip plugin deployment.
   -no-strip                : Don't run 'strip' on the binaries.
   -no-translations         : Skip deployment of translations.
   -qmake=<path>            : The qmake executable to use.
   -qmldir=<path>           : Scan for QML imports in the given path.
   -qmlimport=<path>        : Add the given path to QML module search locations.
   -show-exclude-libs       : Print exclude libraries list.
   -verbose=<0-3>           : 0 = no output, 1 = error/warning (default),
                              2 = normal, 3 = debug.
   -updateinformation=<update string>        : Embed update information STRING; if zsyncmake is installed, generate zsync file
   -qtlibinfix=<infix>      : Adapt the .so search if your Qt distribution has infix.
   -version                 : Print version statement and exit.

linuxdeployqt takes an application as input and makes it
self-contained by copying in the Qt libraries and plugins that
the application uses.

By default it deploys the Qt instance that qmake on the $PATH points to.
The '-qmake' option can be used to point to the qmake executable
to be used instead.

Plugins related to a Qt library are copied in with the library.

See the "Deploying Applications on Linux" topic in the
documentation for more information about deployment on Linux.

查看以上输出信息,可以发现和在windows系统中使用 Qt windeployqt.exe 工具类似。

配置环境变量
  1. 打开终端并以管理员身份登录。

  2. 编辑/etc/profile文件:

sudo nano /etc/profile

如果您使用带图形界面的系统,可以使用sudo gedit /etc/profile命令来编辑文件。

  1. 在文件末尾添加以下内容:
export PATH=/opt/Qt/5.15.2/gcc_64/bin:$PATH
export LIB_PATH=/opt/Qt/5.15.2/gcc_64/lib:$LIB_PATH
export PLUGIN_PATH=/opt/Qt/5.15.2/gcc_64/plugins:$PLUGIN_PATH
export QML2_PATH=/opt/Qt/5.15.2/gcc_64/qml:$QML2_PATH
  1. 按下 Ctrl + O 保存文件,然后按下 Ctrl + X 关闭编辑器。

  2. 重新启动终端或注销并重新登录以使更改生效,或者可以在当前Shell中立即生效,使用命令source /etc/profile

请注意,这种配置将适用于所有用户,因此在更改环境变量之前,请确保了解其影响,并确保不会破坏其他用户的环境。如果只想为特定用户设置环境变量,可以编辑该用户的~/.bashrc文件或~/.profile文件,并将环境变量添加到其中。

对于常规(非QML)程序

执行以下命令,自动将相关依赖项打包到应用程序目录:

linuxdeployqt <应用程序> -appimage 
对于 QML 程序

需要指定项目源代码目录(非系统Qt目录),让linuxdeployqt扫描源代码目录中的*.qml文件以获取依赖信息,这样将自动复制QML的相关依赖项。

linuxdeployqt <应用程序> -appimage -qmldir <指向项目源代码路径> 

这些命令将帮助您在 Linux 平台上打包和部署 Qt 应用程序。

打包后的程序如下图所示:
使用linuxdeployqt工具在Linux上部署Qt项目

拷贝该目录到需要部署的系统上运行即可。

猜你喜欢

转载自blog.csdn.net/cheungxiongwei/article/details/131737576