在Linux下,使用Qt开发完一个项目后,需要将项目发布到其他机器上,如果手动操作,需要安装Qt,配置环境变量,编译项目,打包项目,非常麻烦,因此,写了一个一键发布脚本,可以自动完成上述操作。
参考
原理参考 南理汉子的如何打包我们用 Qt 写的软件 - Linux 篇 一文, 感谢!
假设软件为 myapp, 目录为~/bin, Qt 安装目录为 /opt/Qt/5.12.8/gcc_64
总结起来就是:
- 运行ldd.sh myapp
- 将ldd.sh拷贝到 /opt/Qt/5.12.8/gcc_64/plugin/platforms 目录 执行 ldd.sh libqxcb.so
- cp -R /opt/Qt/5.12.8/gcc_64/plugin/platforms/lib/* ~/bin/lib/
- cp -R /opt/Qt/5.12.8/gcc_64/plugin/platforms ~/bin/
- 如果有数据库, 则 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
qxcbs=`locate libqxcb.so` numOfqxcbs=`locate libqxcb.so | wc -l`
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
cindex=0 for i in $qxcbs do qcxb=$i ((cindex++)) if [ ${cindex} -eq ${serial} ]; then break fi done
echo "libqxcb is:" $qcxb echo ""
LibDir=$PWD"/libs" if [ ! -d "$LibDir" ]; then $(mkdir $LibDir) fi
Target=$1 lib_array=($(ldd $Target | grep -o "/.*" | grep -o "/.*/[^[:space:]]*"))
echo "extra Qt lib is: $lib_array"
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="$(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
redundancyLib=$PWD/platforms/libs rm -rf $redundancyLib
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"
|
更多打包
- 去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
| ./linuxdeployqt myapp -appimage
|
附件下载
一键打包工具