Installing gcc and gfortran for Mac OS X (10.7.3)

Things you’ll need:

  • Knowledge of how to use the terminal
  • An internet connection
  • A Mac developer account (you can get this as we go along)
  • Copy of Xcode (free)
  • About an hour of your time (30 minutes downloading, 15-30 minutes doing things)

Basic steps:

  1. Download and install Xcode
  2. Download command line tools
  3. Download and install gfortran from other source
Note that if you attempt to only download and install gfortran without gcc you might get the following error!

error trying to exec `as': execvp: No such file or directory

Also note that I performed this installation on a Macbook Air.

gcc

Download and install Xcode by clicking this link, or by searching for it in the Apple App Store, where it can be downloaded for free (see image).

Xcode contains gcc

After you’ve downloaded Xcode, you’ll want to open it and agree to their terms of service. Then, you’ll want to navigate to the menu Xcode –> Preferences –> Downloads. Here you’ll see an option to download Command Line Tools (see image). Note that you’ll need a developer account at this stage, and I was redirected to their developer page where I had to fill out a form and create my account (using my existing Apple ID, where a lot of the form was already auto-filled).

CL Tools

Download Command Line Tools from Preferences --> Downloads

After you have successfully installed the command line tools, open your terminal and type something like:

$ which gcc

which should return the path of your gcc in /usr/local/bin. All of this should have been taken care of automatically.
gfortran

I mentioned at the beginning that I got an error when attempting to use gfortran on my machine before I’d even installed gcc. I found that gcc must be installed in order to use gfortran. But my gfortran installation went smooth because it’s very straightforward.

Download gfortran from this link.

After considering my hardware, I chose the option:

Mac OS Lion (10.7) on Intel 64-bit processors (gfortran 4.6.2): download (released on 2011-10-20)

The installation has a walkthrough that comes with the package, like many Mac installations. Straightforward and it should also work automatically. Then, open your terminal and type

$ which gfortran

and it should reveal that it was successfully installed in /usr/local/bin.

Happy programming!

About these ads

16 thoughts on “Installing gcc and gfortran for Mac OS X (10.7.3)

  1. I dont know who you are but you are awesome. I have spent all day trying to figure out how to get gcc to work on the terminal for my hw and all the other tutorials didnt work. Thanks

  2. Hi, thank you for putting all these notes together. I am not a very committed developer of Mac/iOS apps, so I am not too sure that I want to install Xcode. Can one install only the gfortran and gcc bundle without Xcode, which would take more than 1Gb?
    At present, my ‘which gfortran’ and ‘which gcc’ are both pointing to /usr/local/bin
    I hope you don’t mind if I share with you some details of this installation. If this is not appropriate, please delete/unapprove this comment.

    $ which gfortran
    /usr/local/bin/gfortran

    $ ls -l /usr/local/bin/gfortran
    lrwxr-xr-x 1 root wheel 32 12 Mar 22:42 /usr/local/bin/gfortran -> /usr/local/gfortran/bin/gfortran

    $ ls -l /usr/local/gfortran/bin/gfortran
    -rwxr-xr-x 1 root admin 404900 20 Ott 2011 /usr/local/gfortran/bin/gfortran

    $ gfortran -v
    Using built-in specs.
    COLLECT_GCC=gfortran
    COLLECT_LTO_WRAPPER=/usr/local/gfortran/libexec/gcc/x86_64-apple-darwin11/4.6.2/lto-wrapper
    Target: x86_64-apple-darwin11
    Configured with: ../gcc-4.6.2-RC-20111019/configure –prefix=/usr/local/gfortran –with-gmp=/Users/fx/devel/gcc/deps-static/x86_64 –enable-languages=c,c++,fortran,objc,obj-c++ –build=x86_64-apple-darwin11
    Thread model: posix
    gcc version 4.6.2 20111019 (prerelease) (GCC)

    Yours,
    Massimo

  3. My ‘which gfortran’ statement had same results as Massimo. Xcode and command tools seem to be installed correctly but I am having trouble compiling old code and setting up a project. 10 years since I worked with Fortran and then on mainframe. I can’t seem to force Xcode 4.6 to understand I am using fortran. I read that the target should be changed to the fortran compiler and the main should be changed to fortran. I changed the main but can’t figure out the part about the target and build rules. Can you help or lead me to a site? Thanks

  4. Additional info.
    When I try to set up a new build rule I double click on the target, this brings up a window that allows me to build a new rule.
    The PROCESS option includes Fortran source which I assume is correct but
    the Using options do not include the Gfortran compiler.
    The command ‘which gfortran’ returned
    /usr/local/bin/gfortran
    should that have been /usr/local/bin
    and if so how do I make that happen?

    Thanks for your time

  5. I have followed your instructions and run the example very good. I am running a program reading an external file. The reading is running well. However when a try to write in an external file I have an error message. What I have to do?

  6. The following is a segment of the program,the idea is to write in UNIT 2 as was identified with the path where is the file:

    OPEN (UNIT=2,FILE=”/Users/Gabriel/Documents/MANUNI2.SAL”)

    OPEN (UNIT=1,FILE=”/Users/Gabriel/Documents/MANUNI2.DAT”)
    ! READ (2,*) PREPARA
    ! WRITE (*,*) PREPARA
    ! WRITE (2,*) PREPARA

    READ (1,*) CANTID
    WRITE (*,*) “INICIA ECO DE DATOS”
    WRITE (2,*) “EN MANIU2.SAL INICIA ECO DE DATOS Y LUEGO LEE CANTID”
    OPEN (UNIT=2,FILE=”/Users/Gabriel/Documents/MANUNI2.SAL”)
    WRITE (2,*) CANTID
    WRITE (*,*) CANTID

    !C LEE UNIDADES INICIALES UNID(I)

    DO 17 I=1,50

    READ (1,*) UNID(I)
    WRITE (*,*) UNID(I)
    WRITE (2,*) UNID(I)

    IF(UNID(I).EQ.’*’) GOTO 17

    IF (UNID(I).EQ.’CONVIERTE’) GOTO 18

    READ (1,*) EXP(I)
    WRITE (*,*) EXP(I)

    17 CONTINUE

    !C RENGLON ANTES DE CONVIERTE

    18 LIM1LEC=I-1
    WRITE(*,*) “LIM1LEC=”, LIM1LEC

    !C LEE UNIDADES TARGET UNID2(I)

    DO 20 I=1,50

    READ (1,*,ERR=21,END=21) UNID2(I)
    WRITE (*,*) UNID2(I)

    IF(UNID2(I).EQ.’*’) GOTO 20

    READ (1,*,ERR=21,END=21) EXP2(I)
    WRITE (*,*) EXP2(I)

    20 CONTINUE

    21 LIM2LEC=I-1
    WRITE (*,*) “LIM2LEC=”, LIM2LEC

    WRITE (*,*) CANTID

    WRITE (2,*) CANTID

    The LOG error message is:
    fortran -c /Users/Gabriel/Documents/MANUNIMAC.f90 -o /Users/Gabriel/Documents/MANUNIMAC.o
    gfortran -o /Users/Gabriel/Documents/MANUNIMAC /Users/Gabriel/Documents/MANUNIMAC.o
    Process terminated with status 0 (0 minute(s), 0 second(s))
    0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

    Checking for existence: /Users/Gabriel/Documents/MANUNIMAC
    Executing: osascript -s “s” -e ‘tell app “Terminal”‘ -e ‘activate’ -e ‘do script “/Users/Gabriel/Documents/MANUNIMAC” ‘ -e ‘end tell’ /Applications/CodeBlocks.app/Contents/MacOS/cb_console_runner (in /Users/Gabriel/Documents)
    Process terminated with status 0 (0 minute(s), 1 second(s))

    The error message in TERMINAL after run the program is:

    Last login: Sun Mar 16 20:50:14 on ttys000
    MacBook-Pro-de-Gabriel:~ Gabriel$ /Users/Gabriel/Documents/MANUNIMAC
    INICIA ECO DE DATOS
    100.00000000000000
    K
    1
    CONVIERTE
    LIM1LEC= 1
    F
    1
    LIM2LEC= 1
    100.00000000000000
    K 1

    CONVERTIR A

    F 1

    Program received signal SIGSEGV: Segmentation fault – invalid memory reference.

    Backtrace for this error:
    #0 0x1037b3e62
    #1 0x1037b462e
    #2 0x7fff86781909
    Segmentation fault: 11
    MacBook-Pro-de-Gabriel:~ Gabriel$

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s