Chinaunix首页 | 论坛 | 博客
  • 博客访问: 395074
  • 博文数量: 69
  • 博客积分: 1992
  • 博客等级: 上尉
  • 技术积分: 1162
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-03 19:50
文章分类
文章存档

2015年(1)

2011年(55)

2010年(13)

分类: LINUX

2011-04-16 20:54:07

Ubuntu10.10下利用crosstool-ng构建交叉编译链

      之所以用crosstool-ng是因为crosstool-ng是最新的用来建立交叉工具链的工具,正如其官网所说,crosstool-ng is a rewrite of the original crosstool by Dan Kegel.它是crosstool的替代者,crosstoolgcc版本只能到4.1.1,无法编译版本高于2.6.29 linux内核,而crosstool-ng一直保存着更新,而且crosstool-ng is really targetted at building toolchains, and only toolchains.

 

      系统环境:pursuitxh@ubuntu:~$ uname -a

 

Linux ubuntu 2.6.35-24-generic #42-Ubuntu SMP Thu Dec 2 01:41:57 UTC 2010 i686 GNU/Linux

 

 

      一、下载crosstool-ng,并为使用安装必要的软件。
   crosstool-ng
的下载地址是:
   
值得注意的是,下载里最新的crosstool-ng以后,记得在到看看有没有相应的补丁,有得话一起下载下来。

      我这次使用的是1.9.2

      使用crosstool-ng必须先安装一些开发用的软件,在ubuntu下必须安装(使用apt):

 

#apt-get install libncurses5-dev

#apt-get install bison

#apt-get install flex

#apt-get install texinfo

#apt-get install automake

#apt-get install libtool

#apt-get install patch

#apt-get install gcj

#apt-get install cvs

#apt-get install cvsd

#apt-get install gawk

 

然后手动安装termcap

#cd /tmp
#wget ftp://ftp.gnu.org/gnu/termcap/termcap-1.3.1.tar.gz
#tar zxvf termcap-1.3.1.tar.gz
#cd termcap-1.3.1
#./configure --prefix=/usr
#make 
#make install

 

二,解压、打补丁(如果有)并建立工作目录

 

      crosstool-ngcrosstool不同的地方之一就是:她并不是一下载下来就可以使用了,必须先配置安装。
将下载下来的crosstool-ng-X.Y.Z.tar.bz2解压到你为她准备的工作目录(这里假设/home/lwm/software/crosstool,并建立安装和编译目录。

#tar xjvf crosstool-ng-1.9.2.tar.bz2 -C /home/lwm/software/crosstool

#cd /home/lwm/software/crosstool  //进入工作目录

#mkdir crosstool-ng-1.9.2_build   //这是编译新交叉编译器的工作目录

#mkdir crosstool-ng-1.9.2_install //这是crosstool-ng的安装目录


#cd  crosstool-ng-1.9.2   //
进入解压后的 crosstool-ng-1.9.2目录

#patch -p1 < 补丁文件      //如果有补丁,打补丁

#./configure –prefix=/home/lwm/software/crosstool/crosstool-ng-1.9.2_install            //配置crosstool-ng

#make                    //编译crosstool-ng

#make install            //安装crosstool-ng

# echo "PATH=$PATH:/home/lwm/software/crosstool/crosstool-ng-1.9.2_install/bin" >> ~/.bashrc //为后面调用ct-ng命令增加环境变量
# source ~/.bashrc
# ct-ng help

This is crosstool-NG version 1.9.2

 

 

 

Copyright (C) 2008  Yann E. MORIN

 

This is free software; see the source for copying conditions.

 

There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A

 

PARTICULAR PURPOSE.

 

 

 

See below for a list of available actions, listed by category:

 

 

 

Configuration actions:

 

  menuconfig         - Update current config using a menu based program

 

  oldconfig          - Update current config using a provided .config as base

 

  extractconfig      - Extract to stdout the configuration items from a

 

                       build.log file piped to stdin

 

  saveconfig         - Save current config as a preconfigured target

 

  show-tuple         - Print the tuple of the currently configured toolchain

 

 

 

Preconfigured toolchains (#: force number of // jobs):

 

  list-samples       - prints the list of all samples (for scripting)

 

  show-      - show a brief overview of (list with list-samples)

 

             - preconfigure crosstool-NG with (list with list-samples)

 

  build-all[.#]      - Build *all* samples (list with list-samples) and install in

 

                       ${CT_PREFIX} (which you must set)

 

 

 

Build actions (#: force number of // jobs):

 

  build[.#]          - Build the currently configured toolchain

 

  list-steps         - List all build steps

 

 

 

Clean actions:

 

  clean              - Remove generated files

 

  distclean          - Remove generated files, configuration and build directories

 

 

 

Distribution actions:

 

  wiki-samples       - Print a DokuWiki table of samples

 

  updatetools        - Update the config tools

 

  tarball            - Build a tarball of the configured toolchain

 

 

 

Environement variables (see /home/lwm/software/crosstool/crosstool-ng-1.9.2_install/share/doc/ct-ng-1.9.2/0 - Table of content.txt):

 

  STOP=step          - Stop the build just after this step (list with list-steps)

 

  RESTART=step       - Restart the build just before this step (list with list-steps)

 

  CT_PREFIX=dir      - install samples in dir (see action "build-all", above).

 

  V=0|1|2            - 0 => show only human-readable messages (default)

 

                       1 => show only the commands being executed

 

                       2 => show both

 

 

 

Use action "menuconfig" to configure your toolchain

 

Use action "build" to build your toolchain

 

Use action "version" to see the version

 

See "man 1 ct-ng" for some help as well

 

pursuitxh@ubuntu:/home/lwm/software/crosstool$

 

好了,crosstool-ng安装成功。

 

三,配置本次编译的交叉编译工具链

接下来就是配置你要编译的交叉编译工具链了,在crosstool-ng已很多已经做好的默认配置(位于crosstool-ng- X.Y.Z/samples目录下),你只要针对其进行修改就好了。对于编译器组件部分的版本最好不要修改,因为那个配搭应该是经过测试后的最高本版了,但内核版本可以修改。

#mkdir /home/lwm/software/mini2440/x-tools //建立交叉编译器的安装路径

#mkdir /home/lwm/software/mini2440/crosstool-ng/src //建立保存源码包目录

 

#cd /home/lwm/software/crosstool/crosstool-ng-1.9.2_build/  

//进入编译新交叉编译器的工作目录 
#cp 
../crosstool-ng-1.9.2/samples/arm-unknown-linux-gnueabi /* ./

//拷贝默认配置文件到工作目录

#mv crosstool.config .config

//修改文件名

#../crosstool-ng-1.9.2_install/bin/ct-ng menuconfig

进入menuconfig,开始修改配置。

 

此时会出现crosstool-ng的配置窗口,针对armv4t体系架构,因为mini2440用的是armv4t架构,开始对其进行修改配置:

0、已下载好的源码包路径和交叉编译器的安装路径。
Paths and misc options  --->
  (
/home/lwm/software/mini2440/crosstool-ng/src) Local tarballs directory   保存源码包路径
  (
/home/lwm/software/mini2440/x-tools/${CT_TARGET}) Prefix directory  交叉编译器的安装路径
这两个请依据你的实际情况来修改。

1Target options --->
 *** Target optimisations ***
(
armv4t) Architecture level 
(
arm9tdmi) Emit assembly for CPU
(
arm920t) Tune for CPU
通过查找资料,这个应该是指令集的架构,对于S3C2410/S3C2440,都是ARM920T的核心,架构指令是armv4t,所以,如果要编译2410/2440的,就选armv4t。以此类推S3C6410 ARM1176JZF-S核心 使用的是armv6架构,就选armv6,那么具体都支持哪些架构呢?可以用man gcc来查询,搜索arm,再搜索-march=就可以找到本gcc支持的处理器核心列表了。

 

以上这几个参数是如何得出的可以参考gccman手册,你可以在你下载的gcc-4.3.2.tar.bz2解压后找到,她的位置是gcc-4.3.2/gcc/doc/gcc.1。打开方式:

man ./gcc.1


你可以在其中找到:

......
 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.

......

2、根据参考资料,禁止内核头文件检测(只起到节约时间的作用(不到1S的时间),不推荐禁用)
Operating System  --->

 [N]     Check installed headers

3、增加编译时的并行进程数,以增加运行效率,加快编译。
Paths and misc options  --->
   *** Build behavior *** 
   (
4) Number of parallel jobs 
   
这个数值不宜过大,应该为CPU数量的两倍。由于我的CPU是双核的,所以我填了4.

4、一些个性化的修改(可以不修改)
Toolchain options  --->
       *** Tuple completion and aliasing ***  
       (
lwm) Tuple's vendor string 
这样产生的编译器前缀就是:arm-lwm-linux-gnueabi-

5C compiler  --->
       (
crosstool-NG-${CT_VERSION}-lwm) gcc ID string
    以上红色字体部分即为修改之处。

配置好以后保存。

最后,内核源码的版本号修改,请直接修改crosstool-ng-1.9.2_build目录下的.config文件,不止一处,有关的都要修改。
有:

CT_KERNEL_VERSION=
CT_KERNEL_V_2_6_
??_=y
CT_LIBC_GLIBC_MIN_KERNEL=

如果再次../crosstool-ng-1.9.2_install/bin/ct-ng menuconfig,这个修改又会复原,必须再次手工修改。
你也可以选择修改crosstool-ng-X.Y.Z_build/config/kernel/linux.in等文件,只是比较麻烦,但这可以彻底解决,实现在界面中增加内核版本。

 

 配置完后,在crosstool-ng-1.9.2_build目录下执行命令:$ ct-ng build.4

接下来,就是漫长的等待。crosstool-ng会自动下载相应的包,内核文件,然后解压,配置,编译,链接。

我用的是这种方法。

 

虽然crosstool-ng发现找不到软件包的时候会自动下载,但是比较慢。根据这次的配置情况(查看.config”文件),我预先下载了以下的软件包:

[EXTRA]    Saving 'linux-2.6.36.tar.bz2' to local storage

 

[EXTRA]    Saving 'gmp-4.3.2.tar.bz2' to local storage

 

[EXTRA]    Saving 'mpfr-2.4.2.tar.bz2' to local storage

 

[EXTRA]    Saving 'ppl-0.10.2.tar.bz2' to local storage

 

[EXTRA]    Saving 'cloog-ppl-0.15.10.tar.gz' to local storage

 

[EXTRA]    Saving 'libelf-0.8.13.tar.gz' to local storage

 

[EXTRA]    Saving 'binutils-2.19.1.tar.bz2' to local storage

 

[EXTRA]    Saving 'gcc-4.3.2.tar.bz2' to local storage

 

[EXTRA]    Saving 'ecj-latest.jar' to local storage

 

[EXTRA]    Saving 'glibc-2.9.tar.bz2' to local storage

 

[EXTRA]    Saving 'glibc-ports-2.9.tar.bz2' to local storage

 

[EXTRA]    Saving 'dmalloc-5.5.2.tgz' to local storage

 

[EXTRA]    Saving 'duma_2_5_15.tar.gz' to local storage

 

[EXTRA]    Saving 'gdb-6.8.tar.bz2' to local storage

 

[EXTRA]    Saving 'ncurses-5.7.tar.gz' to local storage

 

[EXTRA]    Saving 'expat-2.0.1.tar.gz' to local storage

 

[EXTRA]    Saving 'ltrace_0.5.3.orig.tar.gz' to local storage

 

[EXTRA]    Saving 'strace-4.5.19.tar.bz2' to local storage

 


下载完之后,记得将这些软件包放在配置时指定的文件夹。

接着和上面一样,在/crosstool-ng-1.9.2_install/bin/目录下执行 ct-ng build.4

进行编译,我大约用了两个小时

 

六、增加环境变量
    
以上过程若没有出现错误,那恭喜你成功了,是否该打开香槟庆祝一下呢,sorry,我没有,以后一定有。呀,但别急,还需要最后一步,就是增加环境变量,输入:
$ echo "PATH=$PATH:
/home/lwm/software/mini2440/x-tools/arm-lwm-linux-gnueabi/bin" >> ~/.bashrc
    
OK,大功造成,是否该测试一下呢,简单:

pursuitxh@ubuntu:/home/lwm/software/crosstool/crosstool-ng-1.9.2_build$ arm-lwm-linux-gnueabi-gcc -v

 

Using built-in specs.

 

Target: arm-lwm-linux-gnueabi

 

Configured with: /home/lwm/software/crosstool/crosstool-ng-1.9.2_build/.build/src/gcc-4.3.2/configure --build=i686-build_pc-linux-gnu --host=i686-build_pc-linux-gnu --target=arm-lwm-linux-gnueabi --prefix=/home/lwm/software/mini2440/x-tools/arm-lwm-linux-gnueabi --with-sysroot=/home/lwm/software/mini2440/x-tools/arm-lwm-linux-gnueabi/arm-lwm-linux-gnueabi//sys-root --enable-languages=c,c++,fortran,java --disable-multilib --with-arch=armv4t --with-cpu=arm9tdmi --with-tune=arm920t --with-float=soft --with-pkgversion=crosstool-NG-1.9.2-lwm --disable-sjlj-exceptions --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --with-gmp=/home/lwm/software/crosstool/crosstool-ng-1.9.2_build/.build/arm-lwm-linux-gnueabi/build/static --with-mpfr=/home/lwm/software/crosstool/crosstool-ng-1.9.2_build/.build/arm-lwm-linux-gnueabi/build/static --enable-threads=posix --enable-target-optspace --with-local-prefix=/home/lwm/software/mini2440/x-tools/arm-lwm-linux-gnueabi/arm-lwm-linux-gnueabi//sys-root --disable-nls --enable-symvers=gnu --enable-c99 --enable-long-long

 

Thread model: posix

 

gcc version 4.3.2 (crosstool-NG-1.9.2-lwm)

 

pursuitxh@ubuntu:/home/lwm/software/crosstool/crosstool-ng-1.9.2_build$

 

七、编译好后的交叉编译器。
编译器位于:#/home/lwm/software/mini2440/x-tools/arm-lwm-linux-gnueabi/bin
库文件位于:#/home/lwm/software/mini2440/x-tools/arm-lwm-linux-gnueabi/arm-lwm-linux-gnueabi/lib

 

出现问题:

 

root@ubuntu:/home/lwm/software/crosstool/crosstool-ng-1.9.2_build# ct-ng build.4

 

[INFO ]  Performing some trivial sanity checks

 

[INFO ]  Build started 20110111.111651

 

[INFO ]  Building environment variables

 

[EXTRA]  Preparing working directories

 

[ERROR]  Missing: 'i686-pc-linux-gnu-g++' or 'i686-pc-linux-gnu-g++' or 'g++' : either needed!

 

[00:01] / make[1]: *** [build] 错误 1

 

make: *** [build.4] 错误 2

 

 

解决办法:

root@ubuntu:/home/lwm/software/crosstool/crosstool-ng-1.9.2_build# apt-get install g++

 

最后编写一个程序测试一下:

#include

int main()

{

    printf(“hello,this is my own toolchain!\n”);

}

#arm-lwm-linux-gnueabi-gcc -o hello.c hello

#file hello

pursuitxh@ubuntu:/home$ file hello

 

hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.36, not stripped

 

 

参考博客:

 

http://blogold.chinaunix.net/u1/34474/showart_2198589.html

 

http://blogold.chinaunix.net/u3/104206/showart_2325147.html

 

http://hi.baidu.com/caicry/blog/item/f306db639c4281680c33fa1b.html

阅读(3392) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~