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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
smattia@EST01109520:~/src/v4.35 $ gfortran -o rfm *for
atmiso.for:72:17:

70 | DO IISO = 0, NISGAS(IGAS)
| 2
71 | ISOGAS(IISO,NGAS) = ISOGAS(IISO,IGAS)
72 | WGTGAS(IISO,NGAS) = WGTGAS(IISO,IGAS)
| 1
Warning: Array reference at (1) out of bounds (0 < 1) in loop beginning at (2)
atmiso.for:72:37:

70 | DO IISO = 0, NISGAS(IGAS)
| 2
71 | ISOGAS(IISO,NGAS) = ISOGAS(IISO,IGAS)
72 | WGTGAS(IISO,NGAS) = WGTGAS(IISO,IGAS)
| 1
Warning: Array reference at (1) out of bounds (0 < 1) in loop beginning at (2)
ld: library not found for -lm
collect2: error: ld returned 1 exit status

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
2
3
4
5
6
7
8
smattia@EST01109520:~/src/v4.35 $ which ld
/opt/anaconda3/bin/ld
smattia@EST01109520:~/src/v4.35 $ ld -v
@(#)PROGRAM:ld PROJECT:ld64-530
BUILD 07:42:29 Sep 7 2022
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
LTO support using: LLVM version 14.0.6 (static support for 26, runtime is 29)
TAPI support using: Apple TAPI version 10.0.0 (tapi-1000.10.8)

Whereas, Xcode’s linker supports apple TAPI version 14.0.0:

1
2
3
4
5
6
smattia@EST01109520:~/src/v4.35 $ /usr/bin/ld -v
@(#)PROGRAM:ld PROJECT:ld64-820.1
BUILD 20:07:05 Nov 7 2022
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
LTO support using: LLVM version 14.0.0, (clang-1400.0.29.202) (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 14.0.0 (tapi-1400.0.11)

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
smattia@EST01109520:~/src/v4.35 $ which gfortran
/Users/smattia/homebrew/bin/gfortran
smattia@EST01109520:~/src/v4.35 $ which ld
/usr/bin/ld
smattia@EST01109520:~/src/v4.35 $ gfortran -o rfm *for
atmiso.for:72:17:

70 | DO IISO = 0, NISGAS(IGAS)
| 2
71 | ISOGAS(IISO,NGAS) = ISOGAS(IISO,IGAS)
72 | WGTGAS(IISO,NGAS) = WGTGAS(IISO,IGAS)
| 1
Warning: Array reference at (1) out of bounds (0 < 1) in loop beginning at (2)
atmiso.for:72:37:

70 | DO IISO = 0, NISGAS(IGAS)
| 2
71 | ISOGAS(IISO,NGAS) = ISOGAS(IISO,IGAS)
72 | WGTGAS(IISO,NGAS) = WGTGAS(IISO,IGAS)
| 1
Warning: Array reference at (1) out of bounds (0 < 1) in loop beginning at (2)
smattia@EST01109520:~/src/v4.35 $ ll rfm
.rwxr-xr-x smattia 1276952531 994 KB Fri Dec 16 09:45:19 2022  rfm
smattia@EST01109520:~/src/v4.35 $ ./rfm
R-RFM: Program RFM v4.35_01MAY
Optional ID to be appended to filenames (<CR>=none):
^C

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!