Compiling Fortran applications on macOS Monterey
I recently stumbled upon the task to compile a Fortran application on my new macOS Monterey/Apple M1 system. It was not as straightforward as I thought, most likely because of the Anaconda distribution I have installed on my system. Here’s why.
Reference Forward Model (RFM)
The application I want to compile is RFM, a radiative transfer model originally developed at AOPP, Oxford University, under an ESA contract to provide reference spectral calculations for the Envisat/MIPAS instrument.
I was handed over the source code of RFM v4.3.5, which, if I am not mistaken, was still written in FORTRAN77 (a subsequent version was released in 2018, rewritten in FORTRAN90).
Installing the Fortran compiler
Since I am using Anaconda on my system, I naturally went to check if it also provides a Fortran compiler, and, sure enough, you can install GFortran with this command:
$ conda install -c conda-forge gfortran
After some conda rumbling and tumbling, I finally had a Fortran compiler installed on my system: time to compile the program!
Compile time
The source package looks like this:
With no readme in sight I first had to figure what to call the Fortran compiler with. It turns out to be just a matter of specifying the output file and including all the source files:
$ gfortran -o rfm *for
But, when I execute that command, I get this:
1 | smattia@EST01109520:~/src/v4.35 $ gfortran -o rfm *for |
Uh-oh, what’s going on here? With the exception of the warning messages, which don’t appear particularly worrying, the last error message looks quite alarming. Libraries not found? Sounds like something is wrong with my development environment.
Googling the ld: library not found for -lm
message is not very useful as it is quite an everyday error: it would appear that libm
is missing in my system, but this is such a common library that I would be very much surprised if my system still worked without it.
There must be something else at play.
Anaconda and Xcode conflicts
After some serious googling around, it turns out that there might be an incompatibility between the linker used by Anaconda and the development environment provided by Xcode 14.2 installed on my system.
The Anaconda provided ld
only supports Apple TAPI version 10.0.0:
1 | smattia@EST01109520:~/src/v4.35 $ which ld |
Whereas, Xcode’s linker supports apple TAPI version 14.0.0:
1 | smattia@EST01109520:~/src/v4.35 $ /usr/bin/ld -v |
It wasn’t trivial to track down this issue: apparently this problem only shows up when attempting to compile Fortran application, and only one other unfortunate user reported it on Github
The workaround
The obvious workaround in this circumstance is to compile the application outside of the Anaconda environment. I installed another GFortran via brew
$ brew install gcc
and temporarily removed /opt/anaconda3/bin
from my $PATH
.
1 | smattia@EST01109520:~/src/v4.35 $ which gfortran |
It worked!
We will see how to use this application to model the radiative transfer properties of the atmosphere for different tangent heights in another post.
For now, happy Fortran compiling!