Unable to compile Fortran code using gfortran on Mac OS

Hi all,

I have a macbook pro late 2019 (Intel), running with Mac OS 12.1. I cannot compile Fortran code using gfortran, configured using brew, since I get this error at link time:

$ gfortran whatever-fortran-program.f90
ld: library not found for -lSystem
collect2: error: ld returned 1 exit status

trying this SO solution:

export LIBRARY_PATH="$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"

I get a different error:

ld: file not found: /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation for architecture x86_64
collect2: error: ld returned 1 exit status

Has anyone experienced this issue and know how to circumvent it? Thanks!

PS: I know I can install other compilers on Mac OS, but I want gfortran since I want to use the OpenMPI build in brew.

I fixed that by reinstalling CommandLineTools from the developer account on Apple’s website. I believe Xcode doesn’t properly upgrade CommandLineTools.

4 Likes

I was getting a similar error recently as: gfortran collect2: error: ld returned 1 exit status when building fpm.

This was on macOS 12.0 running on Apple Silicon M1 instead of Intel - but I expect it is not hardware related anyway :slight_smile:

When I ran the brew doctor command it suggested re-installing the XCode CLI tools.

I first removed the old tools (but not sure if that is needed however…) and then reinstalled them. Commands used in a macOS Terminal window were:

sudo rm -Rf /Library/Developer/CommandLineTools
sudo xcode-select --install

As @mhulsen stated - I don’t think Apple upgrades the command line tools unfortunately, so things just break occasionally and unexpectedly by the looks of it…

3 Likes

On both Apple Intel and Apple M1, I install gfortran using conda:

conda create -n gf gfortran
conda activate gf

and everything seems to just work for me.

3 Likes

Thank you very much, that solved my problem! :smiley:

3 Likes