寫了點Qt界面程序,然而發現很難移植到其他沒有安裝Qt環境的電腦上運行。查資料了解到,在windows上有windowsdeployqt程序,linux上有linuxdeployqt可以幫助我們快速打包。
1. 配置Qt環境
首先,我們先把Qt環境配置好,在~/.bashrc中加入:
export PATH=/home/xl/Qt5.9.2/5.9.2/gcc_64/bin:$PATH
export LD_LIBRARY_PATH=/home/xl/Qt5.9.2/5.9.2/gcc_64/lib:$LD_LIBRARY_PATH
export QT_PLUGIN_PATH=/home/xl/Qt5.9.2/5.9.2/gcc_64/plugins:$QT_PLUGIN_PATH
export QML2_IMPORT_PATH=/home/xl/Qt5.9.2/5.9.2/gcc_64/qml:$QML2_IMPORT_PATH
其中/home/xl/Qt5.9.2/5.9.2/
目錄要根據自己電腦上安裝的Qt路徑進行修改。
然后執行sourec ~/.bashrc
使配置生效。
2. 編譯linuxdeployqt
項目地址: https://github.com/probonopd/linuxdeployqt.git 。
雖然有放出編譯好的包,但是由于我使用的是Ubuntu18, 系統版本過高,因此還是選擇編譯代碼的方式。
為了避免編譯好的包運行時檢測到我們的系統版本過高,不繼續執行的問題,我們編譯前,將tools/linuxdeployqt/main.cpp
中的下述代碼注釋掉:
// openSUSE Leap 15.0 uses glibc 2.26 and is used on OBS
/*if (strverscmp (glcv, "2.27") >= 0) { //注釋版本檢查
qInfo() << "ERROR: The host system is too new.";
qInfo() << "Please run on a system with a glibc version no newer than what comes with the oldest";
qInfo() << "currently still-supported mainstream distribution (xenial), which is glibc 2.23.";
qInfo() << "This is so that the resulting bundle will work on most still-supported Linux distributions.";
qInfo() << "For more information, please see";
qInfo() << "https://github.com/probonopd/linuxdeployqt/issues/340";
return 1;
}*/
然后就可以使用cmake和make進行編譯。生成好的可執行程序是tools/linuxdeployqt/linuxdeployqt
。
最后為了方便使用,可以將生成的可執行程序拷貝到系統的/usr/local/bin/
目錄。
3. 打包
將Qt編譯的好的程序拷貝到一個單獨的文件夾中。
然后執行linuxdeployqt appname.
一般情況下會很順利的完成,當前目錄下會有個Apprun,直接執行它就可以。
但是有時候并不是那么順利,應該是系統中還缺少相應的庫。比如,我遇到的錯誤是:
ERROR: Could not start patchelf.
ERROR: Make sure it is installed on your $PATH.
ERROR: Error reading rpath with patchelf "libQt5Widgets.so" : ""
ERROR: Error reading rpath with patchelf "libQt5Widgets.so" : ""
這個錯誤是表明缺少需要的pathchelf工具,直接安裝即可解決:
sudo apt install patchelf
然后又出現了下面這個錯誤:
ERROR: ldd outputLine: "libjasper.so.1 => not found"
ERROR: for binary: "/home/xl/Qt5.9.2/5.9.2/gcc_64/plugins/imageformats/libqjp2.so"
ERROR: Please ensure that all libraries can be found by ldd. Aborting.
這表明我們系統中是缺少了libqjp2.so這個庫的。其實很奇怪,本地明明是已經可以跑起來了,為什么還缺少這個庫文件。但是解決方法很簡單,缺什么就裝什么:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
安裝完成之后,就順利打包了。
總結
以上所述是小編給大家介紹的解決Ubuntu下使用linuxdeployqt打包Qt程序問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!