See also: Building Clang From Source
Install the prereqs (for amd64 arch Ubuntu):
sudo apt install build-essential libc6-dev libc6-dev-i386 libgmp-dev libmpfr-dev libmpc-dev libzstd-dev
Download gcc from https://gcc.gnu.org.
Un-tar the archive into a folder, cd inside, then mkdir build; cd build
.
Then run:
../configure --program-suffix=-12.2.0 --enable-languages=c,c++,lto
nice make -j8
sudo make install
This installs into /usr/local with the executables in /usr/local/bin with suffix like "gcc-12.2.0".
To also build cross-compilers, make a new directory, and then use the same commands as before, but with additional options:
--target=avr
--target=arm-none-eabi --disable-shared
sudo apt install libc6-dev-armel-cross libc6-dev-armhf-cross libc6-arm64-cross
--target=arm-none-linux-eabi