Installing gfortran and GCC 4.6 on Snow Leopard

The reason I wanted to install GCC 4.6 on my machine was that my Intel Fortran compiler had trouble linking with my GCC 4.2.1 version, which is the default installation on my Snow Leopard. I dreaded doing the compilation from source as I knew it would be error prone and time consuming, and it did not turn out to good either. Alas, after following the instructions on the GNU website my compilation of GCC ended in error with a strange error message

checking for suffix of object files... configure: error: in `/Users/trond/GMT/gcc/OBJECTDIR/x86_64-apple-darwin10.6.0/libgcc':
configure: error: cannot compute suffix of object files: cannot compile
See `config.log'
for more details.
make[2]: *** [configure-stage1-target-libgcc] Error 1
make[1]: *** [stage1-bubble] Error 2
make: *** [all] Error 2

I could not find a solution to his error message and I still have not understood what it means. Any comments are highly appreciated as it would be nice to compile from source. But to my luck I found the website of http://hpc.sourceforge.net/ which offers Intel Snow Leopard binaries for GCC 4.6! First I made a backup of my /usr/local/ directory using 7-zip (excellent compressor):

7za a -tzip usrlocal.zip /usr/local

Then I downloaded the files for GCC 4.6 for Intel Snow Leopard (found here) and installed the files following the recipe on the website of HPC:

gunzip gcc-bin.tar.gz
sudo tar -xvf gcc-bin.tar -C /

Next I also downloaded the Fortran compiler gfortran binaries that was compiled for GCC4.6 and installed them according to the HPC website:

gfortran-snwleo-intel-bin.tar.gz
sudo tar -xvf gfortran-snwleo-intel-bin.tar -C /

The files for GCC4.6 and gfortran installs by default into /usr/local (make sure you make a copy prior to installation), which you have to add to your PATH environment in .bash_profile:

export PATH=/usr/local:$PATH

After these installations I was finally able to compile and link my fortran programs using both my Intel Fortran compiler and my new gfortran compiler. And it worked perfectly thanks to the great binaries from Gaurav Khanna at http://hpc.sourceforge.net/.

 trond@trondlaptop [00:02:40]> gcc --version
gcc (GCC) 4.6.0 20101106 (experimental)
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  trond@trondlaptop [00:02:45]> gfortran --version
GNU Fortran (GCC) 4.6.0 20101106 (experimental)
Copyright (C) 2010 Free Software Foundation, Inc.

GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING

  trond@trondlaptop [00:02:57]> gfortran -o2 kmeans.f90 kmeans_prb.f90 -o kmeans
  trond@trondlaptop [00:03:04]>

Before I found all the binaries at HPC, i started my installation of GCC 4.6 from source and below is a list of the programs I managed to install by hand. To compile programs for 64bit on Snow Leopard I defined the following flags in my .bash_profile:

export CC=/usr/bin/gcc
export CXX=/usr/bin/g++
export FC=ifort
export F90=ifort
export F77=ifort
export CFLAGS='-shared-intel -arch x86_64 -pipe -no-cpp-precomp -Df2cFortran -fno-common'
export CXXFLAGS='-shared-intel -arch x86_64'
export FFLAGS='-fPIC -shared-intel -arch x86_64'
export MACOSX_DEPLOYMENT_TARGET=10.6

Install The GNU Multiple Precision Arithmetic Library. To be able to compile for 64bit I had to use version 5.0.1:

./configure --prefix=/usr/local --enable-cxx
make
make check
sudo make install

Install the The Parma Polyhedra Library:

./configure --prefix=/usr/local --enable-cxx
make
make check
sudo make install

Install the The GNU MPC package:

./configure --prefix=/usr/local
make
make check
sudo make install

To enable fortran in the compilation of GCC you need the GNU MPFR library:

./configure --prefix=/usr/local
make
make check
sudo make install

2 thoughts on “Installing gfortran and GCC 4.6 on Snow Leopard”

  1. hi, thanks for the guide, but I encountered a problem.

    so I instaled gcc 4.6 and gfortran for snow leopard from hpc.
    It was a succeed til I put the command gfortran.

    it supposed to show gfortran: no input files, but mine is gfortran: fatal error: no input files
    compilation terminated.

    can you help me? any help would be appreciated.
    thanks!

  2. Thanks,
    I was able to upgrade macports version of libgcc using your help and afterwards

    >sudo port -v install libgcc configure.compiler=gcc

    apparently macported compilers failed to compile their own version of libgcc due to unfixed bugs in Snow Leopard.

Leave a Reply

Your email address will not be published. Required fields are marked *