How to Set Up Multiple Cross-Compilation Toolchains in a Linux Environment?

 Common Linux operating systems allow the installation of cross-compilation toolchains via package managers. For example, in an Ubuntu environment, you can install the GCC cross-compiler using the following command:

sudo apt-get install gcc-arm-linux-gnueabihf

To install the G++ cross-compiler, use this command:


sudo apt-get install g++-arm-linux-gnueabihf



Generally, compilers installed through package managers are relatively up-to-date. However, in some cases, embedded devices may run older versions of the GLIBC library. Programs compiled with newer compiler versions might not run on these older embedded systems. In such scenarios, it becomes necessary to manually install an older version of a cross-compiler toolchain. The following example demonstrates how to install an older version of the Linaro toolchain while ensuring it coexists with the previously installed toolchain.
First, download the required toolchain from the following URL: https://releases.linaro.org/components/toolchain/binaries/

For instance, download the December 2019 release, named gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf. After extracting it, copy it to the /opt/ directory. Since the name is quite long, I shortened it to gcc-linaro-7.5.0-x86_64_arm-linux-gnueabihf for convenience.
Next, modify the environment variables $PATH, $ARCH, and $CROSS_COMPILE.
The PATH environment variable specifies the path to the cross-compilation toolchain, and the system searches for executables from left to right. To prioritize the older toolchain, place its path at the beginning of PATH.
Open a terminal window and enter the following command:

export PATH=/opt/gcc-linaro-7.5.0-x86_64_arm-linux-gnueabihf/bin:$PATH

Verify the change with:


echo $PATH



Then, specify the architecture and cross-compiler prefix:



export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabihf-


Check these settings with:


echo $ARCH
echo $CROSS_COMPILE

Note that these environment variable changes are only effective for the current terminal session (process). This is intentional, as the older toolchain is only needed in specific cases, while the newer toolchain remains the default in most situations.
To determine which toolchain version is currently in use, run:


which arm-linux-gnueabihf-gcc




While the above method resolves the coexistence of multiple cross-compilation toolchains, manually entering these commands each time can be tedious. To simplify this, I created a script called env.sh as follows:



#!/bin/bash
# Set the cross-compiler toolchain path
TOOLCHAIN_PATH="/opt/gcc-linaro-7.5.0-x86_64_arm-linux-gnueabihf/bin"
export PATH="$TOOLCHAIN_PATH:$PATH"
# Define target architecture and cross-compiler prefix
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabihf-
# Optional: Verify environment variables
echo "Current PATH=$PATH"
echo "Current ARCH=$ARCH"
echo "Current CROSS_COMPILE=$CROSS_COMPILE"

Make the script executable with:


chmod +x env.sh

Execute the script using one of these commands:

. env.sh

or


source env.sh


Do not use the following command:



./env.sh




Running ./env.sh will apply the environment variables only to a subprocess, not the current terminal (parent process).
That concludes the explanation of how to set up multiple cross-compilation toolchains in a Linux environment.



















echo $PATH