一、运行环境
1.1 执行机环境
ld | GNU ld (GNU Binutils for Raspbian) 2.31.1 |
ldd | ldd (Debian GLIBC 2.28-10+rpi1) 2.28 Copyright (C) 2018 Free Software Foundation, Inc. |
gcc | (Raspbian 8.3.0-6+rpi1) 8.3.0 Copyright (C) 2018 Free Software Foundation, Inc. |
arm | ARMv7 ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0 |
1.2 交叉编译机环境
Linux 桌面系统 Ubuntu 16.04.4 LTS
二、编译步骤
2.1 用户目录下新建gcc_all文件夹,下载binutils-2.31.tar.bz2 gcc-8.3.0.tar.gz glibc-2.28.tar.bz2 linux。
cd ~mkdir gcc_all && cd gcc_allwget https://ftpmirror.gnu.org/binutils/binutils-2.31.tar.bz2wget https://ftpmirror.gnu.org/glibc/glibc-2.28.tar.bz2wget https://ftpmirror.gnu.org/gcc/gcc-8.3.0/gcc-8.3.0.tar.gzgit clone --depth=1 https://github.com/raspberrypi/linux
2.2 提取所有压缩文件
tar xf binutils-2.31.tar.bz2tar xf glibc-2.28.tar.bz2tar xf gcc-8.3.0.tar.gzrm *.tar.*
2.3 下载GCC8.3需要的一些资源文件到源码中,删除没用的压缩包
cd gcc-8.3.0contrib/download_prerequisitesrm *.tar.*
2.4 创建交叉编译的安装文件夹,并添加到PATH路径中
cd ~/gcc_allsudo mkdir -p /opt/cross-pi-gccsudo chown$USER /opt/cross-pi-gccexport PATH=/opt/cross-pi-gcc/bin:$PATH
2.5 安装Linux内核头文件到交叉编译安装文件夹
cd ~/gcc_allcd linuxKERNEL=kernel7make ARCH=arm INSTALL_HDR_PATH=/opt/cross-pi-gcc/arm-linux-gnueabihf headers_install
2.6 编译安装Binutils 2.31
cd ~/gcc_allmkdir build-binutils && cd build-binutils../binutils-2.31/configure --prefix=/opt/cross-pi-gcc --target=arm-linux-gnueabihf --with-arch=armv6 --with-fpu=vfp --with-float=hard --disable-multilibmake -j8make install
2.7 编译安装GCC
cd ~/gcc_allmkdir build-gcc && cd build-gcc../gcc-8.3.0/configure --prefix=/opt/cross-pi-gcc --target=arm-linux-gnueabihf --enable-languages=c,c++,fortran --with-arch=armv6 --with-fpu=vfp --with-float=hard --disable-multilibmake -j8 all-gccmake install-gcc
2.8 编译安装glibc2.28
cd ~/gcc_allmkdir build-glibc && cd build-glibc../glibc-2.28/configure --prefix=/opt/cross-pi-gcc/arm-linux-gnueabihf --build=$MACHTYPE --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf --with-arch=armv6 --with-fpu=vfp --with-float=hard --with-headers=/opt/cross-pi-gcc/arm-linux-gnueabihf/include --disable-multilib libc_cv_forced_unwind=yesmake install-bootstrap-headers=yes install-headersmake -j8 csu/subdir_libinstall csu/crt1.o csu/crti.o csu/crtn.o /opt/cross-pi-gcc/arm-linux-gnueabihf/libarm-linux-gnueabihf-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o /opt/cross-pi-gcc/arm-linux-gnueabihf/lib/libc.sotouch /opt/cross-pi-gcc/arm-linux-gnueabihf/include/gnu/stubs.h
错误处理:* LD_LIBRARY_PATH shouldn’t contain the current directory when
* building glibc. Please change theenvironment variable
* and run configure again.
解决:记录LD_LIBRARY_PATH的值(echo $ LD_LIBRARY_PATH=),清空该变量(# LD_LIBRARY_PATH=),编译安装结束后重新添加。
2.9 回到GCC编译安装libgcc
cd ..cd build-gccmake -j8 all-target-libgccmake install-target-libgcc
2.10 结束安装Glibc
cd ..cd build-glibcmake -j8make install
2.11 最后完成安装GCC8.3.0
cd ..cd build-gccmake -j8make installcd ..
安装完成后系统中存在arm-linux-gnueabihf-gcc-8.3.0。可在控制命令中查到:
三、测试
新建一个test.c文件如下:
#include “stdio.h”intmain(){printf(“test arm-linux-gnueabihf cross-compile-tools\r\n”);return0;}
执行编译:
arm-linux-gnueabihf-gcc test.c -o out
查看编译信息:
file outout: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, not stripped
编译完成。
四、参考
Building GCC as a cross compiler for Raspberry Pi:https://solarianprogrammer.com/2018/05/06/building-gcc-cross-compiler-raspberry-pi/