使用crosstool-ng编译toolchain

host为ubuntu16.04

下载相关工具

$ sudo apt-get install automake bison chrpath flex g++ git gperf 
gawk libexpat1-dev libncurses5-dev libsdl1.2-dev libtool 
python2.7-dev texinfo

安装过程

$ git clone https://github.com/crosstool-ng/crosstool-ng.git
$ cd crosstool-ng
$ git checkout crosstool-ng-1.22.0
$ ./bootstrap
$ ./configure --enable-local
$ make
$ make install

--enable-local选项代表安装在当前目录下。

过程中出现configure: error: could not find GNU libtool >= 1.5.26

解决方法

$ sudo apt install libtool-bin

列出可用的示例

$ ./ct-ng list-samples

可用加上show前缀来显示具体信息

$ ./ct-ng show-arm-cortexa9_neon-linux-gnueabihf

[L.X] arm-cortexa9_neon-linux-gnueabihf
OS : linux-4.3
Companion libs : gmp-6.0.0a mpfr-3.1.3 mpc-1.0.3 expat-2.1.0 ncurses-6.0
binutils : binutils-2.22
C compilers : gcc | 5.2.0
Languages : C,C++
C library : glibc-2.22 (threads: nptl)
Tools : gdb-7.10

和编译kernel一样,执行下列将sample配置写入.config

$ ./ct-ng arm-cortexa9_neon-linux-gnueabihf

执行下列可进行配置

$ ./ct-ng menuconfig

执行下列命令开始构建

./ct-ng build

 大约一个小时可以编译完成(e3-1230v2),编译完成后会在家目录生产x-tools目录,

~/x-tools/arm-cortexa9_neon-linux-gnueabihf/bin存放二进制编译工具。

PATH=~/x-tools/arm-cortexa9_neon-linux-gnueabihf/bin/:${PATH}加入环境变量

编写测试程序

$ vim test.c
#include <stdio.h>

int main(void)
{
        printf("hello world
");
        return 0;
}

$ arm-cortexa9_neon-linux-gnueabihf-gcc test.c
$ file a.out 
a.out: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, not stripped

成功编译

查看版本信息

$ arm-cortexa9_neon-linux-gnueabihf-gcc --version
arm-cortexa9_neon-linux-gnueabihf-gcc (crosstool-NG crosstool-ng-1.22.0) 5.2.0
Copyright (C) 2015 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.

查看配置信息

$ arm-cortexa9_neon-linux-gnueabihf-gcc -v
Using built-in specs.
COLLECT_GCC=arm-cortexa9_neon-linux-gnueabihf-gcc
COLLECT_LTO_WRAPPER=/home/rzx/x-tools/arm-cortexa9_neon-linux-gnueabihf/libexec/gcc/arm-cortexa9_neon-linux-gnueabihf/5.2.0/lto-wrapper
Target: arm-cortexa9_neon-linux-gnueabihf
Configured with: /home/rzx/work/crosstool-ng/.build/src/gcc-5.2.0/configure --build=x86_64-build_pc-linux-gnu --host=x86_64-build_pc-linux-gnu --target=arm-cortexa9_neon-linux-gnueabihf --prefix=/home/rzx/x-tools/arm-cortexa9_neon-linux-gnueabihf --with-sysroot=/home/rzx/x-tools/arm-cortexa9_neon-linux-gnueabihf/arm-cortexa9_neon-linux-gnueabihf/sysroot --enable-languages=c,c++ --with-cpu=cortex-a9 --with-fpu=neon --with-float=hard --with-pkgversion='crosstool-NG crosstool-ng-1.22.0' --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --disable-libsanitizer --with-gmp=/home/rzx/work/crosstool-ng/.build/arm-cortexa9_neon-linux-gnueabihf/buildtools --with-mpfr=/home/rzx/work/crosstool-ng/.build/arm-cortexa9_neon-linux-gnueabihf/buildtools --with-mpc=/home/rzx/work/crosstool-ng/.build/arm-cortexa9_neon-linux-gnueabihf/buildtools --with-isl=/home/rzx/work/crosstool-ng/.build/arm-cortexa9_neon-linux-gnueabihf/buildtools --with-cloog=/home/rzx/work/crosstool-ng/.build/arm-cortexa9_neon-linux-gnueabihf/buildtools --with-libelf=/home/rzx/work/crosstool-ng/.build/arm-cortexa9_neon-linux-gnueabihf/buildtools --enable-lto --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --enable-threads=posix --enable-plugin --enable-gold --disable-multilib --with-local-prefix=/home/rzx/x-tools/arm-cortexa9_neon-linux-gnueabihf/arm-cortexa9_neon-linux-gnueabihf/sysroot --enable-long-long
Thread model: posix
gcc version 5.2.0 (crosstool-NG crosstool-ng-1.22.0) 

几个重点参数

--with-sysroot=/home/chris/x-tools/arm-cortex_a8-linuxgnueabihf/arm-cortex_a8-linux-gnueabihf/sysroot: This is the default sysroot directory; see the following section for an explanation

--enable-languages=c,c++: Using this, we have both C and C++ languages enabled

--with-cpu=cortex-a8: The code is generated for an ARM Cortex A8 core

--with-float=hard: Generates opcodes for the floating point unit and uses the VFP registers for parameters

--enable-threads=posix: This enables the POSIX threads

这些默认的配置信息可以通过使用时在命令行进行重写,例如

$ arm-cortex_a8-linux-gnueabihf-gcc -mcpu=cortex-a5 helloworld.c 
 -o helloworld

查看相关帮助

$ arm-cortexa9_neon-linux-gnueabihf-gcc --target-help

所以说,构建编译链有两种思想:

1.buildroot在开始时就进行详细正确的配置。

2.yocto构建比较通用的形式,针对不同target进行重写。

原文地址:https://www.cnblogs.com/-rzx-/p/12233329.html