Compiling GCC on Linux

Prerequisites
Some prerequisites are required before you can compile GCC.


 * 1) Install the GCC compiler by running sudo apt install build-essential.
 * 2) Also run sudo apt-get install linux-libc-dev:i386 if you want to build the 32-bit binaries.
 * 3) Install Flex and Bison.
 * 4) Run  (this automatically installs the gmp, mpfr, isl and mpc libraries).

Compiling
To compile the GCC source code on Linux:


 * 1) cd into the build path.
 * 2) Run [source directory]/configure.
 * 3) You might also want to include --enable-checking as this will ask the compiler to check its tree structure for errors when in use (useful for seeing if you've messed something up).
 * 4) Run make -j [number of CPU cores] e.g. make -j 6.
 * 5) Then if you want to install you can do sudo make install (gets installed in usr/local/bin/ by default). Some say this is bad practice ... but you only live once.

Common pitfalls:


 * Once you've built it once, if you make changes to the code, you simply have to re-build it again (don't reconfigure it), and it is very quick.
 * Using a build directory with spaces can fail (e.g. instead of calling your build folder "gcc build", you should call it something like "gcc_build").
 * Make sure you have enough disk space (a build on default settings will take up around 10 GB).

Cleaning
To clean the build run make distclean. Although, this doesn't seem to be necessary after doing source code changes; you can simply re-run make -j 6 (apparently it's only necessary if you change your build architecture target.)

Usage
After that, you can simply use the gcc command in the terminal. If you installed it, it's somewhat easier to use, although if you're making changes to it I have found that this is really a bad idea. You don't want to be stuck with a broken GCC in your system folders, because once it's there, it's very difficult to remove. If you try to remove it, you'll probably end up breaking something else, and you also won't be able to compile anything.

Use gcc --version to verify your newly-compiled version is the one that's active.