1. 程式人生 > >Ubuntu 16.04下Qt交叉編譯開發環境搭建

Ubuntu 16.04下Qt交叉編譯開發環境搭建

轉載地址:http://www.linuxidc.com/Linux/2017-03/141552.htm

一、環境及軟體包介紹:

(一)系統環境

(二)軟體包 

1、arm-Linux-gcc.tar.gz 

我提供的包是arm-linux-gcc4.4.3版本,arm-linux-gcc是編譯arm開發板上程式用的一種gcc。

2、qt-everywhere-opensource-src-4.8.5.tar.gz 

這個包是QT4.8.5的原始碼包,everywhere意思就是可以編譯出適合各種平臺的版本。

3、qt-embedded-linux-opensource-src-4.5.0.tar.bz2

這是qt4.5.0用於編譯嵌入式的原始碼包。

4、tslib-1.4.tar.gz

tslib一種觸控式螢幕校準驅動,用觸控式螢幕操作的板子都需要這個,需要用觸控式螢幕操作的軟體自然也需要這裡面的庫。

5、qt-creator-opensource-linux-x86_64-4.1.0.run

qt-creator是一款經常與qt配合使用的IDE,這是目前的最新版。

二、目錄約定及準備工作:

(一)目錄約定:
 交叉編譯器路徑:/usr/local/arm-linux-gcc/bin/
原始碼包存放路徑:/home/lhc/Qt/src/      (下載的所有包都放到這個目錄)
 安裝輸出目錄:/home/lhc/Qt/output/
將qt-everywhere-opensource-src-4.8.5.tar.gz  解壓兩次,分別命名為qt-x11、qt-embedded

將qt-embedded-linux-opensource-src-4.5.0.tar.bz2解壓一次,命名為qt-arm

將arm-linux-gcc.tar.gz和tslib-1.4.tar.gz直接解壓。

(二)準備工作:

1、提前安裝各種軟體及依賴庫,避免後面碰到錯誤再安裝麻煩

sudo apt-get install g++-multilib libx11-dev libxext-dev libxtst-dev zlib1g-dev lib32ncurses5 lib32z1 libpng-dev autoconf automake libtool

三、編譯安裝:

(一)arm交叉編譯環境搭建
1、解壓arm-linux-gcc.tar.gz,移動到/usr/local目錄下

$tar vfzx arm-linux-gcc.tar.gz

$sudo mv arm-linux-gcc /usr/local

2、新增使用者環境變數,編輯~/.profile,在後面加上export PATH=/usr/local/arm-linux-gcc/bin/:$PATH,這只是新增到當前使用者的環境變數中,如果切換了使用者就沒有這個環境變量了,如果想所有使用者都有這個環境變數應該是新增到/etc/profile檔案中。

$echo "export PATH=/usr/local/arm-linux-gcc/bin/:$PATH" >> ~/.profile

3、測試,source ~/.profile使環境變數在當前終端立即生效,重啟後會在當前使用者所有終端生效。arm-linux-gcc -v會看到當前arm-gcc的版本。
 錯誤:$ arm-linux-gcc -v
 /usr/local/arm-linux-gcc/bin/arm-linux-gcc: 15: exec: /usr/local/arm-linux-gcc/bin/.arm-none-linux-gnueabi-gcc: not found
解決方法:$sudo apt-get install g++-multilib

重啟一下,再往下進行

(二)Qt及各種工具編譯安裝

1、x11版本的編譯

首先配置,命令為:
  ./configure -prefix /home/lhc/Qt/output/qt-x11
  然後輸入“o”,在然後輸入“yes”,下面的embedded版本和arm版本配置時也是一樣。
  執行後會出現錯誤:

  出錯:Basic XLib functionality test failed!
  解決方法:sudo apt-get install libx11-dev libxext-dev libxtst-dev

  然後:  make && make install
  出錯:error: ‘insert’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation
  解決辦法:按照note提示,將./tools/porting/src/codemodel.h中的insert改為this->insert,重新編譯


  編譯會持續較長時間,取決與你電腦的配置高低,這裡有個竅門,假如你PC機的CPU是雙核的話,將make指令加上 -j3引數,會進行多執行緒編譯,編譯速度會大大提高,即make -j3,這時會使用兩個核心同時編譯,大家可以試試;編譯完成後qt的x11版本會被安裝在/home/lhc/Qt/output/qt-x11目錄;
  這時還沒與完,關鍵的qvfb程式還沒有被編譯,所以繼續:
  安裝qvfb
  cd tools/qvfb
  make

錯誤:GD5Ev]+0x2ae): undefined reference to `png_write_chunk'
 qanimationwriter.cpp:(.text._ZN19QAnimationWriterMNGD0Ev[_ZN19QAnimationWriterMNGD5Ev]+0x2cc): undefined reference to `png_set_filler'
 collect2: error: ld returned 1 exit status
 Makefile:170: recipe for target '../../bin/qvfb' failed
解決方案:根據http://www.linuxidc.com/Linux/2014-02/97344.htm得知解決辦法為
#ln -s /lib/x86_64-linux-gnu/libpng12.so.0 /lib/x86_64-linux-gnu/libpng.so
修改Makefile檔案,#gedit Makefile(或者 #vi Makefile),在LIBS後面新增-L/lib/x86_64-linux-gnu -lpng這兩項
 重新make
然後將在/home/lhc/Qt/src/qt-x11/bin目錄生成的qvfb程式,將它複製到電腦的/usr/sbin目錄,以後可以直接在終端執行了。

2、embedded版本的編譯
 (1)配置:
./configure -no-largefile -no-accessibility -no-qt3support -no-phonon -no-svg -no-nis -no-cups -qvfb -prefix ~/Qt/output/qt-embedded
(2)配置完成後:
 make -j3
  make install
  一般不會出現任何錯誤的。
Makefile:559: recipe for target 'sub-examples-make_default-ordered' failed
 make: *** [sub-examples-make_default-ordered] Error 2

最後有個錯誤,暫時忽略

3、tslib的編譯
 (1)首先:
  export PATH=/usr/local/arm-linux-gcc/bin/:$PATH
  export PREFIX=/home/lhc/Qt/output/tslib
  export CC=/usr/local/arm-linux-gcc/bin/arm-linux-gcc

(2)./etc/ts.conf配置(當前目錄為tslib的根目錄)

將module_raw input前面的#去掉,注意module_raw input前面不要留空格,否則會段錯誤。
# Uncomment if you wish to use the linux input layer event interface
 module_raw input

(3)配置
 ./autogen.sh
出現錯誤:./autogen.sh: 4: autoreconf: not found
解決方法:
sudo apt-get install autoconf automake libtool

然後重新:./autogen.sh

然後:
echo "ac_cv_func_malloc_0_nonnull=yes" > arm-linux.cache
 ./configure --host=arm-linux --prefix=/home/lhc/Qt/output/tslib --cache-file=arm-linux.cache
錯誤:checking for arm-linux-g++... arm-linux-g++
 checking whether the C++ compiler works... no
 configure: error: in `/home/lhc/Qt/src/tslib':
 configure: error: C++ compiler cannot create executables
 See `config.log' for more details
我們仔細檢視config.log從中發現檢測g++時發現缺少了依賴庫libraries: libz.so.1,查閱apt找到安裝這個依賴庫的方法在下面。
 解決方案:sudo apt-get install lib32z1
若安裝提示不成功,並列出了替代的包,那就按照要求安裝替代包。

配置完成後:make && make install

4、arm版本的編譯
 (1)首先配置:

./configure -embedded arm -xplatform qws/linux-arm-g++ -depths 4,8,12,16 -no-qt3support -no-qvfb -qt-mouse-tslib -prefix /home/lhc/Qt/output/qt-arm/ -qt-sql-sqlite -I/home/lhc/Qt/output/tslib/include -L/home/lhc/Qt/output/tslib/lib -no-rpath -no-largefile

(2)然後:make

出現錯誤:../../corelib/tools/qbytearray.cpp:54: fatal error: zlib.h: 沒有那個檔案或目錄

解決辦法:sudo apt-get install zlib1g-dev

然後重新:make -j3

編譯完成之後make install

5、新增環境變數

(1)到/home/lhc/Qt/output/qt-arm/bin下

將qmake複製為qmake-arm

cp qmake qmake-arm

(2)新增環境變數

$gedit ~/.profile

新增

export PATH=/home/lhc/Qt/output/qt-arm/bin:$PATH

export PATH=/home/lhc/Qt/output/qt-embedded/bin:$PATH

重啟生效

之後qmake && make就可以生成桌面程式

qmake-arm && make就可以生成arm開發板上執行的程式

四、測試

(一)安裝一個qtcreator

(二)用qt建立一個帶介面的工程

(三)在工程目錄下開啟終端,qmake-arm,然後make,生成的程式就可以放到開發板上執行測試了。

各個版本的包的下載地址:Qt GCC