Skip to content

Cross Compiler: Installation Instructions

Abhishek Thakur edited this page Dec 2, 2020 · 55 revisions

Raspberry Pi Toolchains Logo

Raspberry Pi GCC Cross-Compiler ARM Toolchains Setup Guide

These ARM toolchains can be used on any Linux Distributions (32-bit/64-bit) for cross-compiling programs for Raspberry Pi 32-bit OSes.

  • Stretch: Generates programs for Raspbian Stretch OS (Debian Version 9) and equivalent OS only.
  • Buster: Generates programs for Raspbian Buster OS (Debian Version 10) and equivalent OS only.

 


Important ⚠️

  • These instructions are exclusively for GCC version 9.2.0 but will work with any compiler version available with this project.

  • These instructions are Linux-machines specific only.


 

Table of content

 

 

A. Prerequisites

  • Update your environment:

    sudo apt update && sudo apt dist-upgrade
  • Install Important Packages:

    sudo apt-get install build-essential gawk gcc g++ gfortran git texinfo bison libncurses-dev

 

 

B. Download Binary

Compressed pre-compiled GCC Toolchain binaries can be easily be downloaded from the project's SourceForge Repository by clicking links given in the following table:

Toolchains Binaries Status GCC versions
Raspberry Pi GCC Cross-Compiler Toolchains (Stretch) Stable/Production 6.3.0, 9.2.0, 9.3.0, 10.2.0
Raspberry Pi GCC Cross-Compiler Toolchains (Buster) Stable/Production 8.3.0, 9.2.0, 9.3.0, 10.2.0

 

 

C. Extracting and Linking Binary

These Raspberry toolchains have the same standardized headers with no hardcoded paths, thereby can be directly used for immediate application OR can be installed permanently for on a daily basis usage.

C1. Temporary Installation: Use these binaries directly (Recommended)

  • Extraction: Extract using tar terminal command as follows:

    tar xf <filename e.g cross-gcc-9.2.0-pi_2-3.tar.gz>
  • Linking:

    PATH=/<extracted folder-name e.g cross-pi-gcc-9.2.0-1>/bin:$PATH
    LD_LIBRARY_PATH=/<extracted folder-name e.g cross-pi-gcc-9.2.0-1>/lib:$LD_LIBRARY_PATH

 

C2. Permanent Installation:

  • Extraction: Extract using tar terminal command as follows:

    tar xf <filename e.g cross-gcc-9.2.0-pi_2-3.tar.gz>
  • Configuring: Move extracted folder to any location (for e.g. /opt) by using following command:

    sudo mv <extracted folder-name e.g cross-pi-gcc-9.2.0-1> /opt
  • Linking: Properly link Path/Environment Variables permanently with either of the following given methods:

    • Appending variables to your .profile: (Recommended)

       echo 'export PATH=/opt/<extracted folder-name e.g cross-pi-gcc-9.2.0-1>/bin:$PATH' >> .profile  
       echo 'export LD_LIBRARY_PATH=/opt/<extracted folder-name e.g cross-pi-gcc-9.2.0-1>/lib:$LD_LIBRARY_PATH' >> .profile
       source .profile
    • Appending variables to your .bashrc: ⚠️ Some Linux users reported some trouble with configuring path variables at .profile that doesn't seem to work for them. If you encounter a similar problem, try setting/configure by adding paths to your .bashrc file instead of as follows:

       echo 'export PATH=/opt/<extracted folder-name e.g cross-pi-gcc-9.2.0-1>/bin:$PATH' >> .bashrc
       echo 'export LD_LIBRARY_PATH=/opt/<extracted folder-name e.g cross-pi-gcc-9.2.0-1>/lib:$LD_LIBRARY_PATH' >> .bashrc
       source .bashrc

 

 

D. Advanced Information

  • To enable Link-time-optimization (LTO):

    export AR="arm-linux-gnueabihf-gcc-ar"
    export CC="arm-linux-gnueabihf-gcc"
    export CXX="arm-linux-gnueabihf-g++"
    export CPP="arm-linux-gnueabihf-cpp"
    export FC="arm-linux-gnueabihf-gfortran"
    export RANLIB="arm-linux-gnueabihf-gcc-ranlib"
    export LD="$CXX"
    
    GCCPATH="/<extracted folder-name e.g cross-pi-gcc-9.2.0-1>/libexec/gcc/arm-linux-gnueabihf/9.2.0"
    export ARFLAGS="--plugin $GCCPATH/liblto_plugin.so"
    export RANLIBFLAGS="--plugin $GCCPATH/liblto_plugin.so"

    NOTE: 💡 LTO also needs g++ to be the linker, and it can be enabled at compile-time by setting -flto=$(nproc) and -fno-fat-lto-objects flags in CFLAGS, CXXFLAGS, and LDFLAGS respectively.

  • Extra step to use Cross-Compiler Binaries with CMAKE:

    Enable CMAKES's implicit directory feature by injecting the following lines into toolchain file: (Refer Issue:#3)

    unset(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES)
    unset(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES)