arm-linux交叉編譯工具鏈的制作(基於S3C2440)【轉】


本文轉載自:http://eric-gao.iteye.com/blog/2160622

制作arm-linux交叉編譯工具鏈一般通過crosstool工具或者crosstool-NG,前者使用方便,但是制作會受到一些限制,使用crosstool最多只能編譯gcc4.1.1、glibc2.x的版本,無法編譯版本高於2.6.29的linux內核。crosstool-NG是新的用來建立交叉編譯工具鏈的工具,它是crosstool的替代者。crosstool-NG有更好的定制性,並且一直保持着更新,對新版本的編譯工具鏈的支持比較好,當然也帶來了一些麻煩,它並不是下載下來就可以使用的,必須先配置安裝。我們這里選用crosstool-NG來制作編譯工具鏈。

 

本文使用的系統是Ubuntu Kylin(3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux),建議都使用Ubuntu系統,安裝軟件比較方便。

 

一、安裝crosstool-NG

1、建立arm-linux-tools目錄,並進入該目錄

Shell代碼   收藏代碼
  1. $ mkdir arm-linux-tools  
  2. $ cd arm-linux-tools  

 

2、獲取crosstool-NG的源碼

Shell代碼   收藏代碼
  1. $ wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.20.0.tar.bz2  

 

3、解壓crosstool-NG的源碼

Shell代碼   收藏代碼
  1. $ tar xvjf crosstool-ng-1.20.0.tar.bz2  

考慮到后續將要使用到的各種目錄,在這里先建立好后續所需的目錄。

Shell代碼   收藏代碼
  1. $ mkdir crosstool-build crosstool-install src  

 

4、配置並進行依賴檢測

Shell代碼   收藏代碼
  1. $ cd crosstool-ng-1.20.0  
  2. $ ./configure --prefix /home/eric/arm-linux-tools/crosstool-install/  

 

在配置之前,需要確保以下軟件已經安裝

1)gperf

2)bison

3)flex

4)texinfo

5)gawk

6)libtool

7)automake

8)libncurses5-dev

9)g++

 

在進行配置時,會進行依賴檢測,如果存在包缺失的現象,安裝好之后再執行配置即可。

成功配置之后會自動創建我們需要的Makefile文件。

 

5、編譯並安裝

Shell代碼   收藏代碼
  1. $ make  
  2. $ make install  

 

6、配置環境變量

Shell代碼   收藏代碼
  1. $ export PATH=$PATH:/home/eric/arm-linux-tools/crosstool-install/bin/  

 

7、檢查crosstool-NG是否安裝成功

Shell代碼   收藏代碼
  1. $ ct-ng -v  


 

二、配置交叉編譯工具鏈

在crosstool-NG中有很多已經做好的默認配置(位於crosstool-ng-1.20.0/samples目錄下),這里只需要進行修改就可以了。因為是制作arm-linux的交叉編譯工具鏈,所以選擇arm-unknown-linux-gnueabi進行配置。

 

1、將arm-unknown-linux-gnueabi文件夾復制到crosstool-build目錄下

Shell代碼   收藏代碼
  1. $ cd samples  
  2. $ cp -r arm-unknown-linux-gnueabi/ ../../crosstool-build/   

 

2、將默認配置文件拷貝到crosstool-build目錄下並改名為.config

Shell代碼   收藏代碼
  1. $ cd ~/arm-linux-tools/crosstool-build  
  2. $ cp arm-unknown-linux-gnueabi/crosstool.config .config  

 

3、執行ct-ng menuconfig進入配置界面進行配置

Shell代碼   收藏代碼
  1. $ ct-ng menuconfig  

配置界面如下圖所示:

 

(1)設定源碼包路徑和交叉編譯器的安裝路徑

1)源碼包路徑:設置Paths and misc options下的Local tarballs directory為/home/eric/arm-linux-tools/src

2)交叉編譯器的安裝路徑:設置Paths and misc options下的Prefix directory為/home/eric/arm-linux-tools/x-tools/${CT_TARGET}

 

(2)修改交叉編譯器針對的架構

1)設置Target options下的Architecture level為armv4t

2)設置Target options下的Emit assembly for CPU為arm9tdmi

3)設置Target options下的Tune for CPU為arm920t

 

以上這幾個參數是如何得出來的,可以參考gcc的man手冊。你可以在下載的gcc-4.9.1.tar.bz2解壓后的文件夾中找到(gcc-4.9.1/gcc/doc/gcc.1)

執行以下命令打開gcc.1

Shell代碼   收藏代碼
  1. $ man ./gcc.1  

你可以在其中看到下面這段:

Shell代碼   收藏代碼
  1. ......  
  2.  ARM Options  
  3. ......  
  4. -march=name  
  5.            This specifies the name of the target ARM architecture.  GCC uses  
  6.            this name to determine what kind of instructions it can emit when  
  7.            generating assembly code.  This option can be used in conjunction  
  8.            with or instead of the -mcpu= option.  Permissible names are:  
  9.            armv2, armv2a, armv3, armv3m, armv4, armv4t, armv5, armv5t, armv5e,  
  10.            armv5te, armv6, armv6j, armv6t2, armv6z, armv6zk, armv6-m, armv7,  
  11.            armv7-a, armv7-r, armv7-m, armv7e-m, armv7ve, armv8-a, armv8-a+crc,  
  12.            iwmmxt, iwmmxt2, ep9312.  
  13.                 
  14. -mtune=name  
  15.            This option specifies the name of the target ARM processor for  
  16.            which GCC should tune the performance of the code.  For some ARM  
  17.            implementations better performance can be obtained by using this  
  18.            option.  Permissible names are: arm2, arm250, arm3, arm6, arm60,  
  19.            arm600, arm610, arm620, arm7, arm7m, arm7d, arm7dm, arm7di,  
  20.            arm7dmi, arm70, arm700, arm700i, arm710, arm710c, arm7100, arm720,  
  21.            arm7500, arm7500fe, arm7tdmi, arm7tdmi-s, arm710t, arm720t,  
  22.            arm740t, strongarm, strongarm110, strongarm1100, strongarm1110,  
  23.            arm8, arm810, arm9, arm9e, arm920, arm920t, arm922t, arm946e-s,  
  24.            arm966e-s, arm968e-s, arm926ej-s, arm940t, arm9tdmi, arm10tdmi,  
  25.            arm1020t, arm1026ej-s, arm10e, arm1020e, arm1022e, arm1136j-s,  
  26.            arm1136jf-s, mpcore, mpcorenovfp, arm1156t2-s, arm1156t2f-s,  
  27.            arm1176jz-s, arm1176jzf-s, cortex-a5, cortex-a7, cortex-a8,  
  28.            cortex-a9, cortex-a12, cortex-a15, cortex-a53, cortex-a57,  
  29.            cortex-r4, cortex-r4f, cortex-r5, cortex-r7, cortex-m4, cortex-m3,  
  30.            cortex-m1, cortex-m0, cortex-m0plus, marvell-pj4, xscale, iwmmxt,  
  31.            iwmmxt2, ep9312, fa526, fa626, fa606te, fa626te, fmp626, fa726te.  
  32.              
  33.            Additionally, this option can specify that GCC should tune the  
  34.            performance of the code for a big.LITTLE system.  Permissible names  
  35.            are: cortex-a15.cortex-a7, cortex-a57.cortex-a53.  
  36.                 
  37. -mcpu=name  
  38.            This specifies the name of the target ARM processor.  GCC uses this  
  39.            name to derive the name of the target ARM architecture (as if  
  40.            specified by -march) and the ARM processor type for which to tune  
  41.            for performance (as if specified by -mtune).  Where this option is  
  42.            used in conjunction with -march or -mtune, those options take  
  43.            precedence over the appropriate part of this option.  
  44.   
  45.            Permissible names for this option are the same as those for -mtune.  
  46.   
  47.            -mcpu=generic-arch is also permissible, and is equivalent to  
  48.            -march=arch -mtune=generic-arch.  See -mtune for more information.  
  49.   
  50.            -mcpu=native causes the compiler to auto-detect the CPU of the  
  51.            build computer.  At present, this feature is only supported on  
  52.            Linux, and not all architectures are recognized.  If the auto-  
  53.            detect is unsuccessful the option has no effect.  
  54. ......  

-march=name對應Architecture level

-mtune=name對應Tune for CPU

-mcpu=name對應Emit assembly for CPU

 

(3)設置編譯時的並行進程數

1)設置Paths and misc options下的Number of parallel jobs為4

這個數值不宜過大,應該為CPU核心數量的兩倍。

 

(4)設置編譯器前綴

1)設置Toolchain options下的Tuple's vendor string為S3C2440,這樣生成的編譯器的前綴就是arm-S3C2440-linux-gnueabi-

 

4、安裝termcap

Shell代碼   收藏代碼
  1. $ cd /tmp  
  2. $ wget ftp://ftp.gnu.org/gnu/termcap/termcap-1.3.1.tar.gz  
  3. $ tar xvzf termcap-1.3.1.tar.gz  
  4. $ cd termcap-1.3.1  
  5. $ ./configure --prefix=/usr  
  6. $ make  
  7. $ make install  

 

5、編譯

在crosstool-build目錄下執行以下命令進行編譯

Shell代碼   收藏代碼
  1. $ ct-ng build.4  

 

整個編譯過程時間比較長,crosstool-ng會自動下載相應的包和內核文件,然后解壓、配置、編譯、鏈接,但有個別包在自動下載過程中會出錯,例如duma_2_5_15和expat-2.1.0,需要手動下載這兩個包放入Local tarballs directory指定的目錄(這里為/home/eric/arm-linux-tools/src),然后再重新執行編譯即可。

 

編譯過程中,在安裝到cross-gdb時,出現configure: error: expat is missing or unusable的錯誤,安裝libexpat1-dev即可。

編譯過程中,出現g++: Internal error: Killed (program cc1plus)的錯誤,嘗試着將可用內存加大。

 

如下圖所示,代表安裝成功了。

 

6、配置環境變量

Shell代碼   收藏代碼
  1. $ echo "PATH=$PATH:/home/eric/arm-linux-tools/x-tools/arm-S3C2440-linux-gnueabi/bin" >> ~/.bashrc  
  2. $ source ~/.bashrc  

 

7、確認安裝完成

Shell代碼   收藏代碼
  1. $ arm-S3C2440-linux-gnueabi-gcc -v  

 

8、編譯成功后的交叉編譯器路徑

(1)編譯器路徑:/home/eric/arm-linux-tools/x-tools/arm-S3C2440-linux-gnueabi/bin

(2)庫文件路徑:/home/eric/arm-linux-tools/x-tools/arm-S3C2440-linux-gnueabi/arm-S3C2440-linux-gnueabi/lib

 

三、編譯測試

1、寫一個簡單的C程序:Hello, World

C代碼   收藏代碼
  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4.     printf("Hello, World\n");  
  5.   
  6.     return 0;  
  7. }  

 

2、編譯

Shell代碼   收藏代碼
  1. $ arm-S3C2440-linux-gnueabi-gcc -o hello hello.c  

 

3、上傳至目標板並執行

Shell代碼   收藏代碼
  1. $ chmod +x ./hello  
  2. $ ./hello  

 

如上圖所示,創建的交叉編譯鏈可以成功編譯成目標板能夠識別的指令。

 

關於crosstool-ng的詳細介紹和使用,請參考:http://www.crifan.com/files/doc/docbook/crosstool_ng/release/htmls/index.html

需要的依賴包下載:http://download.csdn.net/detail/jsntghf/8198753

編譯后的交叉編譯工具鏈下載:http://download.csdn.net/detail/jsntghf/8206693


免責聲明!

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



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