Compile and install GCC on Linux

Today I needed to upgrade GCC to a newer version than what comes from aptitude. I’m on LinuxMint 18.3

First make sure to remove gcc installed from aptitude

Then, install linux-libc-dev and for Ubuntu based distros, at least, a symlink must be created as GCC expect files in a folder that is in a different location

sudo apt install linux-libc-dev
sudo ln -s /usr/include/asm-generic /usr/include/asm

Finally, fetch the GCC version you need, build and install

cd ~/Downloads
wget ftp://gnu.mirror.iweb.com/gcc/gcc-7.4.0/gcc-7.4.0.tar.gz
tar -xvzf gcc-7.4.0.tar.gz
cd gcc-7.4.0
contrib/download_prerequisites
mkdir build
cd build
../configure -v --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --prefix=/usr/local/gcc-7.4 --enable-checking=release --enable-languages=c,c++,fortran --disable-multilib
make -j8
make install-strip

Finally, add the new gcc 7.4 paths at the end of your .bashrc file

export PATH=/usr/local/gcc-7.4/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/gcc-7.4/lib64:$LD_LIBRARY_PATH

Reload the .bashrc file and make sure gcc 7.4 is found

~ $ source .bashrc
~ $ gcc --version
gcc (GCC) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

When building cmake projects, I force gcc version to 7.4. When I’m confident I won’t have issues with the new compiler, I’ll remove the old one. But for now, before each cmake build I set CC and CXX

export CC=/usr/local/gcc-7.4/bin/gcc
export CXX=/usr/local/gcc-7.4/bin/g++

Leave a Reply

Your email address will not be published. Required fields are marked *