本文轉載自: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目錄,並進入該目錄
- $ mkdir arm-linux-tools
- $ cd arm-linux-tools
2、獲取crosstool-NG的源碼
- $ wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.20.0.tar.bz2
3、解壓crosstool-NG的源碼
- $ tar xvjf crosstool-ng-1.20.0.tar.bz2
考慮到后續將要使用到的各種目錄,在這里先建立好后續所需的目錄。
- $ mkdir crosstool-build crosstool-install src
4、配置並進行依賴檢測
- $ cd crosstool-ng-1.20.0
- $ ./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、編譯並安裝
- $ make
- $ make install
6、配置環境變量
- $ export PATH=$PATH:/home/eric/arm-linux-tools/crosstool-install/bin/
7、檢查crosstool-NG是否安裝成功
- $ ct-ng -v
二、配置交叉編譯工具鏈
在crosstool-NG中有很多已經做好的默認配置(位於crosstool-ng-1.20.0/samples目錄下),這里只需要進行修改就可以了。因為是制作arm-linux的交叉編譯工具鏈,所以選擇arm-unknown-linux-gnueabi進行配置。
1、將arm-unknown-linux-gnueabi文件夾復制到crosstool-build目錄下
- $ cd samples
- $ cp -r arm-unknown-linux-gnueabi/ ../../crosstool-build/
2、將默認配置文件拷貝到crosstool-build目錄下並改名為.config
- $ cd ~/arm-linux-tools/crosstool-build
- $ cp arm-unknown-linux-gnueabi/crosstool.config .config
3、執行ct-ng menuconfig進入配置界面進行配置
- $ 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
- $ man ./gcc.1
你可以在其中看到下面這段:
- ......
- ARM Options
- ......
- -march=name
- This specifies the name of the target ARM architecture. GCC uses
- this name to determine what kind of instructions it can emit when
- generating assembly code. This option can be used in conjunction
- with or instead of the -mcpu= option. Permissible names are:
- armv2, armv2a, armv3, armv3m, armv4, armv4t, armv5, armv5t, armv5e,
- armv5te, armv6, armv6j, armv6t2, armv6z, armv6zk, armv6-m, armv7,
- armv7-a, armv7-r, armv7-m, armv7e-m, armv7ve, armv8-a, armv8-a+crc,
- iwmmxt, iwmmxt2, ep9312.
- -mtune=name
- This option specifies the name of the target ARM processor for
- which GCC should tune the performance of the code. For some ARM
- implementations better performance can be obtained by using this
- option. Permissible names are: arm2, arm250, arm3, arm6, arm60,
- arm600, arm610, arm620, arm7, arm7m, arm7d, arm7dm, arm7di,
- arm7dmi, arm70, arm700, arm700i, arm710, arm710c, arm7100, arm720,
- arm7500, arm7500fe, arm7tdmi, arm7tdmi-s, arm710t, arm720t,
- arm740t, strongarm, strongarm110, strongarm1100, strongarm1110,
- arm8, arm810, arm9, arm9e, arm920, arm920t, arm922t, arm946e-s,
- arm966e-s, arm968e-s, arm926ej-s, arm940t, arm9tdmi, arm10tdmi,
- arm1020t, arm1026ej-s, arm10e, arm1020e, arm1022e, arm1136j-s,
- arm1136jf-s, mpcore, mpcorenovfp, arm1156t2-s, arm1156t2f-s,
- arm1176jz-s, arm1176jzf-s, cortex-a5, cortex-a7, cortex-a8,
- cortex-a9, cortex-a12, cortex-a15, cortex-a53, cortex-a57,
- cortex-r4, cortex-r4f, cortex-r5, cortex-r7, cortex-m4, cortex-m3,
- cortex-m1, cortex-m0, cortex-m0plus, marvell-pj4, xscale, iwmmxt,
- iwmmxt2, ep9312, fa526, fa626, fa606te, fa626te, fmp626, fa726te.
- Additionally, this option can specify that GCC should tune the
- performance of the code for a big.LITTLE system. Permissible names
- are: cortex-a15.cortex-a7, cortex-a57.cortex-a53.
- -mcpu=name
- This specifies the name of the target ARM processor. GCC uses this
- name to derive the name of the target ARM architecture (as if
- specified by -march) and the ARM processor type for which to tune
- for performance (as if specified by -mtune). Where this option is
- used in conjunction with -march or -mtune, those options take
- precedence over the appropriate part of this option.
- Permissible names for this option are the same as those for -mtune.
- -mcpu=generic-arch is also permissible, and is equivalent to
- -march=arch -mtune=generic-arch. See -mtune for more information.
- -mcpu=native causes the compiler to auto-detect the CPU of the
- build computer. At present, this feature is only supported on
- Linux, and not all architectures are recognized. If the auto-
- detect is unsuccessful the option has no effect.
- ......
-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
- $ cd /tmp
- $ wget ftp://ftp.gnu.org/gnu/termcap/termcap-1.3.1.tar.gz
- $ tar xvzf termcap-1.3.1.tar.gz
- $ cd termcap-1.3.1
- $ ./configure --prefix=/usr
- $ make
- $ make install
5、編譯
在crosstool-build目錄下執行以下命令進行編譯
- $ 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、配置環境變量
- $ echo "PATH=$PATH:/home/eric/arm-linux-tools/x-tools/arm-S3C2440-linux-gnueabi/bin" >> ~/.bashrc
- $ source ~/.bashrc
7、確認安裝完成
- $ 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
- #include <stdio.h>
- int main() {
- printf("Hello, World\n");
- return 0;
- }
2、編譯
- $ arm-S3C2440-linux-gnueabi-gcc -o hello hello.c
3、上傳至目標板並執行
- $ chmod +x ./hello
- $ ./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