Linux 下編譯、安裝、配置 QT

聲明: 本文非原創文章,轉載自:http://emouse.cnblogs.com

打算做嵌入式圖像處理,計劃方案嵌入式Linux+OpenCV+QT,昨天簡單入門OpenCV今天看看QT,QT就先弄Linux下面的,回家之前爭取把基本的摸通,然後能在板子上跑起來。

軟件環境


Linux:Ubuntu 12.04 X86

QT:4.8.4

關於QT安裝


QT的早期版本和現在版本有點變化,提供的文件也不盡相同,最新的版本爲5.0,我這裏用的是4.8.4,一個完整的QT應該包括QT庫、IDE也就是QT creator,在Linux下的安裝中,目前看到的主要有三種方法:

1、.bin文件直接安裝,文件名爲qt-sdk-……,一個./bin直接包括了所有相關工具,安裝起來最爲簡單方便,但是目前的4.8.4在幾個網站上已經找不到總的sdk的bin文件了,老版本的還是可以的。

2、Ubuntu下使用atp-get安裝,方法如下(未驗證)

sudo apt-get install qt4-dev-tools #開發包   
sudo apt-get install qtcreator #IDE  
sudo apt-get install qt4-doc #開發幫助文檔   
sudo apt-get install qt4-qtconfig #配置工具   
sudo apt-get install qt4-demos #DEMO源碼  QT

3、這就是本文用的方法,就是下載源碼包,自己編譯安裝,最爲麻煩,但是本來就是學習的,況且現在只能下載到源碼包,那就先學習這種方法把。

軟件下載


QT有好幾個下載網站,這裏彙總介紹,自從諾基亞公司放棄Meego、遣散Qt開發團隊以後,Qt商業版本的網站移到了qt.digia.com,而開源版本則放到了qt-project.org  :

1、首先是諾基亞的官方網站http://qt.digia.com/  這個是不太方便的,想快速下載的接着看後面的。

此網站需要填寫用戶名和郵箱地址,下載地址會發送到郵件裏,我就把我收到的地址貼出來吧。下面是郵件中的地址彙總。這裏面的linux源碼是commercial版本。

Qt 4.8.4 for embedded Linux

http://dist.qt.digia.com/developer/download/qt-everywhere-commercial-src-4.8.4.tar.gz

Installing Qt for embedded Linux

http://qt-project.org/doc/qt-4.8/qt-embedded-install.html

Qt Commercial Charts 1.1.0

http://dist.qt.digia.com/developer/download_charts/qt-commercial-charts-src-1.1.0.tar.gz

Qt 5.0.0 Sources

http://dist.qt.digia.com/developer/download_50/qt-everywhere-commercial-src-5.0.0.tar.gz

Qt 4.8.4 for Windows (Src and VS2008 32bit Binary)

http://dist.qt.digia.com/developer/download/qt-win-commercial-4.8.4-vs2008.exe

Qt Visual Studio Addin 1.1.11 for Qt4 (Binary)

http://dist.qt.digia.com/developer/download/qt-vs-addin-1.1.11.exe

Qt 4.8.4 for Windows (Src and VS2010 32bit Binary)

http://dist.qt.digia.com/developer/download/qt-win-commercial-4.8.4-vs2010.exe

Qt 4.8.4 for Windows (Src and MinGW 32bit Binary)

http://dist.qt.digia.com/developer/download/qt-win-commercial-4.8.4-mingw.exe

Qt Creator 2.6.1 for Windows (32bit Binary)

http://dist.qt.digia.com/developer/download_creator/qt-creator-win-commercial-2.6.1.exe

Qt 4.8.4 for Windows (Src and VS2010 64bit Binary)

http://dist.qt.digia.com/developer/download/qt-win-commercial-4.8.4-vs2010-64bit.exe

Qt Commercial Charts 1.1.0

http://dist.qt.digia.com/developer/download_charts/qt-commercial-charts-src-1.1.0.zip

Qt 5.0.0 Sources

http://dist.qt.digia.com/developer/download_50/qt-everywhere-commercial-src-5.0.0.zip

Qt 5.0.0 Offline SDK for Windows (32-bit Binary)

http://dist.qt.digia.com/developer/download_50/qt-windows-commercial-5.0.0-msvc2010_32-x86-offline.exe

Qt Visual Studio Addin 1.2.0 for Qt5 (Binary)

http://dist.qt.digia.com/developer/download_50/qt-vs-addin-1.2.0.exe

2、QT-project網站下載 http://qt-project.org/downloads

這個網站就直接而且清晰多了,分類很清楚,如下圖所示。


需要說明的是Qt libraries 4.8.4 for embedded Linux (236 MB) 下載鏈接和Qt libraries 4.8.4 for Linux/X11 (225 MB) 其實下載的是一個東西,反正我看到的鏈接地址都是一樣的,這也就是Linux下我們需要用的。另外從這個頁面再下載開發所用的IDE QT Creator,這是一個bin文件,可以直接安裝。

QT的編譯與安裝


1、拷貝文件到自己想要的文件夾,解壓:

tar zxvf qt-everywhere-opensource-src-4.8.4.tar.gz

2、進入解壓後文件目錄,執行配置

root@emouse:~/study# cd qt-everywhere-opensource-src-4.8.4/

root@emouse:~/study/qt-everywhere-opensource-src-4.8.4# ./configure

此處也可以加上配置前綴./configure -prefix /opt/qt-4.8.4-x11

這樣的話再後面編譯過程中的文件會複製到/opt/qt-4.8.4-x11 這個目錄下(目錄不存在會自動創建),好處是源碼便於管理,

關於這部分前綴配置可以我在config文件中找到這部分代碼,可以參考,可以從這段代碼中看出相應的配置以及沒有配置的情況下默認安裝目錄,代碼位於3594行,如下:

複製代碼
#-------------------------------------------------------------------------------
# post process QT_INSTALL_* variables
#-------------------------------------------------------------------------------

#prefix
if [ -z "$QT_INSTALL_PREFIX" ]; then
    if [ "$CFG_DEV" = "yes" ]; then
        QT_INSTALL_PREFIX="$outpath" # In Development, we use sandboxed builds by default
    elif [ "$PLATFORM_QWS" = "yes" -o "$PLATFORM_QPA" = "yes" ]; then
        if [ "$PLATFORM_QPA" = "yes" ]; then
    QT_INSTALL_PREFIX="/usr/local/Trolltech/QtLighthouse-${QT_VERSION}"
        else
    QT_INSTALL_PREFIX="/usr/local/Trolltech/QtEmbedded-${QT_VERSION}"
        fi
        if [ "$PLATFORM" != "$XPLATFORM" ]; then
            QT_INSTALL_PREFIX="${QT_INSTALL_PREFIX}-${CFG_ARCH}"
        fi
    elif [ -d "$EPOCROOT" ] && [ "$XPLATFORM_SYMBIAN" = "yes" ]; then
        if [ "$XPLATFORM_SYMBIAN" = "yes" ]; then
            QT_INSTALL_PREFIX="$EPOCROOT/epoc32/"
            QT_INSTALL_LIBS="$EPOCROOT/epoc32/release/armv5/lib/"
        fi
    else
        QT_INSTALL_PREFIX="/usr/local/Trolltech/Qt-${QT_VERSION}" # the default install prefix is /usr/local/Trolltech/Qt-$QT_VERSION
複製代碼

配置過程中會有以下提示:

Which edition of Qt do you want to use ?

Type 'c' if you want to use the Commercial Edition.
Type 'o' if you want to use the Open Source Edition.

當然選擇o啦。繼續提示:

複製代碼
This is the  Open Source Edition.

You are licensed to use this software under the terms of
the Lesser GNU General Public License (LGPL) versions 2.1.
You are also licensed to use this software under the terms of
the GNU General Public License (GPL) versions 3.

Type '3' to view the GNU General Public License version 3.
Type 'L' to view the Lesser GNU General Public License version 2.1.
Type 'yes' to accept this license offer.
Type 'no' to decline this license offer.

Do you accept the terms of either license?
複製代碼

輸入yes,回車即可。接下來就會生成makefile文件,配置過程十來分鐘左右。配置完畢後執行make。

接下來可以吃個飯或者睡一覺,編譯時間大概3個小時左右,比較漫長,耐心等待吧。

經過漫長的等待之後,執行sudo make install來安裝,會安裝到/usr/local/Trolltech/Qt-4.8.4目錄下。安裝時間比較短,大概需要幾分鐘左右,至此,安裝完成。

小測試一下,運行 /usr/local/Trolltech/Qt-4.8.4/bin/designer ,看見 qt 啓動,如下圖所示,就說明QT已經安裝成功。


其他配置


qmake這個命令需要進行環境變量的配置才能找到的到,我看了下我的系統裏已經包含了這個命令了,只是路徑不一樣。測試如下:

root@emouse:/# qmake -v
Qmake version: 1.07a (Qt 3.3.8b)
Qmake is free software from Trolltech ASA.
root@emouse:/# which qmake
/usr/bin/qmake

在看一下qmake的屬性


從這個地方可以看出qmake這個命令別鏈接到了/etc/alternatives/qmake,還是不清楚是啥地方來的命令,繼續看!找到/etc/alternatives/qmake,在看一下屬性就清楚了,可以看到這個地方又鏈接回去了,那麼如果還使用qmake命令的話實際使用的是qmake-qt3,這樣就會出現錯誤。


也可以使用以下命令快速查看:

root@emouse:/# ls /usr/bin/qmake -ln
lrwxrwxrwx 1 0 0 23 12月 14 21:06 /usr/bin/qmake -> /etc/alternatives/qmake
root@emouse:/# ls /etc/alternatives/qmake -ln
lrwxrwxrwx 1 0 0 18 12月 14 21:06 /etc/alternatives/qmake -> /usr/bin/qmake-qt3

通過以上的分析可以知道,解決這個問題有兩種方法,一個就是更改qmake的鏈接,使得qmake這個命令鏈接到安裝目錄下我們安裝版本的qmake,還有一種方法就是改環境變量。

先說更改鏈接的方法,通過上面的介紹,這個其實操作就比較直接和簡單了,命令和實驗結果如下:

root@emouse:/# rm /etc/alternatives/qmake 
root@emouse:/# ln -s /usr/local/Trolltech/Qt-4.8.4/bin/qmake /etc/alternatives/qmake
root@emouse:/# qmake -v
QMake version 2.01a
Using Qt version 4.8.4 in /usr/local/Trolltech/Qt-4.8.4/lib

可以看到,qmake已經成功更改爲4.8.4版本。

第二種辦法就是更改環境變量,這個我沒有嘗試,有興趣的自己驗證。

vi /home/defonds/.bashrc 用來修改用戶環境變量,在文件後面添加以下信息

export QTDIR=/usr/local/Trolltech/Qt-4.8.4 
export PATH=$QTDIR/bin:$PATH  
export MANPATH=$QTDIR/man:$MANPATH  
export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH

再修改所有用戶環境變量vi /etc/profile 同樣在文件後添加上面的環境變量信息。這種方法應該同樣可行。

Qt-creator的安裝與配置


根據上面提供的網址下載qt creator在linux下的安裝文件qt-creator-linux-x86-opensource-2.6.1.bin,下面的安裝就比較簡單了,首先更改文件屬性,使它可執行。

chmod u+x qt-creator-linux-x86-opensource-2.6.1.bin

然後運行sudo ./qt-creator-linux-x86-opensource-2.6.1.bin

接下來就是非常熟悉的安裝界面了。


安裝完成即可。接下來就可以使用這個IDE來進行開發了,這樣的界面是不是在linux下很難得呢?


初步的安裝就到這裏,下次繼續寫關於嵌入式開發平臺的構建。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章