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:
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:
These packages will give you a proper build environment, including 32-bit support, which some of the libraries require.sudo apt-get update sudo apt-get install build-essential gcc-multilib
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:
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.xcode-select --install
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:
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:/usr/local/gcc-7.1
The size of the installation is about on Linux is about 750 MB. The size on Mac is about 220 MB.bin include lib lib32 lib64 libexec share
Steps to build and install:
You should see these files:unzip gcc7.1-src.zip
You may have to make the scripts executable by doing this: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
chmod +x *.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:./install.sh
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:
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.**************************************************************** * 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.
To use the new version, you have to type:
If you just type g++, you'll get the old version that was already installed on your computer.g++-7.1
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.