用cygwin交叉编译环境构造基于newlib的目标系统

[原文:http://www.unixresources.net/linux/clf/embedded/archive/00/00/12/40/124083.html]

Background: 背景
===========
在交叉编译中,三个术语非常重要:host,target,build
host:toolchain要运行的目标机器
build:用来建立toolchain的机器
target:toolchain所创建的二进制代码可以运行的机器
最通常的情况是三者都是一样的,即host == build == target;
绝大多数交叉编译的情况是host == build,但是target不同;
在canadian交叉编译情况下,三个机器可能都不一样。
.... build .............. host .................. target
-----..........-------..........------
| cross .|...--->...| toolchain .|...--->...| binaries |
-----..........-------..........------

假设我们有一台运行Win2k和Cygwin的PC,我们想在该PC上建立powerpc-eabi的
二进制代码,target上的运行库选用newlib。

我们选定下面的源代码包:

1. Cygwin -- 1.1.7 (with all updates applied to date)
2. GCC -- 2.95.2-6 (part of Cygwin source distribution)
3. Binutils -- 2.10.1 (standard GNU distribution)
4. newlib -- 1.9.0 (http://sources.redhat.com/newlib/)

基本的步骤如下:

Preliminaries: 预备
==============

确定你想要安装的位置以及其它一些环境变量

cygwin$ host=i686-pc-cygwin
cygwin$ build=i686-pc-cygwin
cygwin$ target=powerpc-eabi
cygwin$ prefix=/usr/local/powerpc
cygwin$ src_root=/usr/local/src/gnu

可以根据你自己的机器环境修改$prefix和$src_root,但是最好不要修改$host,
$build和$target。同时,必须确保$src_root目录存在。

Step 1:
=======
(1)获取你所需要的所有源代码包,并放到host上。

cygwin$ cd $src_root
cygwin$ tar zxf /tmp/gcc-2.95.2.tar.gz
cygwin$ tar zxf /tmp/binutils-2.10.1.tar.gz
cygwin$ tar zxf /tmp/newlib-1.9.0.tar.gz

由于我们的host机上运行的是cygwin,所以最好是获取一个cygwin发行的gcc-2.95.2。
可以参看cygwin的主页:http://sources.redhat.com/cygwin/

Step 2:
=======
(2)建立并安装binutils。
注意:千万不要建立在源代码目录上,所以这里强行指定为$src_root/BUILD,并
将在该目录下建立binutils和gcc

cygwin$ mkdir -p $src_root/BUILD/binutils
cygwin$ cd $src_root/BUILD/binutils
cygwin$ $src_root/binutils-2.10.1/configure
--with-included-gettext
--target=$target --host=$host --build=$build
--prefix=$prefix -v
cygwin$ make > make.log 2>&1

如果一切正常,下面开始安装:

cygwin$ make install > install.log 2>&1

注意*IMPORTANT*:一定要记得增加$prefix/bin目录到PATH中

cygwin$ export PATH=$PATH:$prefix/bin

检查一下安装结果:

cygwin$ $target-ld --version
GNU ld 2.10.1
Copyright 2000 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License. This program has absolutely no warranty.
Supported emulations:
elf32ppc

一切正常!:)

Step 3:
=======
(3)建立并"只"安装GCC中的C编译器。

cygwin$ mkdir -p $src_root/BUILD/gcc
cygwin$ cd $src_root/BUILD/gcc
cygwin$ $src_root/gcc-2.95.2/configure
--enable-languages=c,c++
--with-included-gettext --enable-shared --enable-threads
--target=$target --host=$host --build=$build
--with-newlib
--prefix=$prefix -v

可以修改--enable-languages,--enable-shared和--enable-threads。
"必需"--with-newlib,因为它告诉编译器:在建立libgcc.a的时候不要加入
stdlib.h和unistd.h
注意:我们只想建立C编译器!

cygwin$ make LANGUAGES=c all-gcc > make.log 2>&1
cygwin$ mv make.log make-c-only.log

如果一切正常,下面开始安装:

cygwin$ make LANGUAGES=c install-gcc > install.log 2>&1
cygwin$ mv install.log install-c-only.log

Step 4:
=======
(4)使用刚才安装的C编译器建立并安装newlib。

cygwin$ mkdir -p $src_root/BUILD/newlib
cygwin$ cd $src_root/BUILD/newlib
cygwin$ $src_root/newlib-1.9.0/configure
--target=$target --host=$host --build=$build
--prefix=$prefix -v

cygwin$ make > make.log 2>&1

如果一切正常,下面开始安装:

cygwin$ make install > install.log 2>&1

Step 5:
=======
(5)建立并安装GCC中的其余部分(支持运行库和其它库文件的编译器和语言)

cygwin$ cd $src_root/BUILD/gcc
cygwin$ make > make.log 2>&1
cygwin$ make install > install.log 2>&1

Postscript: 附言
===========
现在做一个简单示例,创建一个$target的二进制代码。

$ cat > hello.c
#include
int
main()
{
printf("hello world ");
return 0;
}
^D
$ $target-gcc -o hello hello.c
$ ls -l hello*
hello.c hello.exe

由于我们的目标机是powerpc-eabi,所以可能至少需要一个 -m 参数,比如 -msim

如果希望去掉自动增加的.exe后缀,那么在第三步编译gcc前,修改
$src_root/gcc-2.95.2-6/gcc/config/i386/xm-cygwin.h中的EXECUTABLE_SUFFIX宏,
然后再建立gcc即可。

posted @ 2009-03-26 21:30  YY哥  阅读(2386)  评论(0)    收藏  举报