Skip to content

Instantly share code, notes, and snippets.

@sol-prog
Last active February 14, 2024 22:07
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sol-prog/94b2ba84559975c76111afe6a0499814 to your computer and use it in GitHub Desktop.
Save sol-prog/94b2ba84559975c76111afe6a0499814 to your computer and use it in GitHub Desktop.
How to build a cross compiler for Raspberry Pi

Sources:

http://preshing.com/20141119/how-to-build-a-gcc-cross-compiler/
https://www.raspberrypi.org/documentation/linux/kernel/building.md


https://wiki.osdev.org/Why_do_I_need_a_Cross_Compiler%3F
https://wiki.osdev.org/GCC_Cross-Compiler
https://wiki.osdev.org/Building_GCC
http://www.ifp.illinois.edu/~nakazato/tips/xgcc.html

Tested on Ubuntu 18.04 64 bits

  1. Update system
sudo apt update
sudo apt upgrade
  1. Install prereq:
sudo apt install build-essential gawk git texinfo bison
  1. Download software:
wget https://ftpmirror.gnu.org/binutils/binutils-2.30.tar.bz2
wget https://ftpmirror.gnu.org/gcc/gcc-8.1.0/gcc-8.1.0.tar.gz
wget https://ftpmirror.gnu.org/glibc/glibc-2.27.tar.bz2
git clone --depth=1 https://github.com/raspberrypi/linux
  1. Extract archives and remove them
tar xf binutils-2.30.tar.bz2
tar xf glibc-2.27.tar.bz2
tar xf gcc-8.1.0.tar.gz
rm *.tar.*
  1. Get GCC prereq
cd gcc-*
contrib/download_prerequisites
rm *.tar.*
cd ..
  1. Create the folder in which we'll put the cross compiler and add it to the PATH
sudo mkdir -p /opt/cross-pi-gcc
sudo chown $USER /opt/cross-pi-gcc
export PATH=/opt/cross-pi-gcc/bin:$PATH
  1. Build binutils
mkdir build-binutils && cd build-binutils
../binutils-2.30/configure --prefix=/opt/cross-pi-gcc --target=arm-linux-gnueabihf --with-arch=armv6 --with-fpu=vfp --with-float=hard --disable-multilib
make -j 8
make install
  1. Install kernel headers
# See also https://www.raspberrypi.org/documentation/linux/kernel/building.md

cd linux
KERNEL=kernel7
make ARCH=arm INSTALL_HDR_PATH=/opt/cross-pi-gcc/arm-linux-gnueabihf headers_install
  1. Build compilers
cd ..
mkdir build-gcc && cd build-gcc
../gcc-8.1.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-multilib
make -j8 all-gcc
make install-gcc
  1. Partially build glibc
cd ..
mkdir build-glibc && cd build-glibc
../glibc-2.27/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=yes
make install-bootstrap-headers=yes install-headers
make -j8 csu/subdir_lib
install csu/crt1.o csu/crti.o csu/crtn.o /opt/cross-pi-gcc/arm-linux-gnueabihf/lib
arm-linux-gnueabihf-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o /opt/cross-pi-gcc/arm-linux-gnueabihf/lib/libc.so
touch /opt/cross-pi-gcc/arm-linux-gnueabihf/include/gnu/stubs.h
  1. Build compiler support library
cd ..
cd build-gcc
make -j8 all-target-libgcc
make install-target-libgcc
  1. Finish building Glibc
cd ..
cd build-glibc
make -j8
make install
  1. Finish building GCC
cd ..
cd build-gcc
make -j8
make install
cd ..
  1. You build any C++ code for the Pi with something like:
arm-linux-gnueabihf-g++ -std=c++17 test.cpp -o test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment