一、运行环境
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_all
wget https://ftpmirror.gnu.org/binutils/binutils-2.31.tar.bz2
wget https://ftpmirror.gnu.org/glibc/glibc-2.28.tar.bz2
wget https://ftpmirror.gnu.org/gcc/gcc-8.3.0/gcc-8.3.0.tar.gz
git clone --depth=1 https://github.com/raspberrypi/linux
2.2 提取所有压缩文件
tar xf binutils-2.31.tar.bz2
tar xf glibc-2.28.tar.bz2
tar xf gcc-8.3.0.tar.gz
rm *.tar.*
2.3 下载GCC8.3需要的一些资源文件到源码中,删除没用的压缩包
cd gcc-8.3.0
contrib/download_prerequisites
rm *.tar.*
2.4 创建交叉编译的安装文件夹,并添加到PATH路径中
cd ~/gcc_all
sudo mkdir -p /opt/cross-pi-gcc
sudo chown $USER /opt/cross-pi-gcc
export PATH=/opt/cross-pi-gcc/bin:$PATH
2.5 安装Linux内核头文件到交叉编译安装文件夹
cd ~/gcc_all
cd linux
KERNEL=kernel7make ARCH=arm INSTALL_HDR_PATH=/opt/cross-pi-gcc/arm-linux-gnueabihf headers_install
2.6 编译安装Binutils 2.31
cd ~/gcc_all
mkdir 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 -j8
make install
2.7 编译安装GCC
cd ~/gcc_all
mkdir 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-gcc
make install-gcc
2.8 编译安装glibc2.28
cd ~/gcc_all
mkdir 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
2.9 回到GCC编译安装libgcc
cd ..
cd build-gcc
make -j8 all-target-libgcc
make install-target-libgcc
2.10 结束安装Glibc
cd ..
cd build-glibc
make -j8
make install
2.11 最后完成安装GCC8.3.0
cd ..
cd build-gcc
make -j8
make install
cd ..
安装完成后系统中存在arm-linux-gnueabihf-gcc-8.3.0。可在控制命令中查到:
三、测试
新建一个test.c文件如下:
#include “stdio.h”
int main()
{printf(“test arm-linux-gnueabihf cross-compile-tools\r\n”);return 0;
}
执行编译:
arm-linux-gnueabihf-gcc test.c -o out
查看编译信息:
file out
out: 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/