目录
一、简介
本文主要讲解gcc交叉编译工具链搭建的全过程,以本人实际的操作去讲述整个工具链的搭建,希望对大家有所帮助。
二、工具链的搭建流程
1、下载相关源码:包括binutils、kernel、gcc、glibc等; 2、搭建本地编译环境,安装本地编译依赖包; 3、设置环境变量; 4、编译binutils; 5、复制Linux内核头文件; 6、建立初始编译器; 7、编译glibc; 8、建立全套编译器(full gcc); 9、 编译其他工具 10、创建链接; 11、工具链验证;
三、过程搭建详细讲解
3.1 下载相关源码 3.1.1 首先下载本次交叉编译工具需要的源码包具体如下:1、binutils; 2、mpc、mpfr、gmp; 3、kernel; 4、gcc; 5、glic;
3.1.2 相关源码下载网址参考:
[root@localhost /]# lftp ftp://ftp.gnu.org/gnu/gcc/[root@localhost /]# lftp ftp://ftp.kernel.org/pub/linux/devel/binutils/[root@localhost /]# lftp ftp://ftp.gnu.org/gnu/mpc/[root@localhost /]# lftp ftp://ftp.gnu.org/gnu/mpfr/[root@localhost /]# lftp ftp://ftp.gnu.org/gnu/gmp/lftp ftp.gnu.org:/gnu/gmp> mget gmp-6.1.1.tar.xz[root@localhost /]# wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.10.40.tar.xz[root@localhost /]# wget http://ftpmirror.gnu.org/glibc/glibc-2.20.tar.xz
其他依赖包:wget ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.15.tar.bz2 wget ftp://gcc.gnu.org/pub/gcc/infrastructure/cloog-0.18.1.tar.gz
注:在解开的内核源码中Documentation/Changes中写明了该版本内核所需要依赖的各个包的版本以及下载的路径,注意下载的mpc、mpfr、gmp都尽量下载最新版本。
3.2 搭建本地编译环境,安装本地编译依赖包
[root@localhost /]# apt-get install xz-utils[root@localhost /]# apt-get install m4[root@localhost /]# apt-get install libncurses-dev[root@localhost /]# apt-get install g++[root@localhost /]# apt-get install gawk
3.3 设置环境变量
export TARGET=arm-linux-gnueabiexport PREFIX=/opt/cross/gcc-4.8.7export TARGET_PREFIX=TARGETexport PATH=PREFIX/bin
--prefix 编译好的文件重定位安装路径,如make install后相关的可执行文件都安装在prefix指定的路径下--build – 本地编译机器的平台架构,如x86--host – 需要交叉编译成的平台架构,如arm64--target – 执行的可执行代码的目标平台
也可以将其加入到.bashrc文件中打开终端后可以自动执行
[root@localhost /]# vim ~/.bashrc[root@localhost /]# source ~/.bashrc
3.4 编译binutils 此步骤构建并安装交叉汇编器、交叉链接器和其他工具。
[root@localhost /]# tar -xjvf binutils-2.7.tar.gz[root@localhost binutils-2.7]# cd binutils-2.7/[root@localhost binutils-2.7]# ./configure --prefix=TARGET --disable-multilib[root@localhost binutils-2.7]# make[root@localhost binutils-2.7]# make install
此时在PREFIX headers_install[root@localhost /]# ls /opt/cross/gcc-4.8.7/includeasm asm-generic drm linux misc mtd rdma scsi sound uapi video xen[root@localhost /]# ls /opt/cross/gcc-4.8.7/include/generated/uapi/linux/version.h
3.6 建立初始编译器(不带glibc支持)
[root@localhost /]# tar -xjvf gcc-4.8.7.tar.bz2//解压mpfr-3.1.2.tar.bz2、mpc-1.0.1.tar.gz、gmp-5.1.3.tar.bz2,并改名复制至gcc-4.8.7中[root@localhost /]# tar -xjvf gmp-5.1.3.tar.bz2[root@localhost /]# mv gmp-5.1.3 gcc-4.8.7/gmp[root@localhost /]# tar -xjvf mpfr-3.1.2.tar.bz2[root@localhost /]# mv mpfr-3.1.2 gcc-4.8.7/mpfr[root@localhost /]# tar -xzvf mpc-1.0.1.tar.gz[root@localhost /]# mv mpc-1.0.1 gcc-4.8.7/mpc[root@localhost /]# cd gcc-4.8.7[root@localhost gcc-4.8.7]# ./configure --prefix=TARGET --with-headers=PREFIX/bin下又多了几个文件:arm-linux-gnueabi-cpp:GNU的C的预编译器arm-linux-gnueabi-gcc:GNU的C语言编译器arm-linux-gnueabi-gcc-4.8.7:GNU的C语言编译器,其实和arm-linux-gcc是一样的arm-linux-gnueabi-gcov:gcc 的辅助测试工具,用来分析和优化程序
在/opt/cross/lib/gcc/arm-linux/4.8.7/中安装了两个静态库libgcc.a 和 libgcc_eh.a。 共享库libgcc_s.so安装到/opt/cross/arm-linux/lib64。
3.7 编译glibc
[root@localhost /]# tar -xvf glibc-2.20.tar.xz[root@localhost glibc-2.20]# cd glibc-2.20[root@localhost glibc-2.20]# export CC=PREFIX/lib/gcc/arm-linux-gnueabi/4.8.7/libgcc.a TARGET --target=TARGET_PREFIX --enable-add-ons --disable-profile --cache-file=config.cache --with-binutils=TARGET_PREFIX/include[root@localhost glibc-2.20]# make all[root@localhost glibc-2.20]# make install
3.8 建立全套编译器(full gcc)
[root@localhost /]# unset CC[root@localhost /]# unset CFLAGS[root@localhost /]# cd gcc-4.8.7/[root@localhost gcc-4.8.7]# ./configure --prefix=TARGET --enable-shared --enable-languages=c,c++[root@localhost gcc-4.8.7]# make[root@localhost gcc-4.8.7]# make install
完成之后,PREFIX/bin[root@localhost bin]# ln -s arm-linux-gnueabi-addr2line arm-linux-addr2line[root@localhost bin]# ln -s arm-linux-gnueabi-ar arm-linux-ar[root@localhost bin]# ln -s arm-linux-gnueabi-as arm-linux-as[root@localhost bin]# ln -s arm-linux-gnueabi-c++ arm-linux-c++[root@localhost bin]# ln -s arm-linux-gnueabi-c++filt arm-linux-c++filt[root@localhost bin]# ln -s arm-linux-gnueabi-cpp arm-linux-cpp[root@localhost bin]# ln -s arm-linux-gnueabi-elfedit arm-linux-elfedit[root@localhost bin]# ln -s arm-linux-gnueabi-g++ arm-linux-g++[root@localhost bin]# ln -s arm-linux-gnueabi-gcc arm-linux-gcc[root@localhost bin]# ln -s arm-linux-gnueabi-gcc-4.8.2 arm-linux-gcc-4.8.2[root@localhost bin]# ln -s arm-linux-gnueabi-gcov arm-linux-gcov[root@localhost bin]# ln -s arm-linux-gnueabi-gdb arm-linux-gdb[root@localhost bin]# ln -s arm-linux-gnueabi-gdbtui arm-linux-gdbtui[root@localhost bin]# ln -s arm-linux-gnueabi-gprof arm-linux-gprof[root@localhost bin]# ln -s arm-linux-gnueabi-ld arm-linux-ld[root@localhost bin]# ln -s arm-linux-gnueabi-ld.bfd arm-linux-ld.bfd[root@localhost bin]# ln -s arm-linux-gnueabi-nm arm-linux-nm[root@localhost bin]# ln -s arm-linux-gnueabi-objcopy arm-linux-objcopy[root@localhost bin]# ln -s arm-linux-gnueabi-objdump arm-linux-objdump[root@localhost bin]# ln -s arm-linux-gnueabi-ranlib arm-linux-ranlib[root@localhost bin]# ln -s arm-linux-gnueabi-readelf arm-linux-readelf[root@localhost bin]# ln -s arm-linux-gnueabi-run arm-linux-run[root@localhost bin]# ln -s arm-linux-gnueabi-size arm-linux-size[root@localhost bin]# ln -s arm-linux-gnueabi-strings arm-linux-strings[root@localhost bin]# ln -s arm-linux-gnueabi-strip arm-linux-strip
3.11 工具链验证 下面编写一个简单的C程序,使用建立的工具链。
[root@localhost /]# vi hello.c#include<stdio.h>intmain(void){ printf("hellolinux/n");return 0;}[root@localhost /]# arm-linux-gcc hello.c -o hello –static (制作静态可执行文件)[root@localhost /]# file hellohello: ELF 32-bit LSB executable, ARM, version 1, statically linked, for GNU/Linux 2.0.0, not stripped
有如上输出表示编译ARM版本程序成功。 制作的可执行文件hello可以直接在目标机上运行。