Here’s a comprehensive script for building the latest GCC compiler from source code. I’ll omit detailed explanations of options as they are available on the official documentation.
Building GCC yourself instead of using system packages allows you to access the latest features and bug fixes.
When executed, GCC will be installed in $HOME/opt/gcc.
Build Script
The following script will build GCC 14.2.0 and install it to $HOME/opt/gcc:
#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status# Create source code directorymkdir -p "$HOME/src"cd "$HOME/src"# Download necessary filescurl -O https://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-14.2.0/gcc-14.2.0.tar.gz
tar -xvf gcc-14.2.0.tar.gz
cd gcc-14.2.0
# Download dependenciesbash ./contrib/download_prerequisites
# Create a build directory (it's recommended to build outside the source directory)mkdir -p build
cd build
# Configure compilation../configure --disable-multilib \
--enable-languages=c,c++ \
--prefix="$HOME/opt/gcc"# Compile (parallel build)make -j$(nproc)# Installmake install
Option Explanations
--disable-multilib: Disables building libraries for multiple target architectures (reduces build time)
--enable-languages=c,c++: Build only C and C++ compilers. Other options include fortran, objc, ada, etc.
--prefix="$HOME/opt/gcc": Specifies the installation directory
make -j$(nproc): Performs parallel build according to the number of available CPU cores