Emmet Friel cyber security blog

Building GCC from source

Introduction

Simple breakdown on installing GCC from source, updating the system with the newer version and updating GLIBC shared libraries. This will walk through installing GCC 11.4.0.

GCC Releases can be download here.

Dependencies

sudo apt install bzip2 flex build-essentials

Install

tar xzf gcc-11.4.0.tar.gz
cd gcc-11.4.0
./contrib/download_prerequisites
cd ..
mkdir objdir
cd objdir
$PWD/../gcc-11.4.0/configure --prefix=/opt/gcc-11.4.0 --enable-languages=c,c++,fortran,go
sudo make -j 4
sudo make install

Add PATHS

Add to .zshrc or .bashrc depending on your shell:

# Add GCC to PATH
export PATH=/opt/GCC-11.4.0/bin:$PATH
export LD_LIBRARY_PATH=/opt/GCC-11.4.0/lib64:$LD_LIBRARY_PATH

# Update CMake with new version of GCC 11.4.0
export CC=/opt/GCC-11.4.0/bin/gcc
export CXX=/opt/GCC-11.4.0/bin/g++

save, exit and source:

source .zshrc
source .bashrc
cp /opt/GCC-11.4.0/lib64/libstdc++.so.6.0.29 /usr/lib/x86_64-linux-gnu

# remove old symlink
cd /usr/lib/x86_64-linux-gnu
rm libstdc++.so.6

# add symlink to our new GLIBC
sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.29 /usr/lib/x86_64-linux-gnu/libstdc++.so.6

# update linker cache
sudo ldconfig

Update to new GCC and G++ version

#create symlinks to related binaries
sudo update-alternatives --install /usr/bin/gcc gcc /opt/GCC-11.4.0/bin/gcc 30
sudo update-alternatives --install /usr/bin/g++ g++ /opt/GCC-11.4.0/bin/g++ 30

# update alternatives
sudo update-alternatives --config gcc
sudo update-alternatives --config g++

Verify

➜   gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/GCC-11.4.0/bin/../libexec/gcc/x86_64-pc-linux-gnu/11.4.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /home/emmet/objdir/../gcc-11.4.0/configure --prefix=/home/emmet/GCC-11.4.0 --enable-languages=c,c++,go
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.4.0 (GCC)

➜   g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/GCC-11.4.0/bin/../libexec/gcc/x86_64-pc-linux-gnu/11.4.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /home/emmet/objdir/../gcc-11.4.0/configure --prefix=/home/emmet/GCC-11.4.0 --enable-languages=c,c++,go
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.4.0 (GCC)

Reverting back to older GCC versions

If you want to revert back to older GCC versions, you need to update the symlink in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.

strings /opt/GCC-11.4.0/lib64/libstdc++.so.6.0.29 | grep GLIBCX

In this case, the version is up to GLIBCXX_3.4.29. In GCC10, it depends up to GLIBCXX_3.4.28 but you can check this yourself.