linux centos7 使用 crosstool-ng 构建 交叉编译 工具链 即构建各cpu架构平台的gcc编译器

简介

crosstool-ng,全称是crosstool Next Generation,即下一代crosstool。crosstool是个交叉编译器的制作工具,但是做的不够好,于是有人(Yann E. MORIN)弄出了个更好的——crosstool-ng。crosstool-ng的特点:

支持menuconfig(类似于Linux内核配置)

支持众多的架构

可选多种不同的C库等模块

提供示例配置

支持多种主机编译环境:各种Linux发行版,Cygwin等

安装

#yum安装需要的包
yum install -y gperf bison flex texinfo help2man gcc-c++ expat-devel patch ncurses-devel automake libstdc++-static


#下载crosstool-ng并源码编译 设置软连接
wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.23.0.tar.xz
tar -Jvxf crosstool-ng-1.23.0.tar.xz
cd crosstool-ng-1.23.0
./configure --prefix=/usr/local/crosstool-ng
make
make install
ln -s  /usr/local/crosstool-ng/bin/ct-ng /usr/local/bin/ct-ng

制作交叉编译工具链

1.使用ct-ng list-samples命令查看具有哪些默认配置。具体如下:

[G..]   aarch64-rpi3-linux-gnueabi
[G..]   aarch64-unknown-linux-gnueabi
[G..]   aarch64-unknown-linux-uclibcgnueabi
[G..]   alphaev56-unknown-linux-gnu
[G..]   alphaev67-unknown-linux-gnu
[G..]   arm-bare_newlib_cortex_m3_nommu-eabi
[G..]   arm-cortex_a15-linux-gnueabihf
[G.X]   arm-cortexa5-linux-uclibcgnueabihf
[G..]   arm-cortex_a8-linux-gnueabi
[G.X]   arm-cortexa9_neon-linux-gnueabihf
[G.X]   x86_64-w64-mingw32,arm-cortexa9_neon-linux-gnueabihf
[G..]   armeb-unknown-eabi
[G..]   armeb-unknown-linux-gnueabi
[G..]   armeb-unknown-linux-uclibcgnueabi
[G..]   arm-multilib-linux-uclibcgnueabi
[G..]   arm-nano-eabi
[G..]   arm-unknown-eabi
[G..]   arm-unknown-linux-gnueabi
[G.X]   arm-unknown-linux-musleabi
[G..]   arm-unknown-linux-uclibcgnueabi
[G.X]   arm-unknown-linux-uclibcgnueabihf
[G..]   armv6-nommu-linux-uclibcgnueabi
[G..]   armv6-rpi-linux-gnueabi
[G..]   armv7-rpi2-linux-gnueabihf
[G..]   armv8-rpi3-linux-gnueabihf
[G..]   avr
[G..]   i586-geode-linux-uclibc
[G..]   i686-centos6-linux-gnu
[G..]   i686-centos7-linux-gnu
[G..]   i686-nptl-linux-gnu
[G..]   i686-ubuntu12.04-linux-gnu
[G..]   i686-ubuntu14.04-linux-gnu
[G..]   i686-ubuntu16.04-linux-gnu
[G.X]   i686-w64-mingw32
[G..]   m68k-unknown-elf
[G..]   m68k-unknown-uclinux-uclibc
[G..]   powerpc-unknown-linux-uclibc,m68k-unknown-uclinux-uclibc
[G..]   mips64el-multilib-linux-uclibc
[G..]   mips-ar2315-linux-gnu
[G..]   mipsel-multilib-linux-gnu
[G..]   mipsel-sde-elf
[G..]   mipsel-unknown-linux-gnu
[G..]   mips-malta-linux-gnu
[G..]   mips-unknown-elf
[G..]   mips-unknown-linux-uclibc
[G.X]   i686-w64-mingw32,nios2-spico-elf
[G..]   powerpc-405-linux-gnu
[G..]   powerpc64le-unknown-linux-gnu
[G..]   powerpc64-multilib-linux-gnu
[G..]   powerpc64-unknown-linux-gnu
[G..]   powerpc-860-linux-gnu
[G..]   powerpc-e300c3-linux-gnu
[G..]   powerpc-e500v2-linux-gnuspe
[G..]   x86_64-multilib-linux-uclibc,powerpc-unknown-elf
[G..]   powerpc-unknown-linux-gnu
[G..]   powerpc-unknown-linux-uclibc
[G..]   powerpc-unknown_nofpu-linux-gnu
[G.X]   s390-ibm-linux-gnu
[G..]   s390x-ibm-linux-gnu
[G..]   sh4-multilib-linux-gnu
[G..]   sh4-multilib-linux-uclibc
[G..]   sh4-unknown-linux-gnu
[G..]   sparc64-multilib-linux-gnu
[G..]   sparc-leon-linux-uclibc
[G..]   sparc-unknown-linux-gnu
[G..]   x86_64-centos6-linux-gnu
[G..]   x86_64-centos7-linux-gnu
[G..]   x86_64-multilib-linux-gnu
[G.X]   x86_64-multilib-linux-musl
[G..]   x86_64-multilib-linux-uclibc
[G.X]   x86_64-w64-mingw32,x86_64-pc-linux-gnu
[G..]   x86_64-ubuntu12.04-linux-gnu
[G..]   x86_64-ubuntu14.04-linux-gnu
[G..]   x86_64-ubuntu16.04-linux-gnu
[G..]   x86_64-unknown-linux-gnu
[G..]   x86_64-unknown-linux-uclibc
[G.X]   x86_64-w64-mingw32
[G..]   xtensa-fsf-linux-uclibc

列表中名词解析参考:crosstool-ng 交叉工具链默认配置 名词解释  crosstool-ng 交叉工具链默认配置 名词解释_whatday的专栏-CSDN博客

2.以其中的 arm-unknown-linux-gnueabi 配置为基础,进行一些自定义配置:必须是非root用户 这里以test用户为示例

cd /home/test
mkdir -p crosstool-ng_build/src crosstool-ng_build/arm-unknown-linux-gnueabi
# 注意 /home/test/crosstool-ng_lib/ 里是 crosstool-ng 交叉编译需要的依赖库
cp /home/test/crosstool-ng_lib/* /home/test/crosstool-ng_build/src
cd crosstool-ng_build/arm-unknown-linux-gnueabi/
ct-ng arm-unknown-linux-gnueabi
ct-ng menuconfig

弹出配置对话框 修改 Paths and misc options --->(${HOME}/src) Local tarballs directory :指定下载的源码保存的位置,根据自己情况修改为:/home/test/crosstool-ng_build/src

选中​ Forbid downloads:不从网络下载需要的源码包 因为自动下载速度太慢 自己在windows用迅雷下载更快些

Target options  --->Emit assembly for CPU 设置CPU内核 

例如 armv4可设置为arm920t    armv5可设置为arm1022e   armv6可设置为arm1136j-s

具体可参考:

ARM架构和ARM核区别和联系   ARM架构和ARM核区别和联系_whatday的专栏-CSDN博客_arm核

crosstool-ng配置中的Architecture level、Emit assembly for CPU 、Tune for CPU  crosstool-ng配置中的Architecture level、Emit assembly for CPU 、Tune for CPU_whatday的专栏-CSDN博客

选择GCC版本:C compiler  ---> gcc version (6.3.0)   

Operating System  --->Linux kernel version (3.19.8 (EOL))  ---> 

这个项是选择内核版本的 当然选择后在构建工具链时 需要提供相应的内核源码包

C-library  ---> Minimum supported kernel version (Same as kernel headers (default))  --->

使用gcc编译后的程序  能够运行的最低的内核版本 

假如gcc内核版本选择的3.19.8 系统环境是3.10.0 此时运行该gcc编译的程序会报错:FATAL: kernel too old

就是因为这个选项造成的  这里修改为 Let ./configure decide  就不会有这个问题了

3.使用ct-ng build命令开始制作交叉工具编译链。

这是使用root权限会提示错误[ERROR]  You must NOT be root to run crosstool-NG 

用普通用户运行 ct-ng build 因为没有需求的源码包且配置了不允许网络自动下载 所以会出现下列错误

[ERROR]  >>  Build failed in step 'Retrieving needed toolchain components' tarballs'
[ERROR]  >>        called in step '(top-level)'

cat build.log可以看到需求的源码包名

[DEBUG]    Trying to retrieve an already downloaded copy of 'autoconf-2.69'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.tar.xz'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.tar.lzma'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.tar.bz2'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.tar.gz'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.tgz'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.tar'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69.zip'
[DEBUG]    Trying '/home/test/Desktop/crosstool-ng_build/src/autoconf-2.69'
[DEBUG]    Not allowed to download from the Internet, aborting autoconf-2.69 download

在windows中google直接搜索autoconf-2.69.tar.xz 迅雷下载 传到centos7的${HOME}/crosstool-ng_build/src目录下 上 再次 ct-ng build 依次循环下载各个需要的包 列表如下:

[root@bogon crosstool-ng_build]# ll src/
total 283444
-rwxrwxrwx. 1 root root  1214744 Apr 24  2012 autoconf-2.69.tar.xz
-rwxrwxrwx. 1 root root  1496708 Feb 10 09:09 automake-1.15.tar.xz
-rwxrwxrwx. 1 root root 36228252 Feb 10 09:47 binutils-2.28.tar.gz
-rwxrwxrwx. 1 root root   240479 Feb 10 09:52 duma_2_5_15.tar.gz
-rwxrwxrwx. 1 root root   414352 Feb 10 10:22 expat-2.2.0.tar.bz2
-rwxrwxrwx. 1 root root 99903185 Feb 10 09:49 gcc-6.3.0.tar.bz2
-rwxrwxrwx. 1 root root 19225392 Feb 10 09:53 gdb-7.12.1.tar.xz
-rwxrwxrwx. 1 root root  7209808 Feb 10 09:44 gettext-0.19.8.1.tar.xz
-rwxrwxrwx. 1 root root 13873900 Feb 10 09:50 glibc-2.25.tar.xz
-rwxrwxrwx. 1 root root  1946336 Feb 10 09:22 gmp-6.1.2.tar.xz
-rwxrwxrwx. 1 root root  1449164 Feb 10 09:25 isl-0.16.1.tar.xz
-rwxrwxrwx. 1 root root   148529 Feb 10 09:26 libelf-0.8.13.tar.gz
-rwxrwxrwx. 1 root root  5264188 Feb 10 09:42 libiconv-1.15.tar.gz
-rwxrwxrwx. 1 root root   973080 Feb 16  2015 libtool-2.4.6.tar.xz
-rwxrwxrwx. 1 root root 94235192 Feb 10 09:19 linux-4.10.8.tar.xz
-rwxrwxrwx. 1 root root   482658 Feb 10 09:55 ltrace_0.7.3.orig.tar.bz2
-rwxrwxrwx. 1 root root   669925 Feb 10 09:25 mpc-1.0.3.tar.gz
-rwxrwxrwx. 1 root root  1126668 Feb 10 09:24 mpfr-3.1.5.tar.xz
-rwxrwxrwx. 1 root root  3131891 Feb 10 09:40 ncurses-6.0.tar.gz
-rwxrwxrwx. 1 root root   966668 Feb 10 09:57 strace-4.16.tar.xz

设置权限 chmod 777 -R ${HOME}/crosstool-ng_build/src 再次ct-ng build 经过一个小时的等待会出现完成提示 如下

[INFO ]  =================================================================
[INFO ]  Finalizing the toolchain's directory
[INFO ]    Stripping all toolchain executables
[EXTRA]    Installing the populate helper
[EXTRA]    Installing a cross-ldd helper
[EXTRA]    Creating toolchain aliases
[INFO ]  Finalizing the toolchain's directory: done in 1.51s (at 56:57)
[INFO ]  Build completed at 20190210.230611
[INFO ]  (elapsed: 56:56.82)
[INFO ]  Finishing installation (may take a few seconds)...
[56:57] / [test@bogon arm-unknown-linux-gnueabi]$ 

完成后,交叉工具编译链的位置在/home/tools/${CT_TARGET}目录下

[root@bogon bin]# pwd
/home/test/x-tools/arm-unknown-linux-gnueabi/bin
[root@bogon bin]# ll
total 30256
-r-xr-xr-x. 1 test test  841072 Feb 11 01:56 arm-unknown-linux-gnueabi-addr2line
-r-xr-xr-x. 2 test test  869656 Feb 11 01:56 arm-unknown-linux-gnueabi-ar
-r-xr-xr-x. 2 test test 1451520 Feb 11 01:56 arm-unknown-linux-gnueabi-as
-r-xr-xr-x. 2 test test  817280 Feb 11 01:56 arm-unknown-linux-gnueabi-c++
lrwxrwxrwx. 1 test test      29 Feb 11 01:46 arm-unknown-linux-gnueabi-cc -> arm-unknown-linux-gnueabi-gcc
-r-xr-xr-x. 1 test test  836528 Feb 11 01:56 arm-unknown-linux-gnueabi-c++filt
-r-xr-xr-x. 1 test test  817280 Feb 11 01:56 arm-unknown-linux-gnueabi-cpp
-r-xr-xr-x. 1 test test    3177 Feb 11 00:58 arm-unknown-linux-gnueabi-ct-ng.config
-r-xr-xr-x. 1 test test 2457632 Feb 11 01:56 arm-unknown-linux-gnueabi-dwp
-r-xr-xr-x. 1 test test   31552 Feb 11 01:56 arm-unknown-linux-gnueabi-elfedit
-r-xr-xr-x. 2 test test  817280 Feb 11 01:56 arm-unknown-linux-gnueabi-g++
-r-xr-xr-x. 2 test test  813184 Feb 11 01:56 arm-unknown-linux-gnueabi-gcc
-r-xr-xr-x. 2 test test  813184 Feb 11 01:56 arm-unknown-linux-gnueabi-gcc-6.3.0
-r-xr-xr-x. 1 test test   27152 Feb 11 01:56 arm-unknown-linux-gnueabi-gcc-ar
-r-xr-xr-x. 1 test test   27152 Feb 11 01:56 arm-unknown-linux-gnueabi-gcc-nm
-r-xr-xr-x. 1 test test   27152 Feb 11 01:56 arm-unknown-linux-gnueabi-gcc-ranlib
-r-xr-xr-x. 1 test test  429344 Feb 11 01:56 arm-unknown-linux-gnueabi-gcov
-r-xr-xr-x. 1 test test  388408 Feb 11 01:56 arm-unknown-linux-gnueabi-gcov-tool
-r-xr-xr-x. 1 test test 5295208 Feb 11 01:56 arm-unknown-linux-gnueabi-gdb
-r-xr-xr-x. 1 test test  903760 Feb 11 01:56 arm-unknown-linux-gnueabi-gprof
-r-xr-xr-x. 1 test test     143 Feb 11 01:07 arm-unknown-linux-gnueabi-ld
-r-xr-xr-x. 2 test test 1350528 Feb 11 01:56 arm-unknown-linux-gnueabi-ld.bfd
-r-xr-xr-x. 1 test test   15078 Feb 11 01:56 arm-unknown-linux-gnueabi-ldd
-r-xr-xr-x. 2 test test 4584576 Feb 11 01:56 arm-unknown-linux-gnueabi-ld.gold
-r-xr-xr-x. 2 test test  854064 Feb 11 01:56 arm-unknown-linux-gnueabi-nm
-r-xr-xr-x. 2 test test 1033232 Feb 11 01:56 arm-unknown-linux-gnueabi-objcopy
-r-xr-xr-x. 2 test test 1308880 Feb 11 01:56 arm-unknown-linux-gnueabi-objdump
-r-xr-xr-x. 1 test test   10448 Feb 11 01:56 arm-unknown-linux-gnueabi-populate
-r-xr-xr-x. 2 test test  869656 Feb 11 01:56 arm-unknown-linux-gnueabi-ranlib
-r-xr-xr-x. 2 test test  505760 Feb 11 01:56 arm-unknown-linux-gnueabi-readelf
-r-xr-xr-x. 1 test test  840912 Feb 11 01:56 arm-unknown-linux-gnueabi-size
-r-xr-xr-x. 1 test test  841072 Feb 11 01:56 arm-unknown-linux-gnueabi-strings
-r-xr-xr-x. 2 test test 1033232 Feb 11 01:56 arm-unknown-linux-gnueabi-strip
[root@bogon bin]# 

工具详细解释参考: Binutils工具集 GCC工具集介绍  Binutils工具集 GCC工具集介绍_whatday的专栏-CSDN博客_gcc工具集

出现以下信息说明交叉编译工具链有效

[root@bogon bin]# ./arm-unknown-linux-gnueabi-gcc -v
Using built-in specs.
COLLECT_GCC=./arm-unknown-linux-gnueabi-gcc
COLLECT_LTO_WRAPPER=/home/test/x-tools/arm-unknown-linux-gnueabi/libexec/gcc/arm-unknown-linux-gnueabi/6.3.0/lto-wrapper
Target: arm-unknown-linux-gnueabi
Configured with: /home/test/Desktop/crosstool-ng_build/arm-unknown-linux-gnueabi/.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-gnueabi --prefix=/home/test/x-tools/arm-unknown-linux-gnueabi --with-sysroot=/home/test/x-tools/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi/sysroot --enable-languages=c,c++ --with-float=soft --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/test/Desktop/crosstool-ng_build/arm-unknown-linux-gnueabi/.build/arm-unknown-linux-gnueabi/buildtools --with-mpfr=/home/test/Desktop/crosstool-ng_build/arm-unknown-linux-gnueabi/.build/arm-unknown-linux-gnueabi/buildtools --with-mpc=/home/test/Desktop/crosstool-ng_build/arm-unknown-linux-gnueabi/.build/arm-unknown-linux-gnueabi/buildtools --with-isl=/home/test/Desktop/crosstool-ng_build/arm-unknown-linux-gnueabi/.build/arm-unknown-linux-gnueabi/buildtools --enable-lto --enable-threads=posix --enable-target-optspace --enable-plugin --enable-gold --disable-nls --disable-multilib --with-local-prefix=/home/test/x-tools/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi/sysroot --enable-long-long
Thread model: posix
gcc version 6.3.0 (crosstool-NG crosstool-ng-1.23.0) 
[root@bogon bin]# 


标准化安装交叉编译器

由于标准Makefile需要实用标准的交叉编译器的名称,一般这个名称是arm-linux-gcc这样的  vim link.sh

#!/bin/sh
PREFIX=arm-unknown-linux-gnueabi- 
AFTFIX=arm-linux-
ln -s ${PREFIX}gcc ${AFTFIX}gcc
ln -s ${PREFIX}addr2line ${AFTFIX}addr2line
ln -s  ${PREFIX}gdbtui ${AFTFIX}gdbtui
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}gccbug ${AFTFIX}gccbug
ln -s  ${PREFIX}gcj ${AFTFIX}gcj
ln -s  ${PREFIX}gcov ${AFTFIX}gcov
ln -s  ${PREFIX}gdb ${AFTFIX}gdb
ln -s  ${PREFIX}gfortran ${AFTFIX}gfortran
ln -s  ${PREFIX}gprof ${AFTFIX}gprof
ln -s  ${PREFIX}jcf-dump ${AFTFIX}jcf-dump
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}run ${AFTFIX}run
ln -s  ${PREFIX}size ${AFTFIX}size
ln -s  ${PREFIX}strings ${AFTFIX}strings
ln -s  ${PREFIX}strip ${AFTFIX}strip

保存并执行  这样我们就得到了整个使用标准名称的交叉编译工具链

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值