Running Fortran on a Mac

Hello all,
I am using a MacBook Air, I installed the Xcode application and command line tools from apple, I installed the gfortran binary files and have a code editor to write the fortran files in. indeed I can invoke Fortran from the terminal and “gfortran —version” yields the version information I am running. So the installation seems successful.
However, when I write my program in the code editor and save it as a name.f90 file. I am not sure what directory to store it in. When I try to compile it from the terminal prompt it cannot seem to find it. I am not sure what directory to store my file in or how to change the current directory in fortran to the one my file is in. Apple tends to hide the directory structure in finder.
I am hoping that I am just missing something very simple. Is anyone able to lend some advice?
Kind regards,
Rick

1 Like

The Fortran source code can be stored in any directory. Use the cd command to navigate to that directory. If the gfortran (or whatever compiler is used) command is not found, you need to add the directory where the compiler is installed to your $PATH. There is info online about how to set your $PATH.

1 Like

Hello, Thank you for your response!

I can find the gfortran command, but I don’t know where to store my f90 file or how to point gfortran to the directory I store it in.

Apple doesn’t give you much visibility of the directory structure. For instance, say I save it on my desktop. I run the cd command to “Macintosh HD: desktop” and it tells me it is not found.

Do you know what I am doing wrong?
Rick

Normally you want to operate gfortran in any directory of the system. To do this you need to have gfortran visible in any directory, which requires adding the path to the directory containing gfortran to $PATH, or aliasing gfortran to to the file path to the compiler. The later is useful if you have more than one version of gfortran installed in your computer. By default gfortran searches for the source code by name in the directory in which you execute the command and stores the binary in the same directory. However you can override this default by providing file paths to the source code and the binary output files. The file paths can either be relative to the directory in which you execute the gfortran command, or absolute, i.e. relative to the base directory “/”.

1 Like

Perhaps this tutorial can help: How to navigate files and folders in Terminal | Macworld

Assuming default locations I think the command cd ~/Desktop should get you there.

2 Likes

Hi guys,
Okay - “cd desktop/” seems to fix the problem. I had to update everything to Mac OS Monterey so that slowed me down. To keep it simple I am trying the hello-world code just to make sure it all works. Now I get the attached error:
Screen Shot 2021-11-06 at 2.10.11 PM
Wondering if I need to reinstall the binary files?
Rick

How did you install gfortran?

I am on a mac, and I install gfortran using Conda:

conda create -n gf gfortran
conda activate gf

This works on both Intel and M1 Macs.

2 Likes

I reinstalled fortran after updating to Monterey and it works now!

Thanks all for the help, apparently by typing “Xcode-select --install” at the command line it reinstalled? I’d really like to understand what I did here. How does X code know to install fortran by typing the above line?
Rick

Also what is the difference in running “gfortran hello.f90” and “gfortran hello.f90 -o hello”?

Screen Shot 2021-11-06 at 2.50.34 PM

The -o <placeholder> means write output to file. If you omit the -o, the output will be (typically) placed in a.out or a.exe (Windows).

Now that you have a working Fortran compiler, it’s a good time to read the tutorial’s Building programs - Fortran Programming Language and Quickstart tutorial - Fortran Programming Language.

I have installed Fortran on several MacOS machines, so I can share a few hints:

  1. A very good (and widely used) package manager for MacOS (but also for Linux) is Homebrew. Once installed, you just write brew install gcc to get the newest (now, 11) version of gcc, including gfortran
  2. Contrary to what can be read in many places, the full Xcode (which is several GB) is not needed to use Homebrew. You only need command line developer tools, installed by xcode-select --install
  3. One can also install Intel ifort compiler (although it does not support coarrays - be warned). Also, at least on my macs running BigSur, when installed from m_HPCKit_p_2021.4.0.3389_offline.dmg file downloaded from Intel’s pages, it was failing to build executables, complaining about lack of System library. This can be fixed by using -L/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/lib/ option to ifort.

HTH

1 Like

Thanks for all the help guys, it really does help. I think I have moved on past this problem now in large part to all of your help. Thank you!

3 Likes