Compiling GCC 6 on OS X

Posted on May 10, 2016 by Sol

UPDATE 22 September 2016

If you are using macOS Sierra check the new tutorial.

In this tutorial, I will show you how to compile from source and install the current stable version of GCC with Graphite loop optimizations on your OS X computer. The instructions from this tutorial were tested with Xcode 7.3 and El Capitan (OS X 10.11).

Clang, the default compiler for OS X, supports only C, C++ and Objective-C. If you are interested in a modern Fortran compiler, e.g. you will need gfortran that comes with GCC. Another reason to have the latest stable version of GCC on your Mac is that it provides you with an alternative C and C++ compiler. Testing your code with two different compilers is always a good idea.

In order to compile GCC from sources you will need a working C++ compiler. In the remaining of this article I will assume that you have installed the Command Line Tools for Xcode. At the time of this writing Apple’s Command Line Tools maps the gcc and g++ to clang and clang++. If you don’t have the Command Line Tools installed, open a Terminal and write:

1 xcode-select --install


which will guide through the installation process.

We will also need three other libraries for a successful build of gcc: mpc, mpfr and gmp. Use the above links and download the last versions for all of them: gmp-6.1.0a.tar.bz2, mpc-1.0.3.tar.gz and mpfr-3.1.4.tar.bz2, also save them in your Downloads folder.

For enabling the Graphite loop optimizations you will need two extra libraries, go to ftp://gcc.gnu.org/pub/gcc/infrastructure/ and download isl-0.16.tar.bz2.

We will start by compiling the gmp library:

1 cd ~
3 cd gmp*


Create a new folder named build in which the compiler will save the compiled library:

1 mkdir build && cd build


And now the fun part … write in your Terminal:

1 ../configure --prefix=/usr/local/gcc-6.1.0 --enable-cxx


If you see no error message we can actually compile the gmp library:

1 make -j 4


In a few minutes you will have a compiled gmp library. If you see no error message … congratulations, we are ready to install the library in the /usr/local/gcc-6.1.0 folder (you will need the administrator password for this):

1 sudo make install


We will do the same steps for MPFR now:

1 cd ..
2 cd ..
3 cd mpfr*
4 mkdir build && cd build


Configuration phase:

1 ../configure --prefix=/usr/local/gcc-6.1.0 --with-gmp=/usr/local/gcc-6.1.0


The second parameter will just inform the configure app that gmp is already installed in /usr/local/gcc-6.1.0.

After the configure phase is finished, we can make and install the library:

1 make -j 4
2 sudo make install


Now, we are going to build MPC:

1 cd ..
2 cd ..
3 cd mpc*
4 mkdir build && cd build
5 ../configure --prefix=/usr/local/gcc-6.1.0 --with-gmp=/usr/local/gcc-6.1.0 --with-mpfr=/usr/local/gcc-6.1.0
6 make -j 4
7 sudo make install


At this time you should have finished to build and install the necessary prerequisites for GCC.

Next step is to build the library for the Graphite loop optimizations:

1 cd ..
2 cd ..
3 cd isl*
4 mkdir build && cd build
5 ../configure --prefix=/usr/local/gcc-6.1.0 --with-gmp-prefix=/usr/local/gcc-6.1.0
6 make -j 4
7 sudo make install


We are ready to compile GCC now. Be prepared that this could take more than one hour on some machines … Since I’m interested only in the C, C++ and Fortran compilers, this is the configure command I’ve used on my machine:

1 cd ..
2 cd ..
3 cd gcc*
4 mkdir build && cd build
5 ../configure --prefix=/usr/local/gcc-6.1.0 --enable-checking=release --with-gmp=/usr/local/gcc-6.1.0 --with-mpfr=/usr/local/gcc-6.1.0 --with-mpc=/usr/local/gcc-6.1.0 --enable-languages=c,c++,fortran --with-isl=/usr/local/gcc-6.1.0 --program-suffix=-6.1.0


The above command instructs the configure app where we have installed gmp, mpfr, mpc and isl; also it tells to add a prefix to all the resulting executable programs, so for example if you will invoke GCC 6.1.0 you will write gcc-6.1.0, the gcc command will invoke Apple’s version of clang.

If you are interested in building more compilers available in the GCC collection modify the –enable-languages configure option.

And now, the final touches:

1 make -j 4


Grab a coffee, maybe a book, and wait … this should take approximately, depending on your computer configuration, an hour … or more … and about 4.4GB of your disk space for the build folder.

Install the compiled gcc in /usr/local/gcc-6.1.0:

1 sudo make install


Now, you can keep the new compiler completely isolated from your Apple’s gcc compiler and, when you need to use it, just modify your path by writing in Terminal:

1 export PATH=/usr/local/gcc-6.1.0/bin:\$PATH


If you want to avoid writing the above command each time you open a Terminal, save the above command in the file .bash_profile from your Home folder.

You should be able to invoke any of the newly compiled compilers C, C++, Fortran …, invoking g++ is as simple as writing in your Terminal:

1 g++-6.1.0 test.cpp -o test


Next, I’ll show you how to check if the compiler was properly installed by compiling and running a few examples. GCC 6 uses by default the C++14 standard and C11 for the C coders, you should be able to compile any valid C++14 code directly. In your favorite text editor, copy and save this test program (I’ll assume you will save the file in your Home directory):

 1 //Program to test the C++ lambda syntax and initializer lists
2 #include <iostream>
3 #include <vector>
4
5 using namespace std;
6
7 int main()
8 {
9   // Test lambda
10   cout << [](int m, int n) { return m + n;} (2,4) << endl;
11
12   // Test initializer lists and range based for loop
13   vector<int> V({1,2,3});
14
15   cout << "V =" << endl;
16   for(auto e : V) {
17     cout << e << endl;
18   }
19
20   return 0;
21 }


Compiling and running the above lambda example:

1 g++-6.1.0 tst_lambda.cpp -o tst_lambda
2 ./tst_lambda
3 6
4 V =
5 1
6 2
7 3


We could also compile a C++ code that uses threads:

 1 //Create a C++ thread from the main program
2
3 #include <iostream>
5
6 //This function will be called from a thread
8     std::cout << "Hello, World!" << std::endl;
9 }
10
11 int main() {
14
16     t1.join();
17
18     return 0;
19 }


Next, we present a simple C++ code that uses regular expressions to check if the input read from stdin is a floating point number:

 1 //Uses a regex to check if the input is a floating point number
2
3 #include <iostream>
4 #include <regex>
5 #include <string>
6
7 using namespace std;
8
9 int main()
10 {
11   string input;
12   regex rr("((\\+|-)?[[:digit:]]+)(\\.(([[:digit:]]+)?))?((e|E)((\\+|-)?)[[:digit:]]+)?");
13   //As long as the input is correct ask for another number
14   while(true)
15   {
16     cout<<"Give me a real number!"<<endl;
17     cin>>input;
18     if(!cin) break;
19     //Exit when the user inputs q
20     if(input=="q")
21       break;
22     if(regex_match(input,rr))
23       cout<<"float"<<endl;
24     else
25     {
26       cout<<"Invalid input"<<endl;
27     }
28   }
29 }


If you are a Fortran programmer, you can use some of the Fortran 2008 features like do concurrent with gfortran-6.1.0:

 1 integer,parameter::mm=100000
2 real::a(mm), b(mm)
3 real::fact=0.5
4
5 ! initialize the arrays
6 ! ...
7
8 do concurrent (i = 1 : mm)
9 	a(i) = a(i) + b(i)
10 enddo
11
12 end


The above code can be compiled with (assuming you’ve named it tst_concurrent_do.f90):

1 gfortran-6.1.0 tst_concurrent_do.f90 -o tst_concurrent_do
2 ./tst_concurrent_do


If you are interested in learning more about the new C++11/C++14 syntax I would recommend reading The C++ Programming Language by Bjarne Stroustrup.

or, Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper 2nd edition:

If you need to brush your Fortran knowledge a good book is Modern Fortran Explained by M. Metcalf, J. Reid and M. Cohen: