Linux下的QT程序的打包、运行、和关闭程序教程整理

目录

------------------------------------------------------

linux平台:建议使用:

参考与说明:

使用linuxdeployqt在linux下进行Qt打包发布 (超详细) (*****)

https://blog.csdn.net/ken2232/article/details/131305081

资源:

https://download.csdn.net/download/ken2232/87934515

linux /windows 双跨平台:CQtDeployer

================================

说明:202303截止版本

  • CQtDeployer:

v1.5 直接安装 ok;v1.6 还需要用户另外安装最新c/c++编译器,麻烦。

运行需要在终端里启动。

使用它打包库,似乎方便?

  • AppImage:

目前还不支持debian 11;10.12也不支持 lxde官方默认版本,其他的没有测试,除非自己安装相应的依赖,才会支持吧?

***最新板已经支持 debian 11:linuxdeployqt-continuous-x86_64.AppImage 15.6 MB 2023 Apr 24

与win相比,linux跨操作系统版本的打包,不容易?所有Qt官方也没有推出 linux版的打包工具?还是?

注:CQtDeployer linux打包工具好像要在linux系统中下载?可能是文件名编码问题?


CQtDeployer

https://github.com/QuasarApp/CQtDeployer

https://github.com/QuasarApp/CQtDeployer/releases/tag/v1.5.4.17 (最后的v1.5

https://github.com/QuasarApp/CQtDeployer/releases/tag/v1.6.2277

CQtDeployer_1.6.2277.ecc6eed_Linux_x86_64.deb

CQtDeployer_1.6.2277.ecc6eed_Linux_x86_64.run

AppImage

https://github.com/probonopd/linuxdeployqt/releases

linuxdeployqt-continuous-x86_64.AppImage   //只有 Linux版

参考:

使用linuxdeployqt在linux下进行Qt打包发布(超详细)

https://blog.csdn.net/ken2232/article/details/131305081

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

Linux下的QT程序的打包、运行、和关闭程序教程整理

https://blog.csdn.net/ken2232/article/details/129380997

===================================

Linux 下Qt应用程序打包发布方法 (测试 OK)

https://blog.csdn.net/wangruifengrr/article/details/123358907

1、首先选择Qt中的“Release”模式,然后点击Run,在Qt工作空间生成相应的

build-项目名称-Desktop_4bec8a-Release文件夹

2、将Qt的打包脚本“deployqtapp.sh”放在build-项目名称-Desktop_4bec8a-Release文件夹内,deployqtapp.sh源码如下:

注意:将第90行的libqxcb.so 替换到本机下“libqxcb.so”的路径。

// l ibqxcb.so替换成用户系统中的实际存在文件路径
qtplatformplugin=/usr/lib/aarch64-linux-gnu/qt5/plugins/platforms/libqxcb.so

3、执行“deployqtapp.sh”脚本,XXX表示在build-项目名称-Desktop_4bec8a-Release文件夹内的可执行文件名。

      注:将可执行文件和资源文件等,拷贝到一个全新目录下再进行,否则,会将该文件夹内的所有的文件,都打包在一起。

sh deployqtapp.sh XXX

4、在build-项目名称-Desktop_4bec8a-Release文件夹内找到ota_deploy压缩文件,解压后运行ota_deploy文件夹内的“runXXX.sh”可执行文件,运行成功即可。

source ./runXXX.sh

---------------------------------------------------------------

deployqtapp.sh 文件

在debian 11上测试:OK

原始文件:  Latest commit on Apr 24, 2014

https://github.com/williamhallatt/qtscripts/blob/master/deployqtapp.sh

#!/bin/bash

#================================================================================
# Copyright (c) 2012 - 2013 by William Hallatt.
# 
# This script is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This script is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this script (GNUGPL.txt).  If not, see
#
#                    <http://www.gnu.org/licenses/>
# 
# Should you wish to contact me for whatever reason, please do so via:
#
#                 <http://www.goblincoding.com/contact>
# 
#================================================================================

# Copy your executable as well as this script to the directory where you 
# want to create the final tar ball.  To execute, simply pass the name of the
# executable as command line parameter to this script.
#
# Worth noting is that I built Qt from source myself, you may or may not need 
# additional plugins, etc and different or additional directory structures and
# will have to edit this script to suit your needs!

if [ $# -ne 1 ]
then
        echo "Usage: $0 <executable name>"
        exit 1
fi

executable=$1

# Obtain the Linux flavour and version.
distro=`lsb_release -d | awk '{print $2$3$4}' | sed 's/\./_/g'`

# Create the directory that will be tarred up for distribution.
tardir=`echo $executable"_"$distro | awk '{print tolower($0)}'`
mkdir $tardir
echo "Created tar ball directory: "$tardir

# Copy executable across.
chmod u+x $executable
cp $executable $tardir
echo "Copied executable "$executable" to "$tardir

# Create the libs directory.
libsdir=$PWD/$tardir/libs
mkdir $libsdir 
echo "Created libs directory: "$libsdir

# Copy all dependencies across to the tar directory.
echo "Copying dependencies..."

for dep in `ldd ./$executable | awk '{print $3}' | grep -v "("`
do
  cp $dep $libsdir
  echo "Copied dependency "$dep" to "$libsdir
done

## 可能需要修改 ##############################################
# Create the fonts directory and copy fonts across. You
# will obviously need to assign the directory path leading
# to your fonts to "fontdir", e.g. /home/you/qt/lib/fonts
qtfontsdir=/home/william/qtsource/qt5/qtbase/lib/fonts
fontsdir=$PWD/$tardir/fonts
mkdir $fontsdir
echo "Created fonts directory: "$fontsdir" copying fonts..."
cp -r $qtfontsdir/* $fontsdir

## 替换到本机下“libqxcb.so”的路径 ##############################################
# You will need to change this to point to wherever libqxcb.so lives on your PC.
qtplatformplugin=/home/william/qtsource/qt5/qtbase/plugins/platforms/libqxcb.so

qtplatformplugindir=$tardir/platforms
mkdir $qtplatformplugindir
echo "Created platforms directory: "$qtplatformplugindir
cp $qtplatformplugin $qtplatformplugindir
echo "Copied platform "$qtplatformplugin" to "$qtplatformplugindir

# Create the script to fix xcb dependencies.
fixscript=$qtplatformplugindir/fixdep.sh
echo "Creating fixdep script: "$fixscript

echo "#!/bin/sh" >> $fixscript
echo "" >> $fixscript
echo "#================================================================================" >> $fixscript
echo "# Copyright (c) 2012 - 2013 by William Hallatt." >> $fixscript
echo "#" >> $fixscript
echo "# This script is free software: you can redistribute it and/or modify it under" >> $fixscript
echo "# the terms of the GNU General Public License as published by the Free Software" >> $fixscript
echo "# Foundation, either version 3 of the License, or (at your option) any later" >> $fixscript
echo "# version." >> $fixscript
echo "#" >> $fixscript
echo "# This script is distributed in the hope that it will be useful, but WITHOUT" >> $fixscript
echo "# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS" >> $fixscript
echo "# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details." >> $fixscript
echo "#" >> $fixscript
echo "# You should have received a copy of the GNU General Public License along with" >> $fixscript
echo "# this script (GNUGPL.txt).  If not, see" >> $fixscript
echo "#" >> $fixscript
echo "#                    <http://www.gnu.org/licenses/>" >> $fixscript
echo "#" >> $fixscript
echo "# Should you wish to contact me for whatever reason, please do so via:" >> $fixscript
echo "#" >> $fixscript
echo "#                 <http://www.goblincoding.com/contact>" >> $fixscript
echo "#" >> $fixscript
echo "#================================================================================" >> $fixscript
echo "" >> $fixscript
echo "# This script will install all \"not found\" dependencies for ALL libraries " >> $fixscript
echo "# that exist within the directory from where it is executed. " >> $fixscript
echo "" >> $fixscript
echo "# All of the packages thus installed can be removed through running the generated " >> $fixscript
echo "# \"removedep.sh\" script if you wish to undo these changes." >> $fixscript
echo "" >> $fixscript
echo "removescript=removedep.sh" >> $fixscript 
echo "> \$removescript" >> $fixscript
echo "echo \"#!/bin/sh\" >> \$removescript" >> $fixscript
echo "" >> $fixscript
echo "# For each library in this directory..." >> $fixscript
echo "for lib in \`ls -F | grep -v @ | grep *.so\`" >> $fixscript
echo "do" >> $fixscript
echo "  # Determine the dependencies, find only those that are not on the system, exclude all Qt5 libraries," >> $fixscript
echo "  # print the name of the library (not full path) and extract the package name from the .so name itself." >> $fixscript
echo "  lddquery=\`ldd \$lib | grep -i \"not found\" | grep -i -v qt5 | awk '{print \$1}' | sed 's/\\(^.*\\)\\.so.*\$/\\1/'\`" >> $fixscript
echo "  #echo \$lddquery" >> $fixscript
echo "" >> $fixscript
echo "  echo \"Installing dependencies for library: \"\$lib" >> $fixscript
echo "  #echo \$lib" >> $fixscript
echo "" >> $fixscript
echo "  for dep in \$lddquery" >> $fixscript
echo "  do" >> $fixscript
echo "    echo \"Found dependency: \"\$dep" >> $fixscript
echo "" >> $fixscript
echo "    # Query apt-cache for the relevant package, excluding dev and dbg versions." >> $fixscript
echo "    packagequery=\`apt-cache search \$dep | grep -v dev | grep -v dbg | awk '{print \$1}'\`" >> $fixscript
echo "    echo \"Querying package list for: \"$packagequery" >> $fixscript
echo "" >> $fixscript
echo "    sudo apt-get install \$packagequery" >> $fixscript
echo "    echo \"Installing: \"\$packagequery" >> $fixscript
echo "    echo \"sudo apt-get remove \$packagequery\" >> \$removescript" >> $fixscript 
echo "  done" >> $fixscript
echo "done" >> $fixscript
echo "" >> $fixscript
echo "chmod u+x \$removescript" >> $fixscript

chmod u+x $fixscript

## 替换到本机下“libqxcb.so”的路径 ##############################################
# Edit this script to add whatever other additional plugins your application
# requires.
qtsqliteplugin=/home/william/qtsource/qt5/qtbase/plugins/sqldrivers/libqsqlite.so
qtsqliteplugindir=$tardir/sqldrivers
mkdir $qtsqliteplugindir
echo "Created sql driver directory: "$qtsqliteplugindir
cp $qtsqliteplugin $qtsqliteplugindir
echo "Copied "$qtsqliteplugin" to "$qtsqliteplugindir

# Create the run script.
execscript=$tardir/"run$executable.sh"
echo "Created run script: "$execscript

echo "#!/bin/sh" > $execscript
echo "export LD_LIBRARY_PATH=\`pwd\`/libs" >> $execscript
echo "export QT_QPA_FONTDIR=\`pwd\`/fonts" >> $execscript
echo "./$executable" >> $execscript

# Make executable.
chmod u+x $execscript

# Create a README
echo "Creating README..."

readme=$tardir/README
echo "================================================================================" >> $readme
echo "Please launch $executable via" >> $readme
echo "" >> $readme
echo "                 $execscript" >> $readme
echo "" >> $readme
echo "If you run into any trouble regarding dependencies, all you need to do is to" >> $readme
echo "run " >> $readme
echo "                 $fixscript " >> $readme
echo "" >> $readme
echo "in order to automatically resolve dependencies on your behalf " >> $readme
echo "(note that you will need administrator privileges for this as this script will" >> $readme
echo "download the necessary libraries for your platform). " >> $readme
echo "" >> $readme
echo "Should you wish to contact me for whatever reason, please do so via:" >> $readme
echo "" >> $readme
echo "                 <http://www.goblincoding.com/contact>" >> $readme
echo "" >> $readme
echo "================================================================================" >> $readme

echo "Creating tarball..."
tar -zcvf $tardir".tar" $tardir

echo "Cleaning up..."
rm -rf $tardir
echo "Done!"

旧版本:

#!/bin/bash
#################    Qt打包脚本源码    ########################
#================================================================================
# Copyright (c) 2012 - 2013 by William Hallatt.
# 
# This script is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This script is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this script (GNUGPL.txt).  If not, see
#
#                    <http://www.gnu.org/licenses/>
# 
# Should you wish to contact me for whatever reason, please do so via:
#
#                 <http://www.goblincoding.com/contact>
# 
#================================================================================
 
# Copy your executable as well as this script to the directory where you 
# want to create the final tar ball.  To execute, simply pass the name of the
# executable as command line parameter to this script.
#
# Worth noting is that I built Qt from source myself, you may or may not need 
# additional plugins, etc and different or additional directory structures and
# will have to edit this script to suit your needs!
 
if [ $# -ne 1 ]
then
        echo "Usage: $0 <executable name>"
        exit 1
fi
 
deployscript=${0##*/}# delete path and leave file name remained
executable=$1
 
# Obtain the Linux flavour and version.
# distro=`lsb_release -d | awk '{print $2$3$4}' | sed 's/\./_/g'`
 
# Create the directory that will be tarred up for distribution.
# tardir=`echo $executable"_"$distro | awk '{print tolower($0)}'`
tardir=`echo $executable"_deploy" | awk '{print tolower($0)}'`
mkdir $tardir
echo "Created tar ball directory: "$tardir
 
# Copy executable across.
chmod u+x $executable
cp $executable $tardir
echo "Copied executable "$executable" to "$tardir
 
# Copy other files and dirs.
nfiles=0
ndirs=0
for files_dirs in `ls ./`
do
  if [ "$files_dirs" != "$deployscript" ] && [ "$files_dirs" != "$executable" ] && [ "$files_dirs" != "$tardir" ]; then
if [ -d "$files_dirs" ]; then
ndirs=$(($ndirs+1))
else
nfiles=$(($nfiles+1))
fi
 
  cp -r $files_dirs $tardir
  fi
done
echo "Copied other $nfiles file(s) and $ndirs dir(s) to "$tardir
 
# Create the libs directory.
libsdir=$PWD/$tardir/libs
mkdir $libsdir 
echo "Created libs directory: "$libsdir
 
# Copy all dependencies across to the tar directory.
echo "Copying dependencies..."
 
for dep in `ldd ./$executable | awk '{print $3}' | grep -v "("`
do
  cp $dep $libsdir
  echo "Copied dependency "$dep" to "$libsdir
done
 
# You will need to change this to point to wherever libqxcb.so lives on your PC.
#替换到本机下“libqxcb.so”的路径
qtplatformplugin=/usr/lib/aarch64-linux-gnu/qt5/plugins/platforms/libqxcb.so

qtplatformplugindir=$tardir/platforms
mkdir $qtplatformplugindir
echo "Created platforms directory: "$qtplatformplugindir
cp $qtplatformplugin $qtplatformplugindir
echo "Copied platform "$qtplatformplugin" to "$qtplatformplugindir
 
echo "Copying qtplatformplugin dependencies..."
for dep in `ldd $qtplatformplugin | awk '{print $3}' | grep -v "("`
do
  cp -u $dep $libsdir
  echo "Copied qtplatformplugin dependency "$dep" to "$libsdir
done
echo "Copied qtplatformplugin dependencies"
 
# Create the run script.
execscript=$tardir/"run$executable.sh"
echo "Created run script: "$execscript
 
echo "#!/bin/sh" > $execscript
echo "export LD_LIBRARY_PATH=\`pwd\`/libs" >> $execscript
# echo "export QT_QPA_FONTDIR=\`pwd\`/fonts" >> $execscript
echo "./$executable" >> $execscript
 
# Make executable.
chmod u+x $execscript
 
# Create a README
echo "Creating README..."
 
readme=$tardir/README
echo "================================================================================" >> $readme
echo "Please launch $executable via" >> $readme
echo "" >> $readme
echo "                 $execscript" >> $readme
echo "" >> $readme
echo "If you run into any trouble regarding dependencies, all you need to do is to" >> $readme
echo "run " >> $readme
echo "                 $fixscript " >> $readme
echo "" >> $readme
echo "in order to automatically resolve dependencies on your behalf " >> $readme
echo "(note that you will need administrator privileges for this as this script will" >> $readme
echo "download the necessary libraries for your platform). " >> $readme
echo "" >> $readme
echo "Should you wish to contact me for whatever reason, please do so via:" >> $readme
echo "" >> $readme
echo "                 <http://www.goblincoding.com/contact>" >> $readme
echo "" >> $readme
echo "================================================================================" >> $readme
 
echo "Creating tarball..."
tar -zcvf $tardir".tar" $tardir
 
echo "Cleaning up..."
rm -rf $tardir
echo "Done!"

————————————————

版权声明:本文为CSDN博主「孤独大佬」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/wangruifengrr/article/details/123358907

===================================

以下方法,之前用过 OK,但不知道是因为最近的系统环境改变了,还是其他的什么原因?竟然 NG了。

注:

方法1:更适合经常性的调试用途。

方法2:更适合用于发布。

一、前言

不管是Ubuntu版本还是CentOS7版本的Linux,QT打包都是一个大问题,困扰了我很久。每次都是打包之后,在本机上是可以运行程序的,但是拿到其他电脑上就提示如下图错误,运行不起来。

下面是我整理的打包教程。

二、打包教程

在Ubuntu和CentOS7下都适用(只试了这两个)。

1.(共用:方法1/2)创建一个单独的文件夹install,将需要发布的QT的可执行程序RoutePlanning复制到install文件夹中。

2.(专用:方法1,更适合经常性的调试用途)在install文件夹中创建脚本文件,命令为:

创建 pack.sh 脚本文件

    touch pack.sh 

在 pack.sh 脚本文件中写入如下内容:

#!/bin/bash
 
LibDir=$PWD
Target=$1
 
lib_array=($(ldd $Target | grep -o "/.*" | grep -o "/.*/[^[:space:]]*"))
 
for Variable in ${lib_array[@]}
do
    cp "$Variable" $LibDir
done

设置脚本权限,命令为:

    chmod 777 pack.sh

执行脚本,命令为:

// RoutePlanning为执行程序,即:需要进行打包的、用户的、Qt可执行程序的名称 
        
./pack.sh RoutePlanning

其中RoutePlanning为执行程序,此时会在该文件夹install下:生成执行程序RoutePlanning依赖的QT基础库。

3.(专用:方法2,更适合用于发布)去QT的安装目录下,找到/home/li/Qt5.12.9/5.12.9/gcc_64/plugins/platforms目录

需要进入特定的目录:

注:这个sh脚本还不够智能化,因此,才需要这个麻烦的步骤。其他一些专门的打包工具,会去掉了这个步骤;但是,需要进行专门打包工具的安装,也是麻烦。

// 需要进入特定目录     
cd /home/li/Qt5.12.9/5.12.9/gcc_64/plugins/platforms

创建pack.sh脚本,写入脚本内容,如下:

#!/bin/bash
 
LibDir=$PWD"/lib"
Target=$1
 
lib_array=($(ldd $Target | grep -o "/.*" | grep -o "/.*/[^[:space:]]*"))
 
$(mkdir $LibDir)
 
for Variable in ${lib_array[@]}
do
    cp "$Variable" $LibDir
done

这两个脚本在功能上是一样的,都是生成执行程序的依赖库

只不过区别在于,2步骤直接将依赖库生成在当前目录;3步骤创建了一个lib文件夹,用来存放依赖库,目的是方便清除,不改变QT的安装目录下的文件。

执行脚本,命令为:

    ./pack.sh libqxcb.so

会在该目录下生成一个lib文件夹

将里面的内容复制到install文件夹下

    cd lib
    cp * /home/install/ -rf

删除lib文件夹

    cd ../
    rm lib -rf 

4.将platfroms文件夹也复制到install文件夹下。

    cp platforms /home/install -rf

5.当程序中引用了模块时,也需要将模块库复制到install文件夹下,比如我的程序中引用了network,需要libQt5WebSockets.so.5库,找到/home/li/Qt5.12.9/5.12.9/gcc_64/lib目录

    cp /home/li/Qt5.12.9/5.12.9/gcc_64/lib/libQt5WebSockets.so.5.12.9 ./ -d
    cp /home/li/Qt5.12.9/5.12.9/gcc_64/lib/libQt5WebSockets.so.5 ./ -d

我试了复制这两个文件才能把链接复制过来。可以找更好的方法。

6.将自己创建的动态库链接复制到install文件夹下。

这样程序打包就完成了。

三、运行程序(共用:方法1/2,以下同)

在install文件夹下创建RoutePlanning.sh脚本文件,文件下写入如下内容:

#!/bin/sh
appname=`basename $0 | sed s,\.sh$,,`
 
dirname=`dirname $0`
tmp="${dirname#?}"
 
if [ "${dirname%$tmp}" != "/" ]; then
dirname=$PWD/$dirname
fi
LD_LIBRARY_PATH=$dirname
export LD_LIBRARY_PATH
$dirname/$appname "$@"

设置权限

chmod 777 RoutePlanning.sh

执行

./RoutePlanning.sh 

此时的执行程序在关闭终端之后也会随之关闭,如果想把程序放入后台运行,即使关闭终端也不影响程序进程的运行,可以在install文件夹下创建run.sh脚本并添加如下脚本内容:

#!/bin/sh
path=$(pwd -P)
echo ${path}
nohup ${path}/RoutePlanning.sh &

四、关闭程序

关闭程序可以通过杀进程的方式。在install文件夹下创建close.sh脚本并写入如下内容:

#!/bin/bash
 
PID=`ps -ef | grep RoutePlanning | grep -v "grep" | awk '{print $2}'`
 
if [ $? -eq 0 ]; then
    echo "process id:$PID"
else
    echo "process $input1 not exit"
    exit
fi
 
 
kill -9 ${PID}
 
if [ $? -eq 0 ];then
    echo "kill $input1 success"
else
    echo "kill $input1 fail"
fi

执行脚本即可关闭。

————————————————

版权声明:本文为CSDN博主「做一个坚强的女汉子」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/m0_67254672/article/details/125643016

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值