Building GCC as a cross compiler for Raspberry Pilinux
How to Build a GCC Cross-Compilerandroid
PC: ubuntu18.04 64位 (Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz)c++
GCC:gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)git
下面是咱們交叉編譯GCC用到的三個軟件包:binutils,glibc以及gcc,下載連接以下:github
binutils:https://mirrors.nju.edu.cn/gnu/binutils/,我下載的是binutils-2.32.tar.xzbootstrap
glibc:https://ftp.gnu.org/gnu/glibc/,我下載的是glibc-2.30.tar.xzubuntu
gcc:https://github.com/gcc-mirror/gcc,我下載的是:gcc-9_2_0-release (切到tag標籤,選擇對應的tag)app
sudo apt update sudo apt upgrade sudo apt install build-essential gawk git texinfo bison flex
mkdir -p /home/pengdl/work/GNU/mk_cross_compiler/ cd /home/pengdl/work/GNU/mk_cross_compiler
$ pwd /home/pengdl/work/GNU/mk_cross_compiler $ mkdir code $ cd code $ tar -xf ./binutils-2.32.tar.xz- $ tar -xf ./glibc-2.30.tar.xz- $ tar -xf ./gcc-9_2_0-release.tar.gz
cd gcc-9_2_0/ ./contrib/download_prerequisites
cd /home/pengdl/work/GNU/mk_cross_compiler mkdir cross-gcc export PATH=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/bin:$PATH
cd /home/pengdl/aarch64/android_kernel/linux-4.14 make ARCH=arm64 INSTALL_HDR_PATH=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/aarch64-linux-gnu headers_install
cd /home/pengdl/work/GNU/mk_cross_compiler mkdir build-binutils cd build-binutils/ ../code/binutils-2.32/configure --prefix=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc --target=aarch64-linux-gnu --disable-multilib --with-arch=armv8 make -j8 make install
下面開始編譯glibc和gcc,因爲這兩個相互依賴,因此咱們先部分編譯gcc,而後部分編譯glibc,最後所有編譯gcc和glibc。工具
cd /home/pengdl/work/GNU/mk_cross_compiler mkdir build-gcc cd build-gcc ../code/gcc-9_2_0/configure --prefix=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc --target=aarch64-linux-gnu --enable-languages=c,c++ --disable-multilib make all-gcc -j8 make install-gcc
cd /home/pengdl/work/GNU/mk_cross_compiler mkdir build-glibc cd build-glibc/ ../code/glibc-2.30/configure --prefix=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/aarch64-linux-gnu --build=$MACHINE --host=aarch64-linux-gnu --target=aarch64-linux-gnu --with-headers=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/aarch64-linux-gnu/include --disable-multilib libc_cv_forced_unwind=yes make install-bootstrap-headers=yes install-headers make -j8 csu/subdir_lib install csu/crt1.o csu/crti.o csu/crtn.o /home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/aarch64-linux-gnu/lib aarch64-linux-gnu-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o /home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/aarch64-linux-gnu/lib/libc.so touch /home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/aarch64-linux-gnu/include/gnu/stubs.h
修改/home/pengdl/work/GNU/mk_cross_compiler/code/gcc-9_2_0/libsanitizer/asan/asan_linux.cc,在第77行加入以下代碼:測試
#ifndef PATH_MAX #define PATH_MAX 4096 #endif
cd /home/pengdl/work/GNU/mk_cross_compiler/build-gcc make -j8 all-target-libgcc make install-target-libgcc
cd /home/pengdl/work/GNU/mk_cross_compiler/build-glibc make -j8 make install
cd /home/pengdl/work/GNU/mk_cross_compiler/build-gcc make -j8 make install
首先查看一下咱們本身編譯生成的gcc版本:
$ aarch64-linux-gnu-gcc -v Using built-in specs. COLLECT_GCC=aarch64-linux-gnu-gcc COLLECT_LTO_WRAPPER=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc/libexec/gcc/aarch64-linux-gnu/9.2.0/lto-wrapper Target: aarch64-linux-gnu Configured with: ../code/gcc-9_2_0/configure --prefix=/home/pengdl/work/GNU/mk_cross_compiler/cross-gcc --target=aarch64-linux-gnu --enable-languages=c,c++ --disable-multilib Thread model: posix gcc version 9.2.0 (GCC)
編寫一個hello_world.c:
$ cat hello_world.c #include <stdio.h> int main(int argc, const char *argv[]) { printf("Hello World\n"); return 0; }
編譯測試:
$ aarch64-linux-gnu-gcc --static hello_world.c -o hello_world $ qemu-aarch64 ./hello_world Hello World
完。