Building Gcc From Source

This is a note to self, it contains the build process for gcc for c and c++ frontends. The process is not too different from any project that uses autotools.

Note: This post assumes that we are building gcc on fedora for x86_64 architecture.

Obtain the source code

Clone the repository with

git clone git://gcc.gnu.org/git/gcc.git

dependencies

You will need to install following packages in order to build gcc from source.

To install these dependencies using dnf

sudo dnf groupinstall "Development tools"
sudo dnf install mpfr-devel gmp-devel libmpc-devel zlib-devel glibc-devel

Or you can use the script contrib/download_prerequisites from the gcc repository itself.

cd gcc
./contrib/download_prerequisites

Build and install gcc

The next process is same as the build process for any other project that uses autotools.

In this case, - create a directory gcc-build. - run configure script for gcc from that directory with config configure --prefix=$INSTALLDIR --enable-languages=c,c++ --disable-multilib - run make - run make install

export INSTALLDIR=gcc-install
cd ..
mkdir gcc-build
mkdir gcc-install
cd gcc-build
../gcc/configure --prefix=$INSTALLDIR --enable-languages=c,c++ --disable-multilib
make -j$(nproc)
make install

Once this is done, you can add $INSTALLDIR/bin to your $PATH and use gcc that we just built from the source.