用Crosstool-ng构建交叉编译工具链

交叉编译指在一个平台上生成另一个平台上的可执行代码。常见的有三种:

  • 在Windows PC上,利用集成开发环境(ADS),运行armcc编译器,编译出针对ARM CPU的可执行代码
  • 在Windows PC上,利用cygwin环境,运行arm-elf-gcc编译器,编译出针对ARM CPU 的可执行代码
  • 在Linux PC上,利用arm-linux-gcc编译器,编译出针对Linux ARM平台的可执行代码

交叉编译工具链是一个由编译器、连接器和解释器组成的综合开发环境,交叉编译工具链主要由binutils、gcc和glibc 3部分组成。
本文使用Crosstool-ng在Ubuntu系统中构建用于ARM平台的交叉编译工具链。

  • 交叉编译工具:arm-linux-6.3.0
  • 编译平台:Ubuntu 14.04.4 LTS
  • 目标平台:S3C6410
  • crosstool-ng-1.23.0

一 、下载安装crosstool-ng

  1. 从crosstool-ng官网或github上下载最新版本。
~/arm-linux-tools$ wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.23.0.tar.bz2
  1. 解压源码,进入源码目录
~/arm-linux-tools$ tar jxvf crosstool-ng-1.23.0.tar.bz2 
~/arm-linux-tools$ cd crosstool-ng-1.23.0/
  1. 安装编译依赖包
sudo apt-get install sed bash dpkg-dev bison flex patch texinfo automake m4 libtool cvs websvn tar gzip bzip2 lzma libncurses5-dev bison flex texinfo automake libtool patch cvs cvsd gperf help2man gawk

安装中提示如下错误:
dpkg: error processing package websvn (--configure):
subprocess installed post-installation script returned error exit status 1
E: Sub-process /usr/bin/dpkg returned an error code (1)
解决

$ sudo mv /var/lib/dpkg/info/ /var/lib/dpkg/info_old/
$ sudo mkdir /var/lib/dpkg/info/
$ sudo apt-get update
  1. 执行编译三部曲
./configure --prefix=/opt/crosstool-ng
make
sudo make install

安装成功后会提示要配置环境变量:

For auto-completion, do not forget to install 'ct-ng.comp' into
your bash completion directory (usually /etc/bash_completion.d)

  1. 配置环境变量
echo "PATH=$PATH:/opt/crosstool-ng/bin" >> ~/.bashrc
source ~/.bashrc

测试是否安装成功

ct-ng -v

GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for x86_64-pc-linux-gnu

二、 配置交叉编译工具链

创建目录:交叉工具链的输出目录,保存源码包目录,交叉编译器的安装路径

~/arm-linux-tools$ mkdir crosstool-build src x-tools

将crosstool-ng源码包中samples/arm-unknown-linux-gnueabi/下的crosstool.config拷贝到交叉工具链的输出目录crosstool-build下

~/arm-linux-tools/crosstool-build$ cp /opt/crosstool-ng/lib/crosstool-ng-1.23.0/samples/arm-unknown-linux-gnueabi/crosstool.config .config

执行ct-ng menuconfig进入配置界面

ct-ng menuconfig
用Crosstool-ng构建交叉编译工具链_第1张图片
crosstool-ng 配置界面
  • 设定源码包路径和交叉编译工具链的安装路径
    Paths and misc options--->
    激活 'Try features marked as EXPERIMENTAL' 选项
    (${HOME}/arm-linux-tools/src) Local tarballs directory
    (${HOME}/crosstool-ng/x-tools) Prefix directory
  • 修改交叉编译工具针对的架构(S3C6410)
    Target options--->
    Target architecture(arm)
    Endianness(Little endian)
    Bitness(32-bit)
    (armv6zk) Architecture level
    (arm1176jzf-s) Emit assembly for CPU
    Floating point(hardware FPU)
  • 其它选项用缺省值,退出并保存配置工具
  • 以上配置也可通过直接修改.config文件来完成

执行ct-ng build编译工具链

ct-ng build

编译大约花费4个小时,编译过程中会自动下载所需的源码:


用Crosstool-ng构建交叉编译工具链_第2张图片
交叉编译器所需要的源码文件列表

添加环境变量

echo "PATH=$PATH:$HOME/arm-linux-tools/x-tools/bin" >> ~/.bashrc
source ~/.bashrc

测试是否安装成功

arm-unknown-linux-gnueabihf-gcc -v

Using built-in specs.
COLLECT_GCC=arm-unknown-linux-gnueabihf-gcc
COLLECT_LTO_WRAPPER=/home/lionel/arm-linux-tools/x-tools/libexec/gcc/arm-unknown-linux-gnueabihf/6.3.0/lto-wrapper
Target: arm-unknown-linux-gnueabihf
Configured with: /home/lionel/arm-linux-tools/crosstool-build/.build/src/gcc-6.3.0/configure --build=x86_64-build_pc-linux-gnu --host=x86_64-build_pc-linux-gnu --target=arm-unknown-linux-gnueabihf --prefix=/home/lionel/arm-linux-tools/x-tools --with-sysroot=/home/lionel/arm-linux-tools/x-tools/arm-unknown-linux-gnueabihf/sysroot --enable-languages=c,c++ --with-cpu=arm1176jzf-s --with-float=hard --with-pkgversion='crosstool-NG crosstool-ng-1.23.0' --disable-sjlj-exceptions --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --disable-libsanitizer --disable-libmpx --with-gmp=/home/lionel/arm-linux-tools/crosstool-build/.build/arm-unknown-linux-gnueabihf/buildtools --with-mpfr=/home/lionel/arm-linux-tools/crosstool-build/.build/arm-unknown-linux-gnueabihf/buildtools --with-mpc=/home/lionel/arm-linux-tools/crosstool-build/.build/arm-unknown-linux-gnueabihf/buildtools --with-isl=/home/lionel/arm-linux-tools/crosstool-build/.build/arm-unknown-linux-gnueabihf/buildtools --enable-lto --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --enable-threads=posix --enable-target-optspace --enable-plugin --enable-gold --disable-nls --disable-multilib --with-local-prefix=/home/lionel/arm-linux-tools/x-tools/arm-unknown-linux-gnueabihf/sysroot --enable-long-long
Thread model: posix
gcc version 6.3.0 (crosstool-NG crosstool-ng-1.23.0)

标准化交叉编译链arm-linux-gcc-6.3.0

上面编译生成的工具arm-unknown-linux-gnueabihf-这样的命令,但是很多标准Makefile需要实用标准的交叉编译器的名称,一般这个名称是arm-linux-gcc这样的。
我们可以通过一个shell脚本来完成:

~/arm-linux-tools/x-tools/bin$ sudo vim link.sh
~/arm-linux-tools/x-tools/bin$ sudo sh link.sh

link.sh

#!/bin/sh
PREFIX=arm-unknown-linux-gnueabihf-
AFTFIX=arm-linux-
ln -s ${PREFIX}gcc ${AFTFIX}gcc
ln -s ${PREFIX}addr2line ${AFTFIX}addr2line
ln -s  ${PREFIX}ar ${AFTFIX}ar
ln -s  ${PREFIX}as ${AFTFIX}as
ln -s  ${PREFIX}c++ ${AFTFIX}c++
ln -s  ${PREFIX}c++filt ${AFTFIX}c++filt
ln -s  ${PREFIX}cpp ${AFTFIX}cpp
ln -s  ${PREFIX}g++ ${AFTFIX}g++
ln -s  ${PREFIX}gcov ${AFTFIX}gcov
ln -s  ${PREFIX}gdb ${AFTFIX}gdb
ln -s  ${PREFIX}gprof ${AFTFIX}gprof
ln -s  ${PREFIX}ld ${AFTFIX}ld
ln -s  ${PREFIX}ldd ${AFTFIX}ldd
ln -s  ${PREFIX}nm ${AFTFIX}nm
ln -s  ${PREFIX}objcopy ${AFTFIX}objcopy
ln -s  ${PREFIX}objdump ${AFTFIX}objdump
ln -s  ${PREFIX}populate ${AFTFIX}populate
ln -s  ${PREFIX}ranlib ${AFTFIX}ranlib
ln -s  ${PREFIX}readelf ${AFTFIX}readelf
ln -s  ${PREFIX}size ${AFTFIX}size
ln -s  ${PREFIX}strings ${AFTFIX}strings
ln -s  ${PREFIX}strip ${AFTFIX}strip
ln -s  ${PREFIX}cc ${AFTFIX}cc
ln -s  ${PREFIX}dwp ${AFTFIX}dwp
ln -s  ${PREFIX}elfedit ${AFTFIX}elfedit
ln -s  ${PREFIX}gcc-6.3.0 ${AFTFIX}gcc-6.3.0
ln -s  ${PREFIX}gcc-ar ${AFTFIX}gcc-ar
ln -s  ${PREFIX}gcc-nm ${AFTFIX}gcc-nm
ln -s  ${PREFIX}gcc-ranlib ${AFTFIX}gcc-ranlib
ln -s  ${PREFIX}gcov-tool ${AFTFIX}gcov-tool
ln -s  ${PREFIX}ld.bfd ${AFTFIX}ld.bfd
ln -s  ${PREFIX}ld.gold ${AFTFIX}ld.gold

测试

arm-linux-gcc -v

Using built-in specs.
COLLECT_GCC=arm-linux-gcc
COLLECT_LTO_WRAPPER=/home/lionel/arm-linux-tools/x-tools/libexec/gcc/arm-unknown-linux-gnueabihf/6.3.0/lto-wrapper
Target: arm-unknown-linux-gnueabihf
Configured with: /home/lionel/arm-linux-tools/crosstool-build/.build/src/gcc-6.3.0/configure --build=x86_64-build_pc-linux-gnu --host=x86_64-build_pc-linux-gnu --target=arm-unknown-linux-gnueabihf --prefix=/home/lionel/arm-linux-tools/x-tools --with-sysroot=/home/lionel/arm-linux-tools/x-tools/arm-unknown-linux-gnueabihf/sysroot --enable-languages=c,c++ --with-cpu=arm1176jzf-s --with-float=hard --with-pkgversion='crosstool-NG crosstool-ng-1.23.0' --disable-sjlj-exceptions --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --disable-libsanitizer --disable-libmpx --with-gmp=/home/lionel/arm-linux-tools/crosstool-build/.build/arm-unknown-linux-gnueabihf/buildtools --with-mpfr=/home/lionel/arm-linux-tools/crosstool-build/.build/arm-unknown-linux-gnueabihf/buildtools --with-mpc=/home/lionel/arm-linux-tools/crosstool-build/.build/arm-unknown-linux-gnueabihf/buildtools --with-isl=/home/lionel/arm-linux-tools/crosstool-build/.build/arm-unknown-linux-gnueabihf/buildtools --enable-lto --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --enable-threads=posix --enable-target-optspace --enable-plugin --enable-gold --disable-nls --disable-multilib --with-local-prefix=/home/lionel/arm-linux-tools/x-tools/arm-unknown-linux-gnueabihf/sysroot --enable-long-long
Thread model: posix
gcc version 6.3.0 (crosstool-NG crosstool-ng-1.23.0)

你可能感兴趣的:(用Crosstool-ng构建交叉编译工具链)