编译crosstoolchain的详细记录

http://blog.chinaunix.net/uid-13075095-id-2907609.html
现在是一头雾水,感觉看的东西太散了,决定系统的学习一下,先搞定手动建立交叉编译环境再说,
正好找到了一篇The GNU toolchain for ARM targe HOWTO
仔细看了一遍总算有点头绪了,好吧一步步来吧,

1.建立了几个目录/usr/arm_tools/arm-linux/include


2.编译binutils
#./configure --target=arm-linux --prefix=/usr/arm_tools
#make && make install
安装成功。

3.安装linux-2.6.9头文件
#make ARCH=arm CROSS_COMPILE=arm-linux menuconfig
#cp -dR /usr/src/linux-2.6.x/include/asm-arm /usr/arm_tools/arm-linux/include/asm
#cp -dR LINUX-SOURCE-LOCATION/include/linux  /usr/arm_tools/arm-linux/include/linux

4.编译gcc-3.4.2
编辑 gcc/config/arm/t-linux
TARGET_LIBGCC2_CFLAGS = -fomit-frame-pointer -fPIC
改为
TARGET_LIBGCC2_CFLAGS = -fomit-frame-pointer -fPIC -Dinhibit_libc -D__gthr_posix_h.

#cd /usr/src/gcc-3.4.2
#./configure --prefix=/usr/arm_tools \
             --target=arm-linux \
             --enable-languages=c \
             --without-headers \
             --disable-threads
错误1:

checking host system type... ./config.guess: unable to guess system type

This script, last modified 2005-09-19, has failed to recognize
the operating system you are using. It is advised that you
download the most up to date version of the config scripts from

  http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/config/config/config.guess
and
  http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/config/config/config.sub

If the version you run (./config.guess) is already up to date, please
send the following data and any information you think might be
pertinent to in order to provide the needed
information to handle your system.

config.guess timestamp = 2005-09-19

uname -m = i686
uname -r = 2.6.13.4
uname -s = Linux
uname -v = #2 SMP Wed Oct 19 10:36:29 CST 2005

/usr/bin/uname -p =
/bin/uname -X     =

hostinfo               =
/bin/universe          =
/usr/bin/arch -k       =
/bin/arch              = i686
/usr/bin/oslevel       =
/usr/convex/getsysinfo =

UNAME_MACHINE = i686
UNAME_RELEASE = 2.6.13.4
UNAME_SYSTEM  = Linux
UNAME_VERSION = #2 SMP Wed Oct 19 10:36:29 CST 2005
configure: error: can not guess host type; you must specify one

这样的错误。
在网上找了一下有两种答案,一种是在configure后加上参数--host=i686-pc-linux-gnu
还有一种是说
can not guess host type; you must specify one
configure uses the config.guess script in the admin directory to determine your host type. config.guess expects the C compiler to be called cc, though. If your compiler is named differently you can either specify your compiler via the environment variable CC or create a symbolic link (e.g. ln -s /usr/local/bin/egcs /usr/local/bin/cc). Of course, in the latter example you should make sure that /usr/local/bin is in yourPATH in the first place.

我加上了--host参数但是出现

as: unrecognized option `-Qy'
*** The command 'gcc -o conftest -O2   conftest.c' failed.
*** You must set the environment variable CC to a working compiler.

从这里看来上面的错误是第个说法的情况
我试了试gcc -v可以用,export CC=gcc
但错误仍然
怀疑gcc有问题,
#gcc -o hello -O2 hello.c
as: unrecognized option `-Qy'
原来是AS的问题
#as -v
GNU assembler version 2.16 (arm-linux) using BFD version 2.16
哦它用了我刚装的binutils!

google as: unrecognized option `-Qy'
找到下面的答案

> Under Options for System V -Qy is defined as

> `-Qy'
>       Identify the versions of each tool used by the compiler, in a
>       `.ident' assembler directive in the output.

> I'm not sure why a System V option is used.  I have not specified System V
> anywhere.  ???

 Linux is nearer System V than BSD and its GCC specs will give the '-Qy' option
to 'as' when compiling :

----------------------- clip -----------------------------------------------
 e:\usr\local\lib\gcc-lib\i486-linux-gnu\2_9-edk1.0-2\..\..\..\..\i486-linux-gnu
\bin\as.exe -V -Qy -o D:\temp\ccysaaaa.o D:\temp\ccqmaaaa.s
GNU assembler version 000704 (i486-linux-gnu) using BFD version 000704
----------------------- clip -----------------------------------------------

 And the linux-targeted 'as' will take it without vomiting it...

 The error message says that the native 'as' was found, otherwise something like:

  /usr/local/i686-pc-linux-gnu/bin/as: unrecognized option `-Qy'

would have been there...

 The GCC option '-print-search-dirs' shows the search paths for 'programs' and
your Linux-targeted 'as' must be in these directories, otherwise the native 'as'
will be used. Writing the command './xgcc -print-search-dirs' in the 'gcc' build
subdir should be quite normal practice when any install problems arise...

 And the '-v' option to see the given options to 'cpp', 'cc1', 'as' and 'ld', like:

   make CFLAGS="-v -O"

 The given clip was taken from an output got via using '-v'...

 For linking problems the '-verbose' option to the GNU linker is useful :

   make CFLAGS="-Wl,-verbose -O"

 And not forgetting the '--help' option for all GNU tools to give quick on-line
info about the available options :

   ./as --help

 When building and installing binutils, they will be put into the $prefix/bin
with the '$target-' name-prefix and into the '$prefix/$target/bin' with the
base names. In your case with your '--prefix=$prefix and '--target=$target'
settings the directories are:

 /usr/local/bin

and

 /usr/local/i686-pc-linux-gnu/bin

 Because no '--prefix=something' was given, the default prefix, '/usr/local'
was used.

Cheers, Kai

应该是我刚装的binutils 与系统原来的冲突
试了一下arm-linux-as -v
bash: arm-linux-as: command not found
原来环境变量设错了。把/usr/arm_tools/arm-linux/bin 加到PATH里去了,那里面正好有as  ,ar, ld,objcopy等
改回来改成/usr/arm_tools/bin

#./configure --prefix=/usr/arm_tools \
             --target=arm-linux \
             --enable-languages=c \
             --without-headers \
             --disable-threads \
        
#make

错误2:

xgcc: and: 没有那个文件或目录
网上也找不到,只有不断的试着变换参数。加上一个--with-newlib
不行,加上--with-headers=/usr/arm_tools/arm-linux/include
错误更多
看到有的在编译的时候有一个参数--disable-shared
加上去
没错误了,我都不敢相信是不是已经编译成功了,
#make install
/usr/arm_tools/bin下已经有gcc 了
应该是成功了吧,
先不管,继续下一步,等做完了看能不能编译hello就知道是不是成功了。

5.编glibc
#export CC=arm-linux-gcc
#../glibc-2.3.5/configure --prefix=/usr/arm_tools/arm-linux --host=arm-linux --build=i686-pc-linux-gnu --with-headers=/usr/arm_tools/arm-linux/include --enable-add-ons=linuxthreads



错误1:configure: error: compiler support for __thread is required
查到如下的内容,
> First of all,
> in libc's main configure script at about line 4040, there's the check
> for gcc > 3.2.0.
>
> Ignoring this, NPTL complains about __thread not being supported by the
> compiler:
>
> running configure fragment for nptl/sysdeps/pthread
> configure: error: compiler support for __thread is required
>
> "--without-__thread" didn't help as advertised, so I just removed the
> "nptl" and "nptl_db" directories for now...

有说删除glibc-2.3.5/nptl
我没有删,只是在enable-add-ons后加了一个“=linuxthreads"
就是说我只要这一个附加项,其它的不安装



错误2:
/usr/arm_tools/arm-linux/include/asm/errno.h:4:31: asm-generic/errno.h: 没有那个文件或目录

把内核头文件里的asm-generic拷贝到/usr/arm_tools/arm-linux/include 下

错误3:
/build_glibc/misc/ioperm.o.dt -MT /usr/src/build_glibc/misc/ioperm.o
../sysdeps/unix/sysv/linux/arm/ioperm.c: In function `init_iosys':
../sysdeps/unix/sysv/linux/arm/ioperm.c:103: error: `BUS_ISA' undeclared (first use in this function)
../sysdeps/unix/sysv/linux/arm/ioperm.c:103: error: (Each undeclared identifier is reported only once
../sysdeps/unix/sysv/linux/arm/ioperm.c:103: error: for each function it appears in.)
../sysdeps/unix/sysv/linux/arm/ioperm.c:103: error: initializer element is not constant
../sysdeps/unix/sysv/linux/arm/ioperm.c:103: error: (near initialization for `iobase_name[1]')
../sysdeps/unix/sysv/linux/arm/ioperm.c:104: error: initializer element is not constant
../sysdeps/unix/sysv/linux/arm/ioperm.c:104: error: (near initialization for `ioshift_name[1]')
make[2]: *** [/usr/src/build_glibc/misc/ioperm.o] 错误 1

原因:
Description: 
# Kernel headers have changed to avoid confusion with BUS_ISA in input.h.
# State:
# Kernels > 2.4.24 and > 2.6 have a CTL_BUS_ISA definition. Older kernels should not be used anyway :-) 

直接修改了一下ioperm.c把BUS_ISA改为CTL_BUS_ISA,或应用那个patch 

错误4:
/usr/arm_tools/lib/gcc/arm-linux/3.4.2/../../../../arm-linux/bin/ld: cannot find -lgcc_eh
collect2: ld returned 1 exit status
make[2]: *** [/usr/src/build_glibc/iconv/iconvconfig] 错误 1

找到下面的答案

On Thu, Sep 11, 2003 at 04:36:21PM -0400, Daniel Jacobowitz wrote:
> > Why exactly do you need that?
> > What exact library needs the functions from libgcc_eh.a?
> > There shouldn't be any these days (__register_*/__frame_state_for etc.
> > functions should come from sysdeps/generic on arches which need them,
> > _Unwind_* come from unwind*.c).
> > IMHO both:
> > 
> > # Force the backward compatibility EH functions to be linked.
> > LDFLAGS-c.so += -u __register_frame
> > 
> > in Makerules and -lgcc_eh in Makeconfig should go.

> If you say so.  That -lgcc_eh's been in and out quite a number of
> times.  I was just assuming that it was necessary.

I have been testing following patch on hammer.
linuxthreads built just fine and NPTL libraries too,
and in both cases libraries had the same exported symbols as without
the patch (though its .text slightly shrunk because it no longer contained
unneeded libgcc_eh routines (and also libc.so has fewer .plt slots).
Unfortunately, sln in NPTL build doesn't link, as libgcc_eh.a is needed
in this case.
So, IMHO we need my Makerules change and Daniel's patch.

2003-09-11  Jakub Jelinek 

    #* Makerules (LDFLAGS-c.so): Remove -u __register_frame.
    * Makeconfig (gnulib): Remove -lgcc_eh.

# Ulrich already applied this.
#--- libc/Makerules.jj    2003-07-23 03:56:16.000000000 -0400
#+++ libc/Makerules    2003-09-11 16:22:44.000000000 -0400
#@@ -572,8 +572,6 @@ LDFLAGS-c.so = -nostdlib -nostartfiles
# LDLIBS-c.so += $(gnulib)
# # Give libc.so an entry point and make it directly runnable itself.
# LDFLAGS-c.so += -e __libc_main
#-# Force the backward compatibility EH functions to be linked.
#-LDFLAGS-c.so += -u __register_frame
# # Pre-link the objects of libc_pic.a so that we can locally resolve
# # COMMON symbols before we link against ld.so.  This is because ld.so
# # contains some of libc_pic.a already, which will prevent the COMMONs
--- libc/Makeconfig.jj    2003-07-22 08:25:32.000000000 -0400
+++ libc/Makeconfig    2003-09-11 16:23:08.000000000 -0400
@@ -505,7 +505,7 @@ link-libc-bounded = $(common-objpfx)libc
 link-extra-libs-bounded = $(foreach lib,$(LDLIBS-$(@F:%-bp=%)),$(common-objpfx)$(lib)_b.a)
 
 ifndef gnulib
-gnulib := -lgcc -lgcc_eh
+gnulib := -lgcc
 endif
 ifeq ($(elf),yes)
 +preinit = $(addprefix $(csu-objpfx),crti.o)

2005.10.23

我直接修改了makeconfig,把-lgcc_eh删掉了。或把这个做为patch应用一下

错误5:

../sysdeps/generic/s_fmax.c: In function `__fmax':
../sysdeps/generic/s_fmax.c:28: internal compiler error: in elim_reg_cond, at flow.c:3257
Please submit a full bug report,
with preprocessed source if appropriate.
See for instructions.
make[2]: *** [/usr/src/build_glibc/math/s_fmax.o] 错误 1
make[2]: Leaving directory `/usr/src/glibc-2.3.5/math'

找到这样的答案

[ rediffed against gcc-3.4.1, with elbow grease, ending up with same thing as
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/flow.c.diff?cvsroot=gcc&only_with_tag=csl-arm-branch&r1=1.563.4.2&r2=1.563.4.3 ]

--- gcc-3.4.1/gcc/flow.c.old    2004-02-27 19:39:19.000000000 -0800
+++ gcc-3.4.1/gcc/flow.c    2004-08-26 07:29:46.000000000 -0700
@@ -1878,6 +1878,7 @@
       rtx set_src = SET_SRC (pc_set (BB_END (bb)));
       rtx cond_true = XEXP (set_src, 0);
       rtx reg = XEXP (cond_true, 0);
+       enum rtx_code inv_cond;
 
       if (GET_CODE (reg) == SUBREG)
         reg = SUBREG_REG (reg);
@@ -1886,11 +1887,13 @@
          in the form of a comparison of a register against zero.  
          If the condition is more complex than that, then it is safe
          not to record any information.  */
-      if (GET_CODE (reg) == REG
+       inv_cond = reversed_comparison_code (cond_true, BB_END (bb));
+       if (inv_cond != UNKNOWN
+          && GET_CODE (reg) == REG
           && XEXP (cond_true, 1) == const0_rtx)
         {
           rtx cond_false
-        = gen_rtx_fmt_ee (reverse_condition (GET_CODE (cond_true)),
+        = gen_rtx_fmt_ee (inv_cond,
                   GET_MODE (cond_true), XEXP (cond_true, 0),
                   XEXP (cond_true, 1));
           if (GET_CODE (XEXP (set_src, 1)) == PC)

但是改了没用,好像更这个没关吧,它在gcc-3.4.2目录里改了肯定也是跟这个没关系的



于是想重新编译一下GCc,给Gcc打了一个补丁,升级到3.4.3,但是这时候现再在Make发现
ar rc libiberty.a \
  regex.o cplus-dem.o cp-demangle.o cp-demint.o md5.o alloca.o argv.o choose-temp.o concat.o dyn-string.o fdmatch.o fibheap.o floatformat.o fnmatch.o getopt.o getopt1.o getpwd.o getruntime.o hashtab.o hex.o lbasename.o lrealpath.o make-relative-prefix.o make-temp-file.o objalloc.o obstack.o partition.o physmem.o pex-unix.o safe-ctype.o sort.o spaces.o splay-tree.o strerror.o strsignal.o ternary.o xatexit.o xexit.o xmalloc.o xmemdup.o xstrdup.o xstrerror.o  mkstemps.o
ar: argv.o: 没有那个文件或目录
找不到这个文件了。肯定是升了级的原因,Make uninstall
也不行了,说 `LDFLAGS' was not set in the previous run

2005.10.24

没办法,重来。把gcc-3.4.2全部删掉,重新解压,打上补丁,重新编译。这回我学乖了另外建了一个目录build_gcc来编译.
一路顺利
make 
make install
然后接着编译glibc
但是还是到第五个错误的时到停住了,

在网上找到了这个bug,http://gcc.gnu.org/PR15068
就像上面说的那样要修改flow.c。只有试试修改flow.c后再编译一遍gcc.

删掉GCC,打上flow.c的patch,重新编译GCC,
#../gcc-3.4.2/configure      --prefix=/usr/arm_tools \
                     --target=arm-linux \
                     --enable-languages=c \
                --without-headers \
                --with-newlib \
                --disable-threads \
                --disable-shared
#make
#make install
一切顺利,到这里才知道只要加上了--with-newlib 就不用改t-linux,这样要方便多了,要不然下面编glibc时还要改回来。
#cd ../build-glibc
#export CC=arm-linux-gcc
#../glibc-2.3.5/configure --prefix=/usr/arm_tools/arm-linux \
                --host=arm-linux \
                --build=i686-pc-linux-gnu \
                --with-headers=/usr/arm_tools/arm-linux/include \
                --enable-add-ons=linuxthreads
#make
#make install
这个glibc花了大概50分钟
终于成功了,我都不知道用什么来形容了。编译完了回头看一下很简单,就这么十几行的配置,
但是出这个结果时已经花了我两天时间了。

下面再来增加GCC对C++的支持。
#unset CC
#cd ../build_gcc
#../gcc-3.4.2/configure      --prefix=/usr/arm_tools\
                --target=arm-linux \
                --enable-languages=c,c++ \
                --without-headers \
                --disable-shared
#make 
#make install

OK大功告成!!!!!!!!!!!
试一试编一个hello.c
 也OK了。

你可能感兴趣的:(linux/ubuntu)