Mead's Guide to Building GCC 7 on Linux and Mac

(Linux Mint 17/18 and Mac OS El Capitan 10.11)

Overview

Linux distributions generally come with a specific version of the GCC compilers and it takes some effort (usually, only just a little effort) to install newer versions of the compilers. This isn't usually a problem because many people upgrade their Linux distribution on a regular basis and, therefore, get the newer versions of the compilers.

However:

  1. Not everyone wants to upgrade their entire system just to get the latest compilers.
  2. Sometimes a newer version of the compiler comes out way before a newer distribution is going to be released.
I have been part of both reasons stated above. There are basically 3 ways that you would get a compiler onto Linux:
  1. Install a Linux distribution and take the default compiler version that comes with it.
  2. Manually install additional compiler versions from the official repositories. (Linux makes it really easy to have multiple versions installed simultaneously.)
  3. Get the source code to the compiler you want and build/install it yourself.
This document is going to show how to do method #3 above. To install additional versions via method #2, see my other guide: Mead's Guide to Upgrading GCC and Clang on Linux. If your are running a recent version of Linux Mint, I recommend that you choose method #2 as it is faster, easier, and more likely to be successful.

When I first set out to do this, it was because I wanted to install GCC 7.1 on an iMac running macOS 10.11 (El Capitan). It turns out that GCC isn't installed on the Mac. When you run gcc or g++, you are really running clang and clang++, as gcc and g++ are just aliases for the Clang compilers. Once I had all of this working on the Mac, I realized that the exact same procedure could be done on Linux to get 7.1 built and installed there. Sweet!

Disclaimer: While I was searching on "how to install GCC on an iMac", I came across this link which is the basis for this Guide. I basically took the information on that page and then wrote a bash script which will automate the entire process on both Mac and Linux.

Note: I am not an expert with macOS or with building compilers on macOS and Linux. I followed the steps from the link above and successfully installed GCC 7.1 on both platforms. It was relatively painless. There may be "better" ways to do this, and you may actually know them. However, this technique I'm showing worked quite nicely for me and I hope it works for you. If not, you can probably modify some of the steps to suit you better.

Preparation

You might want to read the Notes at the bottom of this document before you begin.

You'll need to download the source code for the compiler that you want. Choose a mirror that's closest to you: GCC mirror sites.

In order to build the GCC compilers, you have to first build a few prerequisite libraries. Those libraries are: mpc, mpfr, gmp, and the Graphite loop optimizations.

To make life easier, I've downloaded all of these dependencies and am including them in a zip file here: gcc7.1-src.zip. You may want to actually download the latest versions of these files from the links above, as the files I'm posting will be outdated at some point. Either way, the process will be identical. You will just substitute the later versions for the ones that I'm providing.

The reason that I'm providing these files is because, when I first began this process, a couple of the links were broken and I had difficulty finding a working download link. A few days later, those links magically worked again. I can't trust that the links won't get broken again, so I've made everything available in that zip file for convenience.

It should also be obvious that, in order to build GCC, you must already have a working compiler installed on your computer! Any recent version of GCC (Linux) or Clang (Mac) should be sufficient.

Notes for building on Linux Mint:

If you have a fresh install of Linux Mint, you will need to install a couple of packages in order to build everything. These commands should do the trick:

sudo apt-get update
sudo apt-get install build-essential gcc-multilib
These packages will give you a proper build environment, including 32-bit support, which some of the libraries require.

Notes for building on macOS:

There is one step that has to be done first before compiling the source code on the Mac. You must open a terminal and run the following:

xcode-select --install
This will install some command-line tools that are required to build the source code. Once that is completed, you can now proceed to build and install the compilers.

Building and Installation

The first thing you should realize is that building and installing GCC 7.1 is not going to interfere with any existing compilers on your computer. You will be building the source code in a temporary directoring and installing the files to an alternate location that isn't used by any other programs.

You will need about 7 GBs of space for all of the files that you are going to build. (There will end up being over 100,000 files.)

The location where the new files will be installed is here:

/usr/local/gcc-7.1
If you want to install it somewhere else, you can change the variable INSTALL_DIR in the script to another location. When the entire process has completed, the installation directory will contain these subdirectories:
bin
include
lib
lib32
lib64
libexec
share
The size of the installation is about on Linux is about 750 MB. The size on Mac is about 220 MB.

Steps to build and install:

  1. Get the archive of source files: gcc7.1-src.zip and put it in an empty temporary directory (somewhere under your home directory).
  2. Extract the archive in the temp directory:
    unzip gcc7.1-src.zip
    
    You should see these files:
    cleanup.sh
    gcc-7.1.0.tar.bz2
    gmp-6.1.2.tar.bz2
    install.sh
    isl-0.16.1.tar.bz2
    main.cpp
    mpc-1.0.3.tar.gz
    mpfr-3.1.5.tar.bz2
    remove-all.sh
    
    You may have to make the scripts executable by doing this:
    chmod +x *.sh
    
  3. Run the installation script:
    ./install.sh
    
    This could take anywhere from 20 minutes to 4 hours, depending on the speed of your computer. These are the times that I got from my computers:

    Those are the kinds of times you should expect. All of those computers had an SSD. If you are using a spinning hard disk, your times may be slightly longer. The largest factor in the speed of building is the number of cores your CPU has.

    I made the installation script automatically adjust to the number of cores you have, so a CPU with 2 threads will compile 2 files at once, and a CPU with 16 threads will compile 16 files at once. That is the beauty of make!

At the end of the build/install process, a sample C++ program will be compiled and run to verify that everything went well. The program will print out some numbers and stuff and you should see these lines at the end:

****************************************************************
* If you see this text, your build and install was successful! *
* If you see this text, your build and install was successful! *
* If you see this text, your build and install was successful! *
****************************************************************

To use the new version, add this to your $PATH:

  /usr/local/gcc-7.1/bin

You should probably do this in your ~/.bashrc or ~/.bash_profile file.
The test program advises you to add the bin directory in the installation directory to your PATH environment variable. Once you've done that, the compiler is ready to use.

To use the new version, you have to type:

g++-7.1
If you just type g++, you'll get the old version that was already installed on your computer.

Once you're sure that everything has been built and installed successfully, you can delete all of the files in your temporary directory as they are no longer needed.

Notes