14

I am getting a strange error when trying to compile a simple fortran-90 file using gfortran. I am working on a macOS mojave system. I have gfortran 8.2.0 installed and I checked to be sure of this by doing the following:

Input: gfortran --version
Output: GNU Fortran (GCC) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE.

Which indicates that gfortran installed correctly and I also confirmed this by typing simply: gfortran, into the terminal, which returns:

gfortran: fatal error: no input files
compilation terminated.

However, when I try to compile my file like so:

gfortran hello.f90 -o hello.x

I get an error which reads:

ld: library not found for -lSystem
collect2: error: ld returned 1 exit status

I've tried compiling in different ways and I also tried setting the LD_LIBRARY_PATH in .bash_profile as described by https://gcc.gnu.org/wiki/GFortranBinaries32Linux but I still get the same error. The gfortran directory has the pathname: /usr/local/gfortran, however another gfortran which is not a directory can be found in /usr/local/bin

Typing:

which gfortran

Outputs:

/usr/local/bin/gfortran

And a long list search of this directory:

Input: ls -l /usr/local/bin/gfortran
Output: lrwxr-xr-x  1 root  wheel  32 15 May 14:07 
/usr/local/bin/gfortran -> /usr/local/gfortran/bin/gfortran

How can I fix this where everything seems to be correctly installed?

Any help would be greatly appreciated!

datasplice
  • 494
  • 1
  • 3
  • 16
  • 2
    Do you have the xcode command line tools installed? Depending on the gfortran binary, those might be needed for stuff to work. You might also want to use e.g. the homebrew builds, those are kept up to date. – janneb May 16 '19 at 08:47
  • 2
    @janneb I do have command line tools installed for xcode, and I tried the homebrew build on another apple setup and the same problem occurred but I can try it on this computer – datasplice May 16 '19 at 12:14
  • Command line tools is outdated. Solution: `sudo rm -rf /Library/Developer/CommandLineTools`, then `xcode-select --install`, `sudo xcode-select --switch /Library/Developer/CommandLineTools`, update Xcode if it's too out-dated. After all, you might like to `sudo xcode-select -s /Applications/Xcode.app` if you installed Xcode. – DawnSong Feb 25 '22 at 13:37

9 Answers9

25

On macOS Big Sur v11.1:

Relevant SO post: https://apple.stackexchange.com/questions/408999/gfortran-compiler-error-on-mac-os-big-sur

The fix is to add the stdlib to your $LIBRARY_PATH. For some reason or another it isn't in your standard $PATH anymore on 11.1.

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

(or add it to login file and restart terminal).

Jacob Faib
  • 1,062
  • 7
  • 22
  • could you ELI5 this? what is a stdlib and a $LIBRARY_PATH? In particular, where do I run the code snippet you shared? Thanks! – user551504 Feb 02 '21 at 01:58
  • 2
    > "what is a stdlib" Around the time of Aristotle programmers thought to themselves "hmm we seem to be writing the same basic code over and over again, maybe we should standardize it". And so `stdlib` was born. It contains all the basic building blocks like memory allocation, random number generators, string to X conversion, process and system control and even some basic sort routines. It is essential for almost any program in existence. – Jacob Faib Feb 02 '21 at 15:37
  • 2
    > $LIBRARY_PATH [Note: `$LIBRARY_PATH` is for gcc only!] Your computer is a big box of junk, and yet somehow the magic fairies working for the compiler and linker know exactly where to look for `myBoolLib.so` so that you can use `bool invertBoolean(bool in);`. How do they know where to look? You could tell them in fine grained detail exactly where to look every time you compile but that's *tedious*. So you declare what is essentially a system-wide variable that lists a bunch of places that the aforementioned fairies should look when compiling. – Jacob Faib Feb 02 '21 at 15:53
  • 1
    > "where do I run the code snippet you shared?" From the command line, but beware you will have to do it every time. Alternatively you can put it in your "login file", which assuming you're using bash is `~/.bash_profile` and/or `~/.bashrc`. These are run every time you open a new terminal and serve as a sort of "customization" file. If you paste the above code in either, it will auto-magically execute whenever you create a new terminal instance. – Jacob Faib Feb 02 '21 at 15:58
  • 1
    On my Big Sur system it's actually at a slightly different path: ```export LIBRARY_PATH="$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/lib"``` – Victor Eijkhout Nov 14 '21 at 22:28
  • As in, you don't have `MacOSX.sdk` at all? Or the up to date headers are in `MacOSX11.1.sdk`? `MacOSX.sdk` should in reality be a symlink to the latest version, on my (also still big sur) system this is `MacOSX.sdk -> MacOSX11.3.sdk`. – Jacob Faib Nov 14 '21 at 22:36
  • I'm not @VictorEijkhout, but I can confirm that on my system there is also no symlink to an unversioned `MacOSX.sdk`, just the versioned directory. – Mark Reed May 04 '23 at 18:46
8

I added this to my ~/.profile:

export SDKROOT=$(xcrun --sdk macosx --show-sdk-path)
export LIBRARY_PATH="$LIBRARY_PATH:$SDKROOT/usr/lib"

And it works.

Jongwook Choi
  • 8,171
  • 3
  • 25
  • 22
yegor256
  • 102,010
  • 123
  • 446
  • 597
3

I'll also chime in (19 Dec 2020) that I just updated from MacOS Mojave to Catalina and suddenly got this error.

It resolved itself with

brew reinstall gcc
brew reinstall gfortran
Brian
  • 43
  • 1
2

Update: I tried what was suggested and installed the gfortran build from the gcc package using homebrew which gave me a newer version of gfortran and my problem was fixed!

datasplice
  • 494
  • 1
  • 3
  • 16
1

I had the same problem, but installing gfortran build from the gcc package using homebrew did not help. Instead, I installed the whole gcc using homebrew:

brew install gcc

and that solved the problem.

oxydentist
  • 80
  • 1
  • 6
1

For those using MacPorts.

# Installed `gcc8` and `gcc_select`
sudo port install gcc_select gcc8

# then to create "gfortran" softlink to "gfortran-mp-8"
sudo port select --set gcc gcc8

# and added to my .profile
export LIBRARY_PATH="$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"

I did ls -l /Library/Developer/CommandLineTools/SDKs/ and found that Xcode had installed one SDK for Catalina and one for Big Sur. I changed the soft link for MacOSX.sdk to point to the Catalina version.

saherr1969
  • 11
  • 1
1

Just placed this line at the end of .zshrc:

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

and ran my Fortran code again to compile – success! The issue with -lSystem has been solved in my macOS system.

Wadek
  • 11
  • 2
0

I had this problem, and I'd tried lots of thing to solve the problem, but only using these "both" commands solved it:

brew reinstall gcc
brew reinstall gfortran
-1

I recently had this

ld: library not found for -lSystem

problem after upgrading Mac OS from v10 to v11 (Big Sur). I am using Xcode 12.3 and I used brew install gcc to install gfortran 10.2.0. I still saw the error after trying brew reinstall gcc. I was able to solve the problem using:

brew install gfortran

but I do not know why this works.

Mead
  • 383
  • 4
  • 19
  • 1
    I encountered the same problem. However, when I run `brew reinstall gfortran`, it simply reinstalls `gcc` 10.2.0 so the issue (`ld: library not found for -lSystem`) remains. Any suggestions? – Tomas Dec 22 '20 at 14:38