【嵌入式】使用Cross Toolchain构建交叉工具链

Preface

   前面编译linux内核的时候,用各种cross版本都不行啊,真是纠结,于是就想着自己也要会编译交叉工具的方法,然后各种尝试,各种问题啊,最后还是没解决(还有其它事情),步骤我都走熟了,记下来吧


 

Preparation

root@lcw:/home/mystery/cross_toolchain# ls
binutils-2.15.tar.bz2  glibc-2.3.2.tar.bz2
crosstool-0.43         glibc-linuxthreads-2.3.2.tar.bz2
crosstool-0.43.tar.gz  linux-2.6.14.tar.bz2
gcc-4.1.0.tar.bz2      linux-libc-headers-2.6.12.0.tar.bz2

 

    后面还下载了gdb-6.5.tar.bz2

Step1
   当然是解压crosstool-0.42.tar.gz
Step2
   我们需要自己配置文件,我取名为arm.sh
<arm.sh>

#!/bin/sh
set -ex
TARBALLS_DIR=/home/mystery/cross_toolchain/crossTools # 定义工具链源码所存放位置。
RESULT_TOP=/opt/crosstool            # 定义工具链的安装目录
export TARBALLS_DIR RESULT_TOP
GCC_LANGUAGES="c,c++"                # 定义支持C, C++语言
export GCC_LANGUAGES
# 创建/opt/crosstool目录
mkdir -p $RESULT_TOP
# 编译工具链,该过程需要数小时完成。
eval 'cat arm.dat gcc-4.1.0-glibc-2.3.2.dat' sh all.sh --notest
echo Done.

 

 


Step3

我们还需要改二个配置文件gcc-4.1.0-glibc-2.3.2.dat与arm.dat,这个根据自己下载的文件需要来改

<gcc-4.1.0-glibc-2.3.2.dat>
BINUTILS_DIR=binutils-2.15
GCC_DIR=gcc-3.3.6
GLIBC_DIR=glibc-2.3.2
GLIBCTHREADS_FILENAME=glibc-linuxthreads-2.3.2
LINUX_DIR=linux-2.6.14
LINUX_SANITIZED_HEADER_DIR=linux-libc-headers-2.6.12.0

 

<arm.dat>

KERNELCONFIG='pwd'/arm.config # 内核的配置
TARGET=arm-linux-                # 编译生成的工具链名称
TARGET_CFLAGS="-O"                # 编译选项

 

 


Step4

 

   执行脚本,将Crosstool的脚本文件和配置文件准备好之后,开始执行arm.sh脚本来编译交叉编译工具。如果顺利的话,这步经过数个小时后,就能生成交叉编译工具了,数个小时啊,准备迎接各种error吧

 

<Question1>

+ abort Don't run all.sh or crosstool.sh as root, it's dangerous
+ echo Don't run all.sh or crosstool.sh as root, it's dangerous
Don't run all.sh or crosstool.sh as root, it's dangerous

 

<Solution>:这脚本够纠结的,用普通帐户时,执行命令又权限不够,用root帐户,又不让你运行,那么

   不用root帐户又有权限的办法就是chmod,自己给自己权限吧,我是chmod+chgrp+chown都改了
 

<Question2>

patching file linuxthreads/sysdeps/unix/sysv/linux/arm/sysdep-cancel.h 
grep: /home/mystery/cross_toolchain/crosstool-0.43/build/arm-linux/gcc-4.1.0-glibc-2.3.2/binutils-2.16.1/patches/README: No such file or directory 
+ test  =  
+ [ -d /opt/crosstool/gcc-4.1.0-glibc-2.3.2/arm-linux ] 
+ mkdir -p /opt/crosstool/gcc-4.1.0-glibc-2.3.2/arm-linux 
mkdir: cannot create directory `/opt/crosstool/gcc-4.1.0-glibc-2.3.2': Permission denied

 

Solution还是权限问题,opt目录是存放第三软件的目录,在这个目录下进行写操作需要root权限,但是又不能用root执行,和前面一样,自己给自己权限吧,我也是chgrp+chown+chmod都用了
mystery@lcw:/opt$ ls -l
total 36
drwxr-xr-x 2 root       root   4096 Apr  7 20:26 crosstool
drwxrwsr-x 9 messagebus users  4096 Feb 25 12:39 eclipse
drwxr-xr-x 3 root       root   4096 Apr  4 10:47 google
drwxr-xr-x 8        500   500  4096 Mar  1 19:36 jdk1.7.0_17
drwxr-xr-x 3 root       root   4096 Apr  4 11:25 kingsoft
drwx------ 2 root       root  16384 Apr  4 09:32 lost+found
mystery@lcw:/opt$ chgrp mystery crosstool/
chgrp: changing group of `crosstool/': Operation not permitted
mystery@lcw:/opt$ sudo chgrp mystery crosstool/
[sudo] password for mystery: 
mystery@lcw:/opt$ sudo chown mystery crosstool/
mystery@lcw:/opt$ ls -l
total 36
drwxr-xr-x 2 mystery    mystery  4096 Apr  7 20:26 crosstool
drwxrwsr-x 9 messagebus users    4096 Feb 25 12:39 eclipse
drwxr-xr-x 3 root       root     4096 Apr  4 10:47 google
drwxr-xr-x 8        500     500  4096 Mar  1 19:36 jdk1.7.0_17
drwxr-xr-x 3 root       root     4096 Apr  4 11:25 kingsoft
drwx------ 2 root       root    16384 Apr  4 09:32 lost+found

 

<Question3>

/home/mystery/cross_toolchain/crosstool-0.43/crosstool.sh: 110:
/home/mystery/cross_toolchain/crosstool-0.43/crosstool.sh: bison: not found 
crosstool: You don't have bison installed

 

Solution这种是最简单的,缺什么装什么,不过最好把这个记一下,不然以后装多了,你都不知道自己电脑上装了些什么

mystery@lcw:~/cross_toolchain/crosstool-0.43$ sudo apt-get install bison

 

<Question4>

oss_toolchain/crosstool-0.43/crosstool.sh: flex: not found
crosstool: You don't have flex installed

 

solution同上

mystery@lcw:~/cross_toolchain/crosstool-0.43$ sudo apt-get install flex

 

<Question5>

checking for pwd... /bin/pwd
checking for arm-linux-gcc... gcc
checking version of gcc... 4.6.3, bad
checking for gnumake... no
checking for gmake... no
checking for make... make
checking version of make... 3.81, ok
configure: error:
*** These critical programs are missing or too old: gcc
*** Check the INSTALL file for required versions
    意思是说,我系统的GCC版本太高了(4.6.3,目录最高版本,也是系统默认版本),版本太高了也不好啊,由于crosstools最后只支持gcc-4.1.0版本的,那我去下个GCC-4.1.0的吧。换了几个源,都没有找到4.1的,只好去网上找了
【嵌入式】使用Cross Toolchain构建交叉工具链
   然后就开始发装,各种依赖关系啊,各种装不上

mystery@lcw:~/Downloads$ dpkg -l "*gcc*" 
Desired=Unknown/Install/Remove/Purge/Hold 
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend 
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) 
||/ Name           Version        Description 
+++-==============-==============-============================================ 
ii  gcc            4:4.6.3-1ubunt GNU C compiler 
un  gcc-4.1        <none>         (no description available) 
ri  gcc-4.1-base   4.1.2-27ubuntu The GNU Compiler Collection (base package) 
un  gcc-4.1-doc    <none>         (no description available) 
un  gcc-4.1-locale <none>         (no description available) 
un  gcc-4.1-multil <none>         (no description available) 
un  gcc-4.3        <none>         (no description available) 
un  gcc-4.4        <none>         (no description available) 
un  gcc-4.4-base   <none>         (no description available) 
un  gcc-4.5        <none>         (no description available) 
un  gcc-4.5-base   <none>         (no description available) 
ii  gcc-4.6        4.6.3-1ubuntu5 GNU C compiler 
ri  gcc-4.6-base   4.6.3-1ubuntu5 GCC, the GNU Compiler Collection (base packa 
un  gcc-4.6-doc    <none>         (no description available) 
un  gcc-4.6-locale <none>         (no description available) 
un  gcc-4.6-multil <none>         (no description available) 
un  gcc-doc        <none>         (no description available) 
un  gcc-multilib   <none>         (no description available) 
ri  libgcc1        1:4.6.3-1ubunt GCC support library 
un  libgcc1-dbg    <none>         (no description available)

 

   最多装上了gcc-4.1,及base,后面因为依赖关系涉及到lib和libgcc,看网上说的,如果直接完全卸载

   这些东西可能会导致系统出错,纠结,那就老老实实地下个gcc-4.1.0的源码编译吧

 

 


Step5

 

   解压gcc-4.1.0的源码,然后执行

mystery@lcw:~/Downloads$ # ./configure --prefix=/opt/gcc-4.1&& make

 

   这里我也是将gcc安装到opt目录下的,相应权限问题如上面一样解决,还有新的问题等着呢

 

<Question6>

WARNING: `makeinfo' is missing on your system.  You should only need it if
         you modified a `.texi' or `.texinfo' file, or any other file
         indirectly affecting the aspect of the manual.  The spurious
         call might also be the consequence of using a buggy `make' (AIX,
         DU, IRIX).  You might want to install the `Texinfo' package or
         the `GNU make' package.  Grab either from any GNU archive site.

 

solution系统不包含“makeinfo”,好吧,安装就是了嘛。
   安装之后,再次编译,仍然如此,纠结了,老规矩,百度解决不了,找谷歌(身边没人玩这个啊。。。。。)

 

<Question7>

   原来是版本问题,不能识别

mystery@lcw:~/Downloads/gcc-4.1.0$ makeinfo --version
makeinfo (GNU texinfo) 4.13
   系统只支持4.2~4.9的版本,我4.13的版本,当然不支持啦
solution这个makeinfo的调用主要是在makefile里面实现的,现在我们需要在makefile里面找到这行代码
   然后改正过来
【嵌入式】使用Cross Toolchain构建交叉工具链
 
如上所示,makefile是通过扩展的正则表达式来匹配的(关于正则表达式,这里就不多说了啊),但是细心的朋友会看到,三千多行啊,其实这还没到这个文件的中间部分呢,怎么找呢?
    这里我比较幸运,刚好两步就找到了(修改这个文件也需要权限的哈):
第一步,进入makefile,L_Shift+G,然后搜索/texinfo
第二步,然后再向上搜索?texinfo,好了,就到了上图的位置

   然后就开始修改这个用于匹配的正则吧,按图上写的就可以了

 

 


Step6

 

   然后再次编译,看着不断变化的字符,慢慢等吧 过了十到二十分钟吧,一屏幕的错误,天啊

/usr/include/dlfcn.h:103: error: expected declaration specifiers or ‘...’ before ‘Dl_info’ 
/usr/include/dlfcn.h:104: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__THROW’ 
/usr/include/dlfcn.h:126: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__THROW’ 
/usr/include/dlfcn.h:176: error: storage class specified for parameter ‘Dl_serpath’ 
/usr/include/dlfcn.h:184: error: expected specifier-qualifier-list before ‘Dl_serpath’ 
/usr/include/dlfcn.h:185: error: storage class specified for parameter ‘Dl_serinfo’ 
/usr/include/dlfcn.h:189: error: expected declaration specifiers before ‘__END_DECLS’ 
In file included from ../.././gcc/crtstuff.c:92: 
/usr/include/link.h:35:63: error: bits/elfclass.h: No such file or directory 
/usr/include/link.h:36:23: error: bits/link.h: No such file or directory 
In file included from ../.././gcc/crtstuff.c:92: 
../.././gcc/crtstuff.c:239: error: storage class specified for parameter ‘__cxa_finalize’ 
../.././gcc/crtstuff.c:239: error: weak declaration of ‘__cxa_finalize’ must be public 
../.././gcc/crtstuff.c:262: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token 
../.././gcc/crtstuff.c:305: error: expected declaration specifiers before ‘asm’ 
../.././gcc/crtstuff.c:319: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token 
../.././gcc/crtstuff.c:345: error: expected declaration specifiers before ‘asm’ 
../.././gcc/crtstuff.c:345: error: old-style parameter declarations in prototyped function definition 
../.././gcc/crtstuff.c:345: error: expected ‘{’ at end of input 
make[2]: *** [crtbegin.o] Error 1 
make[2]: Leaving directory `/home/mystery/Downloads/gcc-4.1.0/host-i686-pc-linux-gnu/gcc' 
make[1]: *** [all-gcc] Error 2 
make[1]: Leaving directory `/home/mystery/Downloads/gcc-4.1.0' 
make: *** [all] Error 2

 

   省略了N行错误啊,找到最后几行

error: old-style parameter declarations in prototyped function definition
error: expected ‘{’ at end of input

 

   这个错误以前在电脑上代码时也遇见过,应该就是哪里少写了分号或者半角和全角的格式没有对
这是指这个源代码中有error吗?天啊,我是没那个能力在这里面去翻了,也没有时间啊,事情还多着呢。。。
   下面的步骤就有时间再验证吧


Step7

   make之后就是安装了,如果顺利的话make install就过去了

Step8

   如果以前有另外版本的gcc,这时只用把新安装的路径链接到gcc就可以了

ln -s /…/gcc-4.1/bin/gcc gcc4
ln -s /…/gcc-4.1/bin/g++ g++4

    如果没有的话,就需要自己修改环境变量了

 

 

感悟

 

这就是自学啊,遇到任何问题的时候,我身边根本没有人能和我讨论这方面的东西,这样有好处,也有坏处,好处当然是:锻炼自学能力和解决问题的能力,缺点就是:周期太长了啊
 
   不过很无奈的说:我也不想这样
    软件工程出身,对于软件方面来说,自己基础还算不错的,入驻嵌入式,就是对自己提出的一个更高的要求,这个要求就是打通软件和硬件的桥梁,做到一个完整的知识结构体系
因为我并不满足于计算机中的产品,我希望有能改变生活的产品
   开学的时候,我和老师请假去参加培训,但是因为才大三,老师不让我走,硬性条件让我只能再次自学……
    对于逛论坛这种事,对嵌入式来说,不像软件那么有意义,因为很多东西和自己配置的环境及硬件条件有关,别人的,只能是参考像这整个步骤,书上绝对是一路顺风走过来的
 
   出现问题了,百度一下,你遇到的问题,别人也遇到过,但是同样的问题,别人的解决方法,却不一定也适合你,就这么简单
    哎,事有轻重缓急,我还有其它事情,这问题就先放在这里了,我先拿cross-3.2的工具用着吧
 
记录里面也有一些问题的解决方法,算是教训吧

 

 
 

本文出自 “成鹏致远” 博客,请务必保留此出处http://infohacker.blog.51cto.com/6751239/1174081

原文链接: https://www.cnblogs.com/lcw/p/3159427.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    【嵌入式】使用Cross Toolchain构建交叉工具链

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/93746

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午2:18
下一篇 2023年2月10日 上午2:18

相关推荐