logo头像

技术引领生活!

Linux下Qt一键发布脚本

在Linux下,使用Qt开发完一个项目后,需要将项目发布到其他机器上,如果手动操作,需要安装Qt,配置环境变量,编译项目,打包项目,非常麻烦,因此,写了一个一键发布脚本,可以自动完成上述操作。

参考

原理参考 南理汉子如何打包我们用 Qt 写的软件 - Linux 篇 一文, 感谢!

假设软件为 myapp, 目录为~/bin, Qt 安装目录为 /opt/Qt/5.12.8/gcc_64
总结起来就是:

  1. 运行ldd.sh myapp
  2. 将ldd.sh拷贝到 /opt/Qt/5.12.8/gcc_64/plugin/platforms 目录 执行 ldd.sh libqxcb.so
  3. cp -R /opt/Qt/5.12.8/gcc_64/plugin/platforms/lib/* ~/bin/lib/
  4. cp -R /opt/Qt/5.12.8/gcc_64/plugin/platforms ~/bin/
  5. 如果有数据库, 则 cp -R /opt/Qt/5.12.8/gcc_64/plugin/sqldrivers ~/bin/

一键脚本

不废话,使用方法

1
2
3
./onekeydeploy.sh myapp
#完成后运行
./myapp.sh

脚本如下(文末可下载)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash


# 获取所有的 libqxcb.so路径
qxcbs=`locate libqxcb.so`
numOfqxcbs=`locate libqxcb.so | wc -l`
#echo ${qxcbs}



# 如果有多个libqxcb.so, 则提示
if [ $numOfqxcbs -ne 1 ]; then
cindex=0
for xcb in $qxcbs
do
((cindex++))
echo $cindex $xcb
done

echo ""
echo "choose the serial of libqxcb.so :"
read serial
fi


#根据输入的序号, 获取对应的libqxcb.so路径
cindex=0
for i in $qxcbs
do
qcxb=$i
((cindex++))
if [ ${cindex} -eq ${serial} ]; then
break
fi
done

echo "libqxcb is:" $qcxb
echo ""

# 当前路径下的libs目录不存在,则创建
LibDir=$PWD"/libs"
if [ ! -d "$LibDir" ]; then
$(mkdir $LibDir)
fi


#获取额外的Qt 库路径
Target=$1
lib_array=($(ldd $Target | grep -o "/.*" | grep -o "/.*/[^[:space:]]*"))

echo "extra Qt lib is: $lib_array"


# 判断是否需要sqldrivers
sql="Sql"
needSql=0
for Variable in ${lib_array[@]}
do
cp "$Variable" $LibDir
if [[ $Variable == *$sql* ]]
then
needSql=1
fi
done

if [ ${needSql} -eq 1 ]; then
echo "need sql ${needSql}"
fi


echo "copy $1 libs finshed"
echo "========================"

xcb_array=($(ldd $qcxb | grep -o "/.*" | grep -o "/.*/[^[:space:]]*"))
for Variable in ${lib_array[@]}
do
cp "$Variable" $LibDir
done


echo " "
echo "copy all libs of '$qcxb' finshed"
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>"


# 拷贝platforms目录到当前文件夹下
platforms="$(dirname "$qcxb")"
echo "paltforms dir is : $platforms"

cp -r $platforms $PWD

if [ "$needSql" -eq 1 ];
then
sqldrivers="$(dirname "$platforms")/sqldrivers"
cp -r $sqldrivers $PWD
echo " "
echo "copy $sqldrivers finshed"
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
fi

# 删除platforms下冗余的lib目录
redundancyLib=$PWD/platforms/libs
rm -rf $redundancyLib

# 生成当前可执行文件对应的 .sh脚本

echo "#!/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/libs:\$LD_LIBRARY_PATH

export LD_LIBRARY_PATH
cd \$dirname/

\$dirname/\$appname \"\$@\"
"> $1.sh

echo ""
echo "#################################################"
echo " generate '${1}.sh' start up script finshed"

更多打包

  1. linuxdeployqt的主页下载linuxdeployqt
1
2
3
4
5
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
export LD_LIBRARY_PATH=/opt/Qt/5.15.2/gcc_64/lib
  1. 执行打包
    1
    ./linuxdeployqt myapp -appimage

附件下载

一键打包工具

支付宝打赏 微信打赏

您的支持是我前行的动力!