如何制作Linux交叉编译器

TooChain包括的内容

gcc  - C编译器
glibc - C标准库
binutils - 二进制工具集,如ld, ar, as等
kernle header files - Linux内核头文件

这4部分不同的版本的正确组合才能成功构建一个交叉编译器,可参考http://www.kegel.com/crosstool/


几个重要的名词

Target: 嵌入式软件真正运行的机器,不是开发机器,而是目标机器,一般用[arch]-[vendor]-[OS]-[libc]进行标识,
  • Common archs: arm, sparc, x86_64, powerpc
  • Common vendors: pc, softfloat, unknown
  • Common OS: liniux, elf, uclinux
  • Common libc: gnu, eabi, uclibc
比如:arm-unknown-linux-gnu。

Sysroot: 目标系统的根目录

ToolChain详细的制作过程


大概的过程是:
1. 从kernel source package提取出头文件
2. 使用本地gcc编译binutils
3. 使用本地gcc编译出一个简单的不用libc的simple ARM-GCC
4. 使用simple ARM-GCC编译出glibc
5. 使用本地GCC编译出完整版的ARM-GCC
6. 使用完整版的ARM-GCC编译binutils

可见,这里涉及到三个GCC,一个是本地GCC,然后是不带libc的简单的ARM-GCC,然后是完整的ARM-GCC。

详细过程如下:
1. Environment setup
§Define your target
–Export the ARCH part of the target
export ARCH=”arm”
–Export the tuple as TARGET
export TARGET=”${ARCH}-unknown-linux-gnu”
§Define your build target (ie the target for you PC)
–Export the tuplce as BUILD
export BUILD=”i686-unknown-linux-gnu”
§Create a directory which will contain our work
–Export this value as PREFIX
export PREFIX=”/usr/${TARGET}”
§Create the sysroot
–Export this value as SYSROOT
export SYSROOT=”${PREFIX}/sysroot”
2. Build  binutils with local gcc
1.Download the binutils tarball from ftp.gnu.org
2. Extract  binutils  to PREFIX/ src
tar -xf binutils-*.tar.gz -C “${PREFIX}/src”
3. Create the build directory
mkdir -p “${PREFIX}/BUILD/binutils”
4. Enter the build directory
cd “${PREFIX}/BUILD/binutils”
5. Configure  binutils
../../binutils-*/configure \
  --prefix=${PREFIX} --target=${TARGET} \
  --with-sysroot=${SYSROOT}
6. Make  binutils  and install
make && make install
3. Generate  linux  headers
1.Download the Linux kernel source tarball from www.kernel.org
2. Extract the kernel to PREFIX/ src
tar -xf linux-*.tar.gz -C “${PREFIX}/src”
3. Create the  linux  symlink
cd “${PREFIX}/src”
ln -s “${PREFIX}/src/linux-”* “${PREFIX}/src/linux”
4. Enter the  linux  directory
cd “${PREFIX}/src/linux”
5. Generate the headers with the  defconfig  for your platform (or  menuconfig )
make mts6000_defconfig
make include/linux/version.h
mkdir -p “${SYSROOT}/usr/include”
cp -a include/linux “${SYSROOT}/usr/include”
cp -a include/asm-arm “${SYSROOT}/usr/include”
cp -a include/asm-generic “${SYSROOT}/usr/include”
4. Generate  glibc  headers
1.Download the glibc tarball from ftp.gnu.org
2. Extract  glibc  to PREFIX/ src
tar -xf glibc-*.tar.gz -C “${PREFIX}/src”
3. Create the build directory
mkdir -p “${PREFIX}/BUILD/glibc-headers”
4. Enter the build directory
cd “${PREFIX}/BUILD/glibc-headers”
5. Configure  glibc
../../glibc-*/configure \
  --prefix=/usr –host=${TARGET}\
  --with-headers=${SYSROOT}/usr/include
6. Make the headers
make cross-compiling=yes install_root=${SYSROOT} install-headers
7. Create empty files needed later (these will be replaced w/ the real build)
touch ${SYSROOT}/usr/include/gnu/stubs.h
touch ${SYSROOT}/usr/include/bits/stdio_lim.h
5. Build  gcc  (needed for  glibc ) with local gcc
1.Download the gcc tarball from ftp.gnu.org
2. Extract  gcc  to PREFIX/ src
tar -xf gcc-*.tar.gz -C “${PREFIX}/src”
3. Create the build directory
mkdir -p “${PREFIX}/BUILD/gcc”
4. Enter the build directory
cd “${PREFIX}/BUILD/gcc”
5. You may need to patch  gcc  to generate  ctri.o  and  ctrn.o  needed by  glibc
patch:
  http://frank.harvard.edu/~coldwell/toolchain/t-linux.diff
6. Configure  gcc  to just generate  gcc  (no g++,  gcj ,  etc )
../../gcc-*/configure \
  –prefix=${PREFIX} –target=${TARGET}\
  --enable-languages=c --with-sysroot=${SYSROOT}
7. Make  gcc  and install
make && make install
6. Build  glibc  (needed for g++) with gcc built in the last step
1.Create the build directory
mkdir -p “${PREFIX}/BUILD/glibc”
2. Enter the build directory
cd “${PREFIX}/BUILD/glibc”
3. Reconfigure  glibc  to really build now
export CROSS_COMPILE=”${TARGET}-”
BUILD_CC=gcc CC=${CROSS_COMPILE}gcc \
  AR=${CROSS_COMPILE}ar RANLIB=${CROSS_COMPILE}ranlib \
  AS=${CROSS_COMPILE}as LD=${CROSS_COMPILE}ld \
  ../../glibc-*/configure --prefix=/usr –build=${BUILD}\
    --host=${TARGET} –target=${TARGET}\
    --with-headers=${SYSROOT}/usr/include
4. Make and install  glibc
make && make install_root=”${SYSROOT}” install
7.Build gcc,g++
1.Enter the build directory
cd “${PREFIX}/BUILD/gcc”
2.Configure gcc to just generate gcc and g++ (now that we have glibc)
../../gcc-*/configure \
  --prefix=${PREFIX} --target=${TARGET} \
  --enable-languages=c,c++ --with-sysroot=${SYSROOT}
3. Make  gcc  and install
make && make install
8.Build any other tools: kernel, gdb, libtool, etc
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值