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…

4 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.

4 Likes

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

3 Likes

Hi, wiremoons. Thanks for the tips. Unfortunately, they did not work for me while using gfortran on Mac OS Ventura. I even installed directly from the binaries provided by Apple here

https://gcc.gnu.org/wiki/GFortranBinariesMacOS

I keep facing the same issues, though :sleepy:

I was able to get co-array fortran to install, compile, and run on an M2 Mac running Ventura. The steps were;

Install all updates
install Xcode
install command line tools
install brew
brew install emacs coarray
Make sure path leads to correct compiler.

Oddly, this was almost the same steps to get everything working from Windows Subsystem for Linux;
Install WSL v2
apt update
install brew
brew emacs coarray
Make sure path leads to correct compiler.

After that everything was working on either platform. Now only if I had good projects to work on!..

Knarfnarf

@Knarfnarf ,

Did you take a look at this link? Perhaps you can collaborate with @certik on coarray parallel solutions to that effort, or with more detailed variations than fastGPT?

1 Like

I have not yet used the Ventura OS, but on earlier MacOS versions it is not necessary to install Xcode in order to get brew, gfortran, ifort, etc. to work. It is only the command line tool installation that is necessary. Xcode is about 40GB now, so it requires a lot of disk space and a lot of network bandwidth to install and keep up to date. The nice thing about Xcode is that it includes all the libraries to compile codes for Apple Macs, tablets, watches, phones, and home entertainment. But if you are only interested in software development on Macs, then you have the option to skip the other stuff.

1 Like

Yeah… But sometimes I do like having something else to code in than emacs. I haven’t done much swift or clang, but Xcode looks fun.

Knarfnarf

An alternative way without reinstalling gfortran or xcode is simply to add the following two lines in the Makefile

  1. LIB = -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
  2. gfortran -O5 -o (EXECNAME) (OBJ) $(LIB)