# Unable to compile Fortran code using gfortran on Mac OS

**URL:** <https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420>\
**Category:** Help\
**Created:** [December 20, 2021, 9:14am UTC](https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420 "2021-12-20T09:14:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcosta](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pcosta/32/5504_2.png) [@pcosta](https://fortran-lang.discourse.group/u/pcosta)\
**Post date:** [December 20, 2021, 9:14am UTC](https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420/1 "2021-12-20T09:14:24Z")

</div>

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:

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

```

trying this SO [solution](https://stackoverflow.com/a/65428700/5663206):

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

```

I get a different error:

```auto
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`.

---

<div class="post-metadata">

**Author:** ![mhulsen](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@mhulsen](https://fortran-lang.discourse.group/u/mhulsen)\
**Post date:** [December 20, 2021, 11:15am UTC](https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420/2 "2021-12-20T11:15:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wiremoons](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/wiremoons/32/764_2.png) [@wiremoons](https://fortran-lang.discourse.group/u/wiremoons)\
**Post date:** [December 20, 2021, 12:33pm UTC](https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420/3 "2021-12-20T12:33:34Z")

</div>

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 🙂

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:

```auto
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…

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [December 20, 2021, 7:51pm UTC](https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420/4 "2021-12-20T19:51:23Z")

</div>

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

```auto
conda create -n gf gfortran
conda activate gf

```

and everything seems to just work for me.

---

<div class="post-metadata">

**Author:** ![pcosta](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pcosta/32/5504_2.png) [@pcosta](https://fortran-lang.discourse.group/u/pcosta)\
**Post date:** [December 20, 2021, 11:00pm UTC](https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420/5 "2021-12-20T23:00:42Z")

</div>

Thank you very much, that solved my problem! 😃

---

<div class="post-metadata">

**Author:** ![HTercas](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/htercas/32/2698_2.png) [@HTercas](https://fortran-lang.discourse.group/u/HTercas)\
**Post date:** [March 17, 2023, 10:25pm UTC](https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420/6 "2023-03-17T22:25:52Z")

</div>

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](https://gcc.gnu.org/wiki/GFortranBinariesMacOS)

I keep facing the same issues, though 😪

---

<div class="post-metadata">

**Author:** ![Knarfnarf](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Knarfnarf](https://fortran-lang.discourse.group/u/Knarfnarf)\
**Post date:** [March 21, 2023, 7:58pm UTC](https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420/7 "2023-03-21T19:58:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [March 21, 2023, 8:34pm UTC](https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420/8 "2023-03-21T20:34:33Z")

</div>

> [@Knarfnarf](#):
>
> … only if I had good projects to work on!

@Knarfnarf ,

Did you take a look at this [**link**](https://fortran-lang.discourse.group/t/fastgpt-faster-than-pytorch-in-300-lines-of-fortran/5385)? Perhaps you can collaborate with @certik on coarray parallel solutions to that effort, or with more detailed variations than fastGPT?

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [March 21, 2023, 9:25pm UTC](https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420/9 "2023-03-21T21:25:18Z")

</div>

> [@Knarfnarf](#):
>
> Install all updates  
> install Xcode  
> install command line tools

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.

---

<div class="post-metadata">

**Author:** ![Knarfnarf](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Knarfnarf](https://fortran-lang.discourse.group/u/Knarfnarf)\
**Post date:** [March 21, 2023, 11:21pm UTC](https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420/10 "2023-03-21T23:21:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Johnuk](https://avatars.discourse-cdn.com/v4/letter/j/f19dbf/32.png) [@Johnuk](https://fortran-lang.discourse.group/u/Johnuk)\
**Post date:** [February 26, 2024, 6:46pm UTC](https://fortran-lang.discourse.group/t/unable-to-compile-fortran-code-using-gfortran-on-mac-os/2420/11 "2024-02-26T18:46:01Z")

</div>

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)
