三.下载并安装croostool-ng
crosstool-ng的下载地址是:http://ymorin.is-a-geek.org/download/crosstool-ng
值得注意的是,下载里最新的crosstool-ng以后,记得在到http://ymorin.is-a-geek.org/download/crosstool-ng/01-fixes/ 看看有没有相应的补丁,有得话一起下载下来。
本次下载的版本是1.8.2,无 patch。
安装方法如下:
$ mkdir ~/croosstool-ng # 在当前用户目录下建立croosstool-ng目录
$ cd ~/croosstool-ng
$ mv /tmp/crosstool-ng-1.8.2.tar.bz2 ./ # 把下载的crosstool-ng软件包放到 crosstool-ng目录中
$ tar -jxvf crosstool-ng-1.8.2.tar.bz2 # 解包
$ cd crosstool-ng-1.8.2 # 进入解压目录
$ patch -p1 < <补丁文件> # 给crosstool-ng打补丁(如果有)
$ ./configure --prefix=/opt/crosstool-ng # 配置crosstool-ng
$ make # 编译crosstool-ng
$ sudo make install # 安装crosstool-ng
$ echo "PATH=$PATH:/opt/crosstool-ng/bin" >> ~/.bashrc # 为后面调用ct-ng命令增加环境变量
$ source ~/.bashrc
$ ct-ng help
This is crosstool-NG version 1.8.2
Copyright (C) 2008 Yann E. MORIN <[email protected]>
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
……
四.配置crosstool-ng
$ cd ~/croosstool-ng
$ mkdir crosstool-ng-1.8.2_build # 建立一个交叉工具链的输出目录
$ mkdir src # 建立保存源码包目录
$ mkdir x-tools # 建立交叉编译器的安装路径
$ cd crosstool-ng-1.8.2_build/
$ cp /opt/crosstool-ng/lib/ct-ng-1.8.2/samples/arm-unknown-linux-gnueabi/crosstool.config .config
$ ct-ng menuconfig
此时会出现crosstool-ng的配置窗口,针对不同架构的ARM芯片对其进行修改配置:
1、已下载好的源码包路径和交叉编译器的安装路径。
Paths and misc options --->
(${HOME}/crosstool-ng/src) Local tarballs directory 保存源码包路径
(${HOME}/crosstool-ng/x-tools/${CT_TARGET}) Prefix directory 交叉编译器的安装路径
2、修改交叉编译器针对的构架
Target options --->
*** Target optimisations ***
(armv5te) Architecture level
(arm926ej-s) Emit assembly for CPU
(arm926ej-s) Tune for CPU
我使用的芯片是Atmel公司的AT91SAM9263,它属于armv5te体系架构,CPU属于arm926ej-s。tune貌似可以不填。。。
其他芯片可以用man gcc来查询,搜索arm,再搜索-march=就可以找到本gcc支持的处理器核心列表了。
例如:
......
ARM Options
......
-mcpu=name (Emit assembly for CPU)
This specifies the name of the target ARM processor. GCC uses this
name to determine what kind of instructions it can emit when gener‐
ating assembly code. Permissible names are: arm2, arm250, arm3,
arm6, arm60, arm600, arm610, arm620, arm7, arm7m, arm7d, arm7dm,
arm7di, arm7dmi, arm70, arm700, arm700i, arm710, arm710c, arm7100,
arm7500, arm7500fe, arm7tdmi, arm7tdmi-s, arm8, strongarm, stron‐
garm110, strongarm1100, 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, arm1176jz-s, arm1176jzf-s, cortex-a8, cortex-r4, cor‐
tex-m3, xscale, iwmmxt, ep9312.
-mtune=name (Tune for CPU)
This option is very similar to the -mcpu= option, except that
instead of specifying the actual target processor type, and hence
restricting which instructions can be used, it specifies that GCC
should tune the performance of the code as if the target were of
the type specified in this option, but still choosing the instruc‐
tions that it will generate based on the cpu specified by a -mcpu=
option. For some ARM implementations better performance can be
obtained by using this option.
-march=name (Architecture level)
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,
armv5te, armv6, armv6j, armv6t2, armv6z, armv6zk, armv7, armv7-a,
armv7-r, armv7-m, iwmmxt, ep9312.
......
3、增加编译时的并行进程数,以增加运行效率,加快编译。
Paths and misc options --->
*** Build behavior ***
(4) Number of parallel jobs
这个数值不宜过大,应该为CPU数量的两倍。由于我的CPU是双核的,所以我填了4.
4、一些个性化的修改(可以不修改)
Toolchain options --->
*** Tuple completion and aliasing ***
(efronc) Tuple's vendor string
这样产生的编译器前缀就是:arm-efronc-linux-gnueabi-
5、C compiler --->
(crosstool-NG-${CT_VERSION}-efronc) gcc ID string
以上红色字体部分即为修改之处。
五.下载源文件
尽管crosstool-ng在编译时会自动下载,但速度较慢,最好自行下载。源文件列表如下:
linux-2.6.33.2.tar.bz2
binutils-2.19.1.tar.bz2
gcc-4.3.2.tar.bz2
glibc-2.9.tar.gz
dmalloc-5.5.2.tgz
duma_2_5_15.tar.gz
gdb-6.8.tar.bz2
ltrace_0.5.3.orig.tar.gz
strace-4.5.19.tar.bz2
gmp-4.3.2.tar.bz2
mpfr-2.4.2.tar.bz2
libelf-0.8.13.tar.gz
以下几个文件没有出现在.config文件里,其实也是需要的:
ncurses-5.7.tar.gz
ecj-latest.jar
glibc-ports-2.9.tar.bz2
sstrip.c
这些下载好的软件包放到前面设定的${HOME}/crosstool-ng/src文件夹中。
六.编译
在crosstool-ng-1.8.2_build目录下执行命令开始编译:
$ ct-ng build.4
我的电脑08年初的了,CPU是4200+,2G内存,大约编译了大概一个半小时。
没有错误的话最后几条输出为:
[INFO ] =================================================================
[INFO ] Cleaning-up the toolchain's directory
[EXTRA] Installing the populate helper
[EXTRA] Installing a cross-ldd helper
[EXTRA] Creating toolchain aliases
[EXTRA] Removing access to the build system tools
[INFO ] Cleaning-up the toolchain's directory: done in 0.14s (at 91:22)
[INFO ] Build completed at 20101016.151504
[INFO ] (elapsed: 91:21.78)
[INFO ] Finishing installation (may take a few seconds)...
[91:22] / [efronc@localhost crosstool-ng-1.8.2_build]$
如果有错误,不要只看终端中的输出,去看看/x-tools/arm-embedi-linux-gnueabi/build.log这个文件,在最后出错前会提示找不到什么等等提示。
要知道,每个人都是经历很多错误和曲折才成功的,有错误慢慢分析会解决的。
七.添加环境变量
还需要最后一步,就是添加环境变量,输入:
$ echo "PATH=$PATH:$HOME/crosstool-ng/x-tools/arm-efronc-linux-gnueabi/bin" >> ~/.bashrc
$ source ~/.bashrc
$ arm-efronc-linux-gnueabi-gcc -v
注意我的名字所efronc,不要盗版哦!否则会出错的o(∩∩)o...最后那一条就是测试用的拉,如果输出下面的内容,那么恭喜你成功拉!
[efronc@localhost bin]$ arm-efronc-linux-gnueabi-gcc -v
Using built-in specs.
Target: arm-efronc-linux-gnueabi
Configured with: /home/efronc/crosschain/crosstool-ng-1.8.2_build/targets/src/gcc-4.3.2/configure
--build=i686-build_pc-linux-gnu --host=i686-build_pc-linux-gnu --target=arm-efronc-linux-gnueabi --
prefix=/home/efronc/crosschain/x-tools/arm-efronc-linux-gnueabi --with-sysroot=/home/efronc/cros
schain/x-tools/arm-efronc-linux-gnueabi/arm-efronc-linux-gnueabi//sys-root --enable-languages=c,c
++,fortran,java --disable-multilib --with-arch=armv5te --with-cpu=arm926ej-s --with-tune=arm926ej-s
--with-float=soft --with-pkgversion=crosstool-NG-1.8.2-Efronc --disable-sjlj-exceptions --enable-__c
xa_atexit --disable-libmudflap --with-gmp=/home/efronc/crosschain/crosstool-ng-1.8.2_build/targets
/arm-efronc-linux-gnueabi/build/static --with-mpfr=/home/efronc/crosschain/crosstool-ng-1.8.2_build
/targets/arm-efronc-linux-gnueabi/build/static --enable-threads=posix --enable-target-optspace --with
-local-prefix=/home/efronc/crosschain/x-tools/arm-efronc-linux-gnueabi/arm-efronc-linux-gnueabi//sy
s-root --disable-nls --enable-symvers=gnu --enable-c99 --enable-long-long
Thread model: posix
gcc version 4.3.2 (crosstool-NG-1.8.2-Efronc)
编译好后:
编译器位于:${你配置时确定的路径}/x-tools/arm-efronc-linux-gnueabi/bin
库文件位于:${你配置时确定的路径}/x-tools/arm-efronc-linux-gnueabi/arm-efronc-linux-gnueabi/lib
八.简单测试
下面通过一个简单的程序测试刚刚建立的交叉编译工具链看是否能够正常工作。写一个最简
单的 hello.c 源文件,内容如下:
#include <stdio.h>
int main( )
{
printf(“Hello,world!\n”);
return 0;
}
通过以下命令进行编译,编译后生成名为 hello 的可执行文件,通过 file 命令可以查看文件
的类型。当显示以下信息时表明交叉工具链正常安装了,通过编译生成了 ARM 体系可执行
的文件。
注意,通过该交叉编译链编译的可执行文件只能在 ARM 体系下执行,不能在基于X86 的普通 PC 上执行。
[efronc@localhost test_for_arm_linux_gcc]$ gedit hello.c
[efronc@localhost test_for_arm_linux_gcc]$ ll
总用量 4
-rw-rw-r--. 1 efronc efronc 86 10月 16 19:03 hello.c
[efronc@localhost test_for_arm_linux_gcc]$ arm-efronc-linux-gnueabi-gcc -o hello hello.c
[efronc@localhost test_for_arm_linux_gcc]$ file hello
hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.33, not stripped
至此,交叉编译工具链建立完毕。
九.常见错误
1.
[ERROR] Missing: 'i686-pc-linux-gnu-gcj' or 'i686-pc-linux-gnu-gcj' or 'gcj' : either needed!
[INFO ] Installing native gdb
[INFO ] Installing native gdb
[EXTRA] Building static target ncurses
[ERROR] make[2]: *** [tic] Error 1
[ERROR] Build failed in step 'Installing native gdb'
[ERROR] Error happened in '/opt/crosstool-ng/lib/ct-ng-1.8.1/scripts/functions' in function 'CT_DoExecLog' (line unknown, sorry)
[ERROR] called from '/opt/crosstool-ng/lib/ct-ng-1.8.1/scripts/build/debug/300-gdb.sh' at line # 162 in function 'do_debug_gdb_build'
[ERROR] called from '/opt/crosstool-ng/lib/ct-ng-1.8.1/scripts/build/debug.sh' at line # 35 in function 'do_debug'
[ERROR] called from '/opt/crosstool-ng/lib/ct-ng-1.8.1/scripts/crosstool-NG.sh' at line # 586 in function 'main'
[ERROR] Look at '/home/iamxzg/crosstool-ngg/x-tools/arm-embedi-linux-gnueabi/build.log' for more info on this error.
[ERROR] (elapsed: 73:59.36)
[73:59] / make[1]: *** [build] 错误 2
make: *** [build.4] 错误 2
解决:sudo yum install glibc-static
在Fedora下,应事先安装glibc-static。
十.参考文献