linux下對qt編寫的程序進行部署


當我們完成程序設計之后,需要將可執行程序交付客戶,而運行環境里面可能是沒有相關支持庫的,這個時候就涉及到部署的相關問題。對於我們在Linux下基於QT編寫的圖像處理程序,我們采用 linuxdeployqt  進行部署,以下是相關注意步驟。我成功的實現了GOQTTemplate在ubuntu16.04上編譯,並且在ubuntu18.04上的運行:
1.linuxdeployqt 安裝
最簡單的方法直接下載編譯好的 linuxdeployqt-x86_64.AppImage文件( https://github.com/probonopd/linuxdeployqt/releases ),
將其改名字為linuxdeployqt,並chmod a+x,然后復制到 /usr/local/bin/。
命令行輸入 linuxdelpoyqt –version,輸出linuxdeployqt 版本就安裝成功。
 
$ mv linuxdeployqt -x86_64.AppImage linuxdeployqt
$ mv . /linuxdeployqt /usr /local /bin
$ linuxdelpoyqt --version
linuxdeployqt 4 (commit 988d294), build 481 built on 2018 - 02 - 02 15 : 05 : 23 UTC
 
2.打包自己的程序
 
將自己的qt程序(如myQtApp)復制到一個目錄(如 qtTest),運行
 
$ linuxdeployqt . /myQtApp -appimage
 
 
3.在ubuntu 中添加qt 應用程序圖標
 
修改qt 目錄下的desktop 文件。可以按照ubuntu 官方提示修改。
 
#-- 全局安裝(所有用戶可用),將xxx.desktop 復制到/usr/share/applications  
#-- 當前用戶可用, 將xxx.desktop 復制到 ~/.local/share/applications 目錄即可
#--appName.desktop
[Desktop Entry]
Version = 1. 0 #app的版本
Name =myQtApp #app的名字
Comment = this app use for xxx #說明信息 
Exec = /path /to /your /QtApp /myQtApp #app的執行路徑,絕對路徑
Icon = /path /to /your /app_icon /myQtApp.png #icon 路徑,絕對路徑
Terminal =false #是否在終端啟動,效果自己試一下就知道了
Type =Application
Categories =Utility;Application;
 
4、關於qt.conf
這個文件指定了qt 程序的運行環境。
引用qt說明的原話:
The qt.conf file can be used to override the hard-coded paths that are compiled into the Qt library. These paths are accessible using the QLibraryInfo class. Without qt.conf, the functions in QLibraryInfo return these hard-coded paths; otherwise they return the paths as specified in qt.conf.
即我們可以使用qt.conf 指定qt程序的運行路徑和庫路徑。
The file should have a Paths group which contains the entries that correspond to each value of the QLibraryInfo::LibraryLocation enum. See the QLibraryInfo documentation for details on the meaning of the various locations.
這個文件應該要包含QLibraryInfo::LibraryLocation enum,如以下內容
Entry Default Value
Prefix 程序運行的路徑,一下所有的路勁都是相對於這個路徑
Libraries 程序的庫庫路勁,linuxdeployqt會自動再這個目錄生成./lib,並將需要的庫拷貝過來
… 
 
主要有這幾個,多余的可以上qt幫助文檔
 
# Generated by linuxdeployqt
# https://github.com/probonopd/linuxdeployqt/
[Paths]
Prefix = . /         #程序的運行路勁
Libraries =  . /lib   #程序的庫路徑
Plugins = . /plugins #插件路徑
參考:http://doc.qt.io/qt-5/qt-conf.html
5.補充
 
雖然linuxdepoyqt可以幫我們解決多數情況下庫的依賴問題,但是也有的時候不能完整解決。這個時候就需要我們自己復制所依賴的庫。
提供一個腳本,復制依賴庫,復制以下代碼,將其保存成為 copylib.sh
 
#!/bin/sh
bin =$ 1         #發布的程序名稱  

desDir = "./lib" #你的路徑  

if [ ! -d $desDir ]; then
      #echo "makedir $desDir"
      mkdir $desDir
fi 
libList =$(ldd $bin | awk   '{if (match($3,"/")){ printf("%s "),$3 } }')
cp $libList $desDir

命令行 運行
chmod a + x . / copylib.sh
. /copylib.sh . /myapp
 
 
就可以復制所需要的庫到當前目錄下的 ./lib 文件夾中
通常情況下,結合linuxdelpoy 和 copylib.sh 可以解決Linux 下 qt 程序的庫依賴問題
 
 
6. 注意事項
linux下安裝Qt5.7后添加qmake環境變量后出現錯誤
執行
qmake -v
 
出現錯誤:qmake: could not exec ‘/usr/lib/x86_64-linux-gnu/qt4/bin/qmake’: No such file or directory
分析:
qtchooser默認選擇路徑中沒有指向qmake路徑
解決方法:
cd /usr /lib /x86_64 -linux -gnu /qt -default /qtchooser
vim default.conf 
將第一行中的/usr/lib/x86_64-linux-gnu/qt4/bin/qmake
替代為qmake的路徑(我的安裝路徑 /home/xuin/Qt5.7.0/5.7/gcc_64/bin)
 
壓縮解壓縮命令
 
tar -cvf jpg.tar *.jpg //將目錄里所有jpg文件打包成jpg.tar 
 
tar -czf jpg.tar.gz *.jpg   //將目錄里所有jpg文件打包成jpg.tar后,並且將其用gzip壓縮,生成一個gzip壓縮過的包,命名為jpg.tar.gz
 
 tar -cjf jpg.tar.bz2 *.jpg //將目錄里所有jpg文件打包成jpg.tar后,並且將其用bzip2壓縮,生成一個bzip2壓縮過的包,命名為jpg.tar.bz2
 
tar -cZf jpg.tar.Z *.jpg   //將目錄里所有jpg文件打包成jpg.tar后,並且將其用compress壓縮,生成一個umcompress壓縮過的包,命名為jpg.tar.Z
 
rar a jpg.rar *.jpg //rar格式的壓縮,需要先下載rar for linux
 
zip jpg.zip *.jpg //zip格式的壓縮,需要先下載zip for linux
 
解壓
 
tar -xvf file.tar //解壓 tar包
 
tar -xzvf file.tar.gz //解壓tar.gz
 
tar -xjvf file.tar.bz2   //解壓 tar.bz2
 
tar -xZvf file.tar.Z   //解壓tar.Z
 
unrar e file.rar //解壓rar
 
unzip file.zip //解壓zip
 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM