交叉编译(gcc 2.95.3)

来源:互联网 发布:twilight软件 编辑:程序博客网 时间:2024/05/21 16:59

1、创建目录

[fedora@localhost lqm]$ pwd
/home/fedora/lqm
[fedora@localhost lqm]$ mkdir -p ./embedded-system/{setup-dir,src-dir,kernel,build-dir,tool-chain,program,doc
}
[fedora@localhost lqm]$


2、下载相关数据

[fedora@localhost setup-dir]$ wget ftp://ftp.gnu.org/gnu/binutils/binutils-2.11.2.tar.gz
[fedora@localhost setup-dir]$ wget ftp://ftp.gnu.org/gnu/gcc/gcc-2.95.3/gcc-core-2.95.3.tar.gz
[fedora@localhost setup-dir]$ wget ftp://ftp.gnu.org/gnu/gcc/gcc-2.95.3/gcc-g++-2.95.3.tar.gz
[fedora@localhost setup-dir]$ wget ftp://ftp.gnu.org/gnu/glibc/glibc-2.2.4.tar.gz
[fedora@localhost setup-dir]$ wget ftp://ftp.gnu.org/gnu/glibc/glibc-linuxthreads-2.2.4.tar.gz
[fedora@localhost setup-dir]$ wget ftp://ftp.kernel.org/pub/linux/kernel/v2.4/linux-2.4.21.tar.gz
[fedora@localhost setup-dir]$ wget ftp://ftp.arm.linux.org.uk/pub/linux/arm/kernel/v2.4/patch-2.4.21-rmk1.gz
[fedora@localhost setup-dir]$ ls
binutils-2.11.2.tar.gz  gcc-g++-2.95.3.tar.gz  glibc-linuxthreads-2.2.4.tar.gz  patch-2.4.21-rmk1.gz
gcc-core-2.95.3.tar.gz  glibc-2.2.4.tar.gz     linux-2.4.21.tar.gz
[fedora@localhost setup-dir]$


3、定义环境变量

[fedora@localhost build-dir]$ cd ../doc
[fedora@localhost scripts]$ vi env

[fedora@localhost scripts]$ cat -n env

     1  export PRJROOT=/home/lqm/embedded-system
     2  export TARGET=arm-linux
     3  export PREFIX=$PRJROOT/tool-chain
     4  export TARGET_PREFIX=$PREFIX/$TARGET
     5  export PATH=$PREFIX/bin:$PATH
[fedora@localhost scripts]$ . env

[fedora@localhost embedded-system]$


4、内核头文件配置

[fedora@localhost embedded-system]$ cd kernel/
[fedora@localhost kernel]$ #内核头文件的配置
[fedora@localhost kernel]$ tar zxvf ../setup-dir/linux-2.4.21.tar.gz
[fedora@localhost kernel]$ gunzip ../setup-dir/patch-2.4.21-rmk1.gz
[fedora@localhost kernel]$ cd linux-2.4.21/
[fedora@localhost linux-2.4.21]$ patch -p1 < ../../setup-dir/patch-2.4.21-rmk1
[fedora@localhost linux-2.4.21]$ make ARCH=arm CROSS_COMPILE=arm-linux-menuconfig #配置
[fedora@localhost linux-2.4.21]$ make dep
[fedora@localhost linux-2.4.21]$ make distclean
[fedora@localhost linux-2.4.21]$ make ARCH=arm CROSS_COMPILE=arm-linux- menuconfig #配置:menuconfig和前面部分是有空格隔开的
[fedora@localhost linux-2.4.21]$ make dep
[fedora@localhost linux-2.4.21]$ ls include/linux/version.h
include/linux/version.h
[fedora@localhost linux-2.4.21]$ ls include/linux/auto
ls: 无法访问 include/linux/auto: 没有那个文件或目录
[fedora@localhost linux-2.4.21]$ ls include/linux/autoconf.h
include/linux/autoconf.h
[fedora@localhost linux-2.4.21]$
[fedora@localhost linux-2.4.21]$ cd include/
[fedora@localhost include]$ ln -s asm-arm asm
[fedora@localhost include]$ ls
asm        asm-arm   asm-generic  asm-ia64  asm-mips    asm-parisc  asm-ppc64  asm-s390x  asm-sparc    asm-x86_64  math-emu  pcmcia  video
asm-alpha  asm-cris  asm-i386     asm-m68k  asm-mips64  asm-ppc     asm-s390   asm-sh     asm-sparc64  linux       net       scsi
[fedora@localhost include]$ cd asm
[fedora@localhost asm]$ ln -s arch-epxa/ arch
[fedora@localhost asm]$ ln -s proc-armv proc
[fedora@localhost asm]$ mkdir -p $TARGET_PREFIX/include
[fedora@localhost asm]$ cp -r $PROJROOT/kernel/linux-2.4.21/include/linux $TARGET_PREFIX/include
cp: 无法 stat “/kernel/linux-2.4.21/include/linux”: 没有那个文件或目录
[fedora@localhost asm]$ cp -r $PRJROOT/kernel/linux-2.4.21/include/linux $TARGET_PREFIX/include
[fedora@localhost asm]$ cp -r $PRJROOT/kernel/linux-2.4.21/include/asm-arm  $TARGET_PREFIX/include/asm
[fedora@localhost asm]$
[fedora@localhost src-dir]$ #binutils' setup
[fedora@localhost src-dir]$ cd $PRJROOT/src-dir
[fedora@localhost src-dir]$ tar zxvf ../setup-dir/binutils-2.11.2.tar.gz
[fedora@localhost src-dir]$ cd $PRJROOT/build-dir/build-binutils
[fedora@localhost build-binutils]$
[fedora@localhost build-binutils]$ ../../src-dir/binutils-2.11.2/configure --target=$TARGET --prefix=$PREFIX
[fedora@localhost build-binutils]$ ../../src-dir/binutils-2.11.2/configure --target=$TARGET --prefix=$PREFIX
expr: 语法错误
Configuring for a i686-pc-linux-gnu host.
config.sub: missing argument
Try `config.sub --help' for more information.
Unrecognized target system name .
[fedora@localhost build-binutils]$ echo $TARGET

[fedora@localhost build-binutils]$ . /home/fedora/lqm/embedded-system/doc/scripts/hjbl
[fedora@localhost build-binutils]$ echo $TARGET

[fedora@localhost build-binutils]$ vi /home/fedora/lqm/embedded-system/doc/scripts/hjbl
[fedora@localhost build-binutils]$ cat -n /home/fedora/lqm/embedded-system/doc/scripts/hjbl
     1  export PRJROOT=/home/fedora/lqm/embedded-system
     2  export TARGET=arm-linux  #原来错成targeg了
     3  export PREFIX=$PRJROOT/tool-chain
     4  export TARGET_PREFIX=$PREFIX/$TARGET
     5  export PATH=$PREFIX/bin:$PATH
[fedora@localhost build-binutils]$
[fedora@localhost build-binutils]$ . /home/fedora/lqm/embedded-system/doc/scripts/hjbl
[fedora@localhost build-binutils]$ echo $TARGET
arm-linux
[fedora@localhost build-binutils]$
[fedora@localhost build-binutils]$ ../../src-dir/binutils-2.11.2/configure --target=$TARGET --prefix=$PREFIX
[fedora@localhost build-binutils]$ make
[fedora@localhost build-binutils]$ make install
[fedora@localhost build-binutils]$ ls $PREFIX/bin #验证安装
arm-linux-addr2line  arm-linux-as       arm-linux-gasp  arm-linux-nm       arm-linux-objdump  arm-linux-readelf  arm-linux-strings
arm-linux-ar         arm-linux-c++filt  arm-linux-ld    arm-linux-objcopy  arm-linux-ranlib   arm-linux-size     arm-linux-strip
[fedora@localhost build-binutils]$


5、配置并编译gcc源代码,生成gcc编译器

[john@localhost build-binutils]$ # 6
[john@localhost build-binutils]$ cd $PRJROOT/setup-dir
[john@localhost setup-dir]$ mv gcc-core-2.95.3.tar.gz gcc-2.95.3.tar.gz #rename
[john@localhost setup-dir]$ ls
binutils-2.11.2.tar.gz  glibc-2.2.4.tar.gz               patch-2.4.21-rmk1
gcc-2.95.3.tar.gz       glibc-linuxthreads-2.2.4.tar.gz
gcc-g++-2.95.3.tar.gz   linux-2.4.21.tar.gz
[john@localhost setup-dir]$ cd $PRJROOT/src-dir
[john@localhost src-dir]$ tar zxvf ../setup-dir/gcc-2.95.3.tar.gz
[john@localhost build-gcc]$ ../../src-dir/gcc-2.95.3/configure --target=$TARGET --prefix=$PREFIX --without-headers --enable-languages=c
[john@localhost build-gcc]$ vi ../../src-dir/gcc-2.95.3/gcc/config/arm/t-linux
[john@localhost build-gcc]$ #modified :TARGET_LIBGCC2_CFLAGS = -fomit-frame-pointer -fPIC -Dinhibit_libc -D__gthr_posix_h
[john@localhost build-gcc]$ make
../../../src-dir/gcc-2.95.3/gcc/config/arm/arm.c: 在函数‘arm_override_options’中:
../../../src-dir/gcc-2.95.3/gcc/config/arm/arm.c:286: 警告:赋值丢弃了指针目标类型的限定
../../../src-dir/gcc-2.95.3/gcc/config/arm/arm.c:530: 错误:赋值运算的左操作数必须是左值
# arm_prog_mode = TARGET_APCS_32 ? PROG_MODE_PROG32 : PROG_MODE_PROG26;

#slove this problem:
#http://lists.freebsd.org/pipermail/freebsd-ports-bugs/2007-October/129673.html
make[1]: *** [arm.o] 错误 1
make[1]: Leaving directory `/home/john/embedded-system/build-dir/build-gcc/gcc'
make: *** [all-gcc] 错误 2
[john@localhost build-gcc]$
#slove this problem:
#http://lists.freebsd.org/pipermail/freebsd-ports-bugs/2007-October/129673.html
[john@localhost build-gcc]$ make
#pass
[john@localhost build-gcc]$ make install


6、配置glibc编译生成glibc的c函数库

[john@localhost ~]$ #7
[john@localhost ~]$ cd embedded-system/
[john@localhost embedded-system]$ . doc/scripts/hjbl
[john@localhost embedded-system]$ cd $PRJROOT/src-dir
[john@localhost src-dir]$ tar zxvf ../setup-dir/glibc-2.2.4.tar.gz
[john@localhost src-dir]$ tar zxvf ../setup-dir/glibc-linuxthreads-2.2.4.tar.gz --directory=glibc-2.2.4
[john@localhost src-dir]$ cd $PRJROOT/build-dir/build-glibc
[john@localhost build-glibc]$ CC=arm-linux-gcc ../../src-dir/glibc-2.2.4/configure --host=$TARGET --prefix="/usr" --enable-add-ons --with-headers=$TARGET_PREFIX/include
[john@localhost build-glibc]$ make
[john@localhost build-glibc]$ #make install_root=$TARGET_PREFIX prefix=""install #error:there is a blank before the last word --install
[john@localhost build-glibc]$ make install_root=$TARGET_PREFIX prefix="" install
[john@localhost build-glibc]$ cd $TARGET_PREFIX/lib
[john@localhost lib]$ ls
[john@localhost lib]$ cp ./libc.so ./libc.so.orig
[john@localhost lib]$
[john@localhost lib]$ vi libc.so
[john@localhost lib]$ cat -n lib.so
cat: lib.so: 没有那个文件或目录
[john@localhost lib]$ cat -n libc.so
     1    /* GNU ld script
     2       Use the shared library, but some functions are only in
     3       the static library, so try that secondarily.  */
     4    /*before modify:  GROUP ( /lib/libc.so.6 /lib/libc_nonshared.a )*/
     5     GROUP ( libc.so.6 libc_nonshared.a )
[john@localhost lib]$


7、生成c++语言编译器

[john@localhost lib]$ # 8
[john@localhost lib]$ cd $PRJROOT/build-dir/build-gcc
[john@localhost build-gcc]$ ../../src-dir/gcc-2.95.3/configure  --target=$TARGET --prefix=$PREFIX --enable-languages=c,c++
[john@localhost build-gcc]$ make all
[john@localhost build-gcc]$ make install

************************************8.验证并修改部分选项*******************************************************

[john@localhost build-gcc]$
[john@localhost build-gcc]$ # 9
[john@localhost build-gcc]$ cd $TARGET_PREFIX/bin

[john@localhost build-gcc]$ cd $TARGET_PREFIX/bin
[john@localhost bin]$ file as ar gcc ld nm ranlib strip
as:     ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
ar:     ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
gcc:    ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
ld:     ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
nm:     ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
ranlib: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
strip:  ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
[john@localhost bin]$
[john@localhost bin]$ arm-linux-gcc  -print-search-dirs
install: /home/john/embedded-system/tool-chain/lib/gcc-lib/arm-linux/2.95.3/
programs: /home/john/embedded-system/tool-chain/lib/gcc-lib/arm-linux/2.95.3/:/home/john/embedded-system/tool-chain/lib/gcc-lib/arm-linux/:/usr/lib/gcc/arm-linux/2.95.3/:/usr/lib/gcc/arm-linux/:/home/john/embedded-system/tool-chain/arm-linux/bin/arm-linux/2.95.3/:/home/john/embedded-system/tool-chain/arm-linux/bin/
libraries: /home/john/embedded-system/tool-chain/lib/gcc-lib/arm-linux/2.95.3/:/usr/lib/gcc/arm-linux/2.95.3/:/home/john/embedded-system/tool-chain/arm-linux/lib/arm-linux/2.95.3/:/home/john/embedded-system/tool-chain/arm-linux/lib/
[john@localhost bin]$
[john@localhost bin]$ mv as ar gcc ld nm ranlib strip $PREFIX/lib/gcc-lib/arm-linux/2.95.3
[john@localhost bin]$ man file
[john@localhost bin]$ for file in as ar gcc ld nm ranlib strip
> do
> ln -s $PREFIX/lib/gcc-lib/arm-linux/2.95.3/$file
> done
[john@localhost bin]$


8、实验交叉编译链

[john@localhost bin]$ # 10
[john@localhost bin]$ cd $PRJROOT/program
[john@localhost program]$ vi hello.c
[john@localhost program]$ cat -n hello.c
     1    #include <stdio.h>
     2    int main()
     3    {
     4        printf("hello world/n");
     5        return 0;
     6    }
[john@localhost program]$
[john@localhost program]$ gcc -g hello.c -o hello
[john@localhost program]$ ls
hello  hello.c
[john@localhost program]$ gdb
GNU gdb Fedora (6.8-1.fc9)
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu".
(gdb) file hello
Reading symbols from /home/john/embedded-system/program/hello...done.
(gdb) l
1    #include <stdio.h>
2    int main()
3    {
4        printf("hello world/n");
5        return 0;
6    }
(gdb)
(gdb) r
Starting program: /home/john/embedded-system/program/hello
hello world

Program exited normally.
Missing separate debuginfos, use: debuginfo-install glibc.i686
(gdb) q
[john@localhost program]$
[john@localhost program]$ arm-linux-gcc -g hello.c -o hello-linux
[john@localhost program]$ file hello-linux
hello-linux: ELF 32-bit LSB executable, ARM, version 1, dynamically linked (uses shared libs), for GNU/Linux 2.0.0, not stripped
[john@localhost program]$

原创粉丝点击