您的位置:首页 > 运维架构 > Linux

Linux交叉编译器的制作(二)

2018-03-13 15:12 183 查看

Linux交叉编译器的制作-crosstool-ng

1.交叉编译器的了解

1.1 GNU

GNU编译器集(其前身为GNU C编译器)诞生于1987年。当时Richard Stallman(GNU项

目的创办人)想要创建一个编译器,它可以满足他定义的“自由软件”概念,并可用来编译

GNU项目发布的其他软件。GNU C编译器迅速在自由软件社区中流行开来,而且以其健壮性和

可移植性而闻名。它已成为许多集成开发工具的基础,被世界各地的发行商应用在Linux和其他操作系统之上。

版本和学习内容,这里还有gcc参考手册链接

[klaus@localhost ~]$ gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。
[klaus@localhost ~]$


GNU工具链中包括编译器(cc)、连接器(ld)、汇编器(as)以及调试器(gdb,用于跟踪所有程序中的不可避免的错误)。

1.2 几种C语言库

1)Glibc

glibc = GNU C Library 是GNU项(GNU Project)目,所实现的 C语言标准库(C standard library)。在PC上安装的时候,默认都是使用glibc。

 常见的桌面和服务器中的GNU/Linux类的系统中,都是用的这套C语言标准库。其实现了常见的C库的函数,支持很多种系统平台,功能很全, 囊括了几乎所有的 UNIX 通行的标准,可以想见其内容包罗万有。

 glibc除了封装linux操作系统所提供的系统服务外,它本身也提供了许多其它一些必要功能服务的实现。

2)uclibc

一个小型的C语言标准库,主要用于嵌入式。其最开始设计用于uClinux(注:uClinux不支持MMU),uclibc是另一c运行库,与glibc相比他更小。当初创建uclinux的时候,需要一个能编译比较小体积的目标文件的编译器,这个时候就有人写了一个uc-libc库,这个库可以说是uclinux上的一个glibc移植,但是还是有很多函数没有实现,所以人们只能勉强用它来在uclinux上写程序。

uClibc的特点

uClibc比glibc要小很多。

uClibc是独立的,为了应用于嵌入式系统中,完全重新实现出来的。

glibc在源码结构和二进制上,都不兼容。

2.编译器下载

制作交叉编译器过程中,Linux主机下需要确定安装了以下软件包。我们可以在Linux主机下通过yum(centos等红帽子系列Linux发行版本下)或apt-get(ubuntu等Debian系列Linxu发行版本下)命令来从网上下载安装下面软件包。这里是下载地址

[klaus@localhost 下载]$ mkdir crosstool
[klaus@localhost 下载]$ cd crosstool/
[klaus@localhost crosstool]$ wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.16.0.tar.bz2 --2018-03-13 14:11:47--  http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.16.0.tar.bz2 正在解析主机 crosstool-ng.org (crosstool-ng.org)... 140.211.15.129
正在连接 crosstool-ng.org (crosstool-ng.org)|140.211.15.129|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1957336 (1.9M) [application/x-bzip]
正在保存至: “crosstool-ng-1.16.0.tar.bz2”
100%[===========================================================================================>] 1,957,336    213KB/s 用时 12s
2018-03-13 14:12:00 (158 KB/s) - 已保存 “crosstool-ng-1.16.0.tar.bz2” [1957336/1957336])
[klaus@localhost crosstool]$ tar -xjf crosstool-ng-1.16.0.tar.bz2
[klaus@localhost crosstool]$ cd crosstool-ng-1.16.0/
[klaus@localhost crosstool-ng-1.16.0]$ ls
bootstrap  configure     contrib  ct-ng.comp  docs     LICENSES    Makefile.in  README   scripts   TODO
config     configure.ac  COPYING  ct-ng.in    kconfig  licenses.d  patches      samples  steps.mk


3. 编译器安装

需要配置安装到当前目录下

[klaus@localhost crosstool-ng-1.16.0]$./configure --prefix=`pwd`
[klaus@localhost crosstool-ng-1.16.0]$make && make install
[klaus@localhost crosstool-ng-1.16.0]$ ls samples/
alphaev56-unknown-linux-gnu           i586-geode-linux-uclibc               powerpc64-unknown-linux-gnu
alphaev6
4000
7-unknown-linux-gnu           i586-mingw32msvc,i686-none-linux-gnu  powerpc-860-linux-gnu
arm-bare_newlib_cortex_m3_nommu-eabi  i686-nptl-linux-gnu                   powerpc-e300c3-linux-gnu
arm-cortex_a15-linux-gnueabi          i686-pc-mingw32                       powerpc-e500v2-linux-gnuspe
arm-cortex_a8-linux-gnueabi           m68k-unknown-elf                      powerpc-unknown-linux-gnu
arm-davinci-linux-gnueabi             mips64el-n32-linux-uclibc             powerpc-unknown-linux-uclibc
armeb-unknown-eabi                    mips64el-n64-linux-uclibc             powerpc-unknown_nofpu-linux-gnu
armeb-unknown-linux-gnueabi           mips-ar2315-linux-gnu                 s390-ibm-linux-gnu
armeb-unknown-linux-uclibcgnueabi     mipsel-sde-elf                        s390x-ibm-linux-gnu
arm-unknown-eabi                      mipsel-unknown-linux-gnu              samples.mk
arm-unknown-linux-gnueabi             mips-malta-linux-gnu                  sh4-unknown-linux-gnu
arm-unknown-linux-uclibcgnueabi       mips-unknown-elf                      x86_64-unknown-linux-gnu
avr32-unknown-none                    mips-unknown-linux-uclibc             x86_64-unknown-linux-uclibc
bfin-unknown-linux-uclibc             powerpc-405-linux-gnu
[klaus@localhost crosstool-ng-1.16.0]$


在模板里面找到arm-bare_newlib_cortex_m3_nommu-eabiarm-cortex_a8-linux-gnueabi就证明安装成功了。

4. 编译器配置

Cortex A8处理器为模块

[klaus@localhost crosstool-ng-1.16.0]$ cp samples/arm-cortex_a8-linux-gnueabi/crosstool.config .config
[klaus@localhost crosstool-ng-1.16.0]$ export TERM=vt100
[klaus@localhost crosstool-ng-1.16.0]$ ./ct-ng menuconfig


接下来就是对交叉编译器进行配置了

[b]4.1 Paths and misc options[/b]

设置下载路径和安装路径



[b]4.2 Target option[/b]

更改CPU型号



[b]4.3 Toolchain options[/b]

工具链修改名称



[b]4.4 不用修改项[/b]

下面的Companion libraries 也可以不用修改。



[b]4.5 取消项[/b]

Debug过程一般我们用不着,可以全部取消掉。



5.开始制作

创建制作交叉编译器工具链的软件包的下载路径就可以开始制作了

[klaus@localhost crosstool-ng-1.16.0]$ mkdir tarballa
[klaus@localhost crosstool-ng-1.16.0]$ ./c
config/        config.gen/    config.status  configure      contrib/       ct-ng
[klaus@localhost crosstool-ng-1.16.0]$ ./ct-ng build


6.出现问题和解决办法

(1)glibc-static没有安装

[klaus@localhost crosstool-ng-1.16.0]$ ./ct-ng build
[INFO ]  Performing some trivial sanity checks
[INFO ]  Build started 20180313.145946
[INFO ]  Building environment variables
[EXTRA]  Preparing working directories
[ERROR]  Static linking impossible on the host system 'x86_64-build_unknown-linux-gnu'
[00:00] / make: *** [build] 错误 1


为了不去挨个寻找,可以简便的全部装上

[klaus@localhost crosstool-ng-1.16.0]$ yum install glibc*
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.cqu.edu.cn
* extras: mirrors.nju.edu.cn
* updates: mirrors.nju.edu.cn
正在解决依赖关系
... ... ...
更新完毕:
glibc.x86_64 0:2.17-196.el7_4.2             glibc-common.x86_64 0:2.17-196.el7_4.2     glibc-devel.x86_64 0:2.17-196.el7_4.2
glibc-headers.x86_64 0:2.17-196.el7_4.2

完毕!
[klaus@localhost crosstool-ng-1.16.0]$ ./ct-ng build


(2)下载链接出错

[INFO ]  Retrieving needed toolchain components' tarballs
[EXTRA]    Retrieving 'linux-3.5'
[ERROR]
[ERROR]  >>
... ... ...
Trying 'http://ftp.kernel.org/pub/linux/kernel/v3.x/linux-3.5.tar.lzma'
[DEBUG]    ==> Executing: 'wget' '--passive-ftp' '--tries=3' '-nc' '--progress=dot:binary' '-T' '10' '-O' '/home/klaus/crosstool/crosstool-ng-1.16.0/.build/tarballs/linux-3.5.tar.lzma.tmp-dl' 'http://ftp.kernel.org/pub/linux/kernel/v3.x/linux-3.5.tar.lzma'
[ALL  ]    --2018-03-13 19:15:46--  http://ftp.kernel.org/pub/linux/kernel/v3.x/linux-3.5.tar.lzma [ALL  ]    Resolving ftp.kernel.org (ftp.kernel.org)... failed: Name or service not known.
[ALL  ]    wget: unable to resolve host address 'ftp.kernel.org'
... ... ...


重新下载这些安装包就行,这里就直接将出错里面的安装包列举出来,通过下面方式直接下载

[klaus@localhost crosstool-ng-1.16.0]$ cd tarballs/
[klaus@localhost tarballs]$ ls
[klaus@localhost tarballs]$ wget http://ftp.gnu.org/gnu/binutils/binutils-2.20.1a.tar.bz2 --2018-03-13 19:42:04--  http://ftp.gnu.org/gnu/binutils/binutils-2.20.1a.tar.bz2 正在解析主机 ftp.gnu.org (ftp.gnu.org)... 208.118.235.20, 2001:4830:134:3::b
正在连接 ftp.gnu.org (ftp.gnu.org)|208.118.235.20|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:16306586 (16M) [application/x-bzip2]
正在保存至: “binutils-2.20.1a.tar.bz2”

100%[==========================================================================================>] 16,306,586   236KB/s 用时 40s

2018-03-13 19:42:44 (402 KB/s) - 已保存 “binutils-2.20.1a.tar.bz2” [16306586/16306586])

[klaus@localhost tarballs]$ ls
binutils-2.20.1a.tar.bz2


可以直接参考下面有的安装包,自行下载

[klaus@localhost tarballs]$ wget http://ftp.gnu.org/gnu/gcc/gcc-4.4.6/gcc-4.4.6.tar.bz2 [klaus@localhost tarballs]$ wget http://ftp.gnu.org/gnu/glibc/glibc-ports-2.9.tar.bz2 [klaus@localhost tarballs]$ wget https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.5.tar.bz2 [klaus@localhost tarballs]$ wget http://gcc.parentingamerica.com/infrastructure/ppl-0.10.2.tar.gz [klaus@localhost tarballs]$ wget http://ftp.osuosl.org/pub/clfs/conglomeration/cloog-ppl/cloog-ppl-0.15.9.tar.gz [klaus@localhost tarballs]$ wget http://ftp.gnu.org/gnu/libc/glibc-2.9.tar.bz2 [klaus@localhost tarballs]$ wget http://ftp.gnu.org/pub/gnu/gmp/gmp-4.3.2.tar.bz2 [klaus@localhost tarballs]$ wget http://ftp.twaren.net/Unix/GNU/gnu/mpfr/mpfr-2.4.2.tar.xz [klaus@localhost tarballs]$ ls
binutils-2.20.1a.tar.bz2  gcc-4.4.6.tar.bz2  glibc-ports-2.9.tar.bz2  linux-3.5.tar.bz2  ppl-0.10.2.tar.gz
cloog-ppl-0.15.9.tar.gz   glibc-2.9.tar.bz2  gmp-4.3.2.tar.bz2        mpfr-2.4.2.tar.xz
[klaus@localhost tarballs]$ cd ..
[klaus@localhost crosstool-ng-1.16.0]$ ./ct-ng build


(3)MPFR版本过低

[INFO ]  Extracting and patching toolchain components
[ERROR]    configure.in:294: error: automatic de-ANSI-fication support has been removed
[ERROR]    aclocal: error: echo failed with exit status: 1
[ERROR]
[ERROR]  >>
[ERROR]  >>  Build failed in step 'Extracting and patching toolchain components'
[ERROR]  >>        called in step '(top-level)'
[ERROR]  >>
[ERROR]  >>  Error happened in: CT_DoExecLog[scripts/functions@172]
[ERROR]  >>        called from: do_mpfr_extract[scripts/build/companion_libs/mpfr.sh@37]
[ERROR]  >>        called from: main[scripts/crosstool-NG.sh@560]
[ERROR]  >>
[ERROR]  >>  For more info on this error, look at the file: 'build.log'
[ERROR]  >>  There is a list of known issues, some with workarounds, in:
[ERROR]  >>      'share/doc/crosstool-ng/ct-ng.1.16.0/B - Known issues.txt'
[ERROR]
[ERROR]  (elapsed: 0:04.44)
[00:04] / make: *** [build] 错误 1


升级MPFR,记得需要在选项里面修改过来



如果编译不通还可以尝试更改更高的版本,重新下载所要的安装包

[klaus@localhost tarballs]$ wget http://ftp.twaren.net/Unix/GNU/gnu/mpfr/mpfr-3.0.0.tar.xz[/code] 
(4)GMP版本过高

[INFO ]  =================================================================
[INFO ]  Installing PPL for host
[EXTRA]    Configuring PPL
[ERROR]    configure: error: Cannot find GMP version 4.1.3 or higher.
[ERROR]
[ERROR]  >>
[ERROR]  >>  Build failed in step 'Installing PPL for host'
[ERROR]  >>        called in step '(top-level)'
[ERROR]  >>
[ERROR]  >>  Error happened in: CT_DoExecLog[scripts/functions@172]
[ERROR]  >>        called from: do_ppl_backend[scripts/build/companion_libs/ppl.sh@82]
[ERROR]  >>        called from: do_ppl_for_host[scripts/build/companion_libs/ppl.sh@59]
[ERROR]  >>        called from: main[scripts/crosstool-NG.sh@598]
[ERROR]  >>
[ERROR]  >>  For more info on this error, look at the file: 'build.log'
[ERROR]  >>  There is a list of known issues, some with workarounds, in:
[ERROR]  >>      'share/doc/crosstool-ng/ct-ng.1.16.0/B - Known issues.txt'
[ERROR]
[ERROR]  (elapsed: 1:28.30)
[01:29] / make: *** [build] 错误 1


将GLooG/ppl version版本降低就可以解决该问题,同时注意在tarballs下载相应版本。



(5)未知错误–待解决

... ... ...
[INFO ]  =================================================================
[INFO ]  Installing PPL for host
[EXTRA]    Configuring PPL
[EXTRA]    Building PPL
[ERROR]    /home/klaus/crosstool/crosstool-ng-1.17.0/.build/src/ppl-0.10.2/src/Interval.defs.hh:451:85: error: 'f_info' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
[ERROR]    /home/klaus/crosstool/crosstool-ng-1.17.0/.build/src/ppl-0.10.2/src/Interval.defs.hh:390:85: error: 'f_info' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
[ERROR]    /home/klaus/crosstool/crosstool-ng-1.17.0/.build/src/ppl-0.10.2/src/Interval.defs.hh:432:60: error: 'f_info' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
[ERROR]    /home/klaus/crosstool/crosstool-ng-1.17.0/.build/src/ppl-0.10.2/src/Interval.defs.hh:492:60: error: 'f_info' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
[ERROR]    /home/klaus/crosstool/crosstool-ng-1.17.0/.build/src/ppl-0.10.2/src/Interval.defs.hh:390:85: error: 'f_info' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
[ERROR]    /home/klaus/crosstool/crosstool-ng-1.17.0/.build/src/ppl-0.10.2/src/Interval.defs.hh:451:85: error: 'f_info' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
[ERROR]    make[4]: *** [Polyhedron_widenings.lo] Error 1
[ERROR]    make[3]: *** [all] Error 2
[ERROR]    make[2]: *** [all-recursive] Error 1
[ERROR]    make[1]: *** [all] Error 2
[ERROR]
[ERROR]  >>
[ERROR]  >>  Build failed in step 'Installing PPL for host'
[ERROR]  >>        called in step '(top-level)'
[ERROR]  >>
[ERROR]  >>  Error happened in: CT_DoExecLog[scripts/functions@258]
[ERROR]  >>        called from: do_ppl_backend[scripts/build/companion_libs/ppl.sh@105]
[ERROR]  >>        called from: do_ppl_for_host[scripts/build/companion_libs/ppl.sh@59]
[ERROR]  >>        called from: main[scripts/crosstool-NG.sh@609]
[ERROR]  >>
[ERROR]  >>  For more info on this error, look at the file: 'build.log'
[ERROR]  >>  There is a list of known issues, some with workarounds, in:
[ERROR]  >>      'share/doc/crosstool-ng/ct-ng.1.17.0/B - Known issues.txt'
[ERROR]
[ERROR]  (elapsed: 3:13.50)
[03:14] / make: *** [build] 错误 2
[klaus@localhost crosstool-ng-1.17.0]$


关于这个问题,博主我弄了两天,还是没有发现解决方案,不知道各位能够解决的能人,遇到解决了还请留言。

我的推测是,博主我的虚拟机centos 7,在centos 6的版本是没有这个问题的。可能是gcc的原因,在其他网站上浏览到:



所以博主我这个的crosstool-ng差最后一步,暂时没能实现。

附上错误日志和解决办法,这是6.9版本错误日志,这是7.0版本错误日志
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: