Building gcc 4.7 on MacOS X

Building gcc 4.7 on Mountain Lion is similar to building 4.6 on Lion, but there are some differences. Below is the whole procedure.

  1. Install Xcode and the command line tools.

    Xcode is Apple’s IDE. If you don’t already have it, download it via the Mac App store.  It’s quite large – around 2.4GB or so.

    As of Xcode 4.4, you can get the command line tools (compiler, assembler, linker, make and whatnot) by opening Xcode, going to the ‘Preferences’ menu, clicking the ‘Downloads’ panel, and clicking ‘Install’.

  2. Install the libraries that gcc needs.  Some of them can be installed through MacPorts.  However, MacPorts has other drawbacks (doesn’t have the correct versions of some dependencies, leading to build errors), so I prefer to install them myself.  The libraries are gmp, mpfr, mpc, ppl and cloog-ppl.

  3. First is gmp, the GNU Multiple Precision library.  MacOS X includes a version, but it’s unfortunately too old. Version 5.0.5 works fine. Version 5.1 and newer breaks ppl.

    $ curl -O http://ftp.gnu.org/gnu/gmp/gmp-5.0.5.tar.bz2
    $ tar -xjf gmp-5.0.5.tar.bz2
    $ cd gmp-5.0.5
    $ ./configure –prefix=/usr/local/gcc –enable-cxx
    $ make -j4
    $ sudo make install
    $ cd ..

  4. Next, install MPFR, the multiple precision floating point library.  It needs GMP.

    $ curl -O http://www.mpfr.org/mpfr-current/mpfr-3.1.1.tar.bz2
    $ tar -xjf mpfr-3.1.1.tar.bz2
    $ cd mpfr-3.1.1
    $ ./configure –prefix=/usr/local/gcc –with-gmp=/usr/local/gcc
    $ make -j4
    $ sudo make install
    $ cd ..

  5. MPC, the multiprecision complex library requires both GMP and MPFR.

    $ curl -O http://www.multiprecision.org/mpc/download/mpc-1.0.1.tar.gz
    $ tar -xzf mpc-1.0.1.tar.gz
    $ cd mpc-1.0.1
    $ ./configure –prefix=/usr/local/gcc –with-gmp=/usr/local/gcc
    $ make -j4
    $ sudo make install
    $ cd .. 

  6. The Parma Polyhedral Library, PPL, is needed for various gcc optimizations.  Version 0.11.x is needed for gcc 4.7.

    $ curl -O  http://bugseng.com/products/ppl/download/ftp/releases/0.11.2/ppl-0.11.2.tar.bz2
    $ tar -xjf ppl-0.11.2.tar.bz2
    $ cd ppl-0.11.2
    $ ./configure –prefix=/usr/local/gcc –with-gmp=/usr/local/gcc
    $ make -j4
    $ sudo make install

  7. Lastly, CLooG, the Chunky Loop Generator.  gcc needs the version specifically designed for PPL.

    $ curl -O ftp://gcc.gnu.org/pub/gcc/infrastructure/cloog-ppl-0.15.11.tar.gz
    $ tar -xzf cloog-ppl-0.15.11.tar.gz
    $ cd cloog-ppl-0.15.11
    $ ./configure –prefix=/usr/local/gcc –with-gmp=/usr/local/gcc –with-ppl=/usr/local/gcc
    $ make -j4
    $ sudo make install
    $ cd .. 

  8. Finally, we’re ready to build gcc.

    The download is around 80MB.  Note that gcc should not be built inside its own source directory.

    $ curl -O http://ftp.gnu.org/gnu/gcc/gcc-4.7.2/gcc-4.7.2.tar.bz2
    $ tar -xjf gcc-4.7.2.tar.bz2
    $ mkdir objdir
    $ cd objdir

    The build itself takes some time (the compiler actually rebuilds itself twice to verify that it’s fully functional).

    $ ../gcc-4.7.2/configure –enable-languages=c,c++,objc,obj-c++,fortran –disable-nls \
    –with-gmp=/usr/local/gcc –with-mpfr=/usr/local/gcc –with-mpc=/usr/local/gcc \
    –with-ppl=/usr/local/gcc –with-cloog=/usr/local/gcc –prefix=/usr/local/gcc \
    $ make bootstrap -j4
    $ sudo make install

  9. The newly installed compiler is in /usr/local/gcc/bin/. You can either add the directory to $PATH, or add symbolic links to gcc and g++ in /usr/local/bin.

Comments are closed.