Fortran codes won't compile on Mac with gfortran
Asked Answered
I

3

1

I recently moved from the SGI, Sun workstation environment to a Mac. SGI and Sun came with Fortran compilers so I have maybe 100 small f77 codes I wrote over the years for post-processing and analysis of simulated data. I was hoping to get these codes running on my iMac with gfortran. Most of these are very simple codes but I can't get them to compile and execute. I tried starting with the basics and wrote the Hello World code from a gfortran help page. My code, fortran.f is:

program helloworld
print *, "hello world"
end program helloworld

When I tried compiling this according to the example I typed:

gfortran fortran.f

But I keep getting the error message:

FATAL:/opt/local/bin/../libexec/as/x86_64/as: I don't understand 'm' flag!

This is the same error message I get on my other codes. Can someone tell me what I'm doing wrong? I can't think of a simpler example but I can't seem to get it to work.

Imaginal answered 9/1, 2018 at 18:39 Comment(10)
How did you install gfortran exactly?Cammie
I honestly don't recall. I think MacPorts or Homebrew. I loaded gfortran years ago but haven't needed it until now.Imaginal
Personally, I would be inclined to uninstall it, update Xcode and CommandLine tools and then reinstall. All the underlying linkages and stuff change with each upgrade to macOS. Run which gfortran to see which one you have - homebrew will be in /usr/local/bin and MacPorts probably in /opt.Cammie
my which fortran returns /usr/local/bin/gfortran so I assume from your statement I usedHomebrew. I will attempt to uninstall it and re-install with Homebrew. Thanks for your advice.Imaginal
Upgrade Xcode by running it and checking for updates in the menus and/or by running AppStore and clicking Updates. Commandline tools are installed with xcode-select --install. Homebrew packages gfortran with GCC, so you do brew install gcc to get it.Cammie
I'm running the latest High Sierra and Xcode with command line tools. Do you still think I need to reload Xcode?Imaginal
Probably no need to reload the whole Xcode, just make sure it and CommandLine tools are up-to-date, before reinstalling GCC.Cammie
Thanks. if this works i'll let you know and you can submit it as an answer and i'll select it.Imaginal
@MarkSetchell Your comment to reinstall gfortran worked. If you want to pose that as an answer I'll be happy to accept it. Thanks for your help on both my questions.Imaginal
Excellent! I'm glad you are up and running. It's late here in the UK, so I'll write it up tomorrow. Good luck with your project.Cammie
C
2

If your gfortran was installed a long time ago and you have updated macOS since installing, it may need re-installing to get correctly aligned and linked with the latest macOS tools and libraries.

My advice would be to:

  • uninstall gfortran,
  • check that Xcode and its command line tools are up-to-date,
  • re-install gfortran.

Hints for each of those steps follow:

Note that gfortran is a part of GCC - the "GNU Compiler Collection".

If you installed gfortran via homebrew, you can remove it with:

brew rm gcc

You can update Xcode by by going to AppStore and clicking Updates at top-right.

The Xcode Command Line tools include make and git and command-line versions of the compilers. You can install/update the Xcode command line tools with:

xcode-select --install

You can install gfortran with homebrew using:

brew install gcc

When you are finished, you should make sure that your PATH includes /usr/local/bin near the start and that there are no errors when you run:

brew doctor

which is a brilliant utility that checks your homebrew configuration is correct.

Cammie answered 10/1, 2018 at 9:34 Comment(0)
S
2

When it comes to macOS, I think that building form sources is the best approach you can have. You can achieve that quite easily by downloading and compiling GFortran as part of GCC directly from: https://gcc.gnu.org/wiki/GFortran

However, there are few things you have to take care of:

  • make sure you have XCode installed, you can get it here

    XCode

    XCode is free of charge

  • Make sure you have command line tools

    You can get them either from developer.apple.com

    Command Line Tools

    or directly from XCode. It might be that XCode will tell you to install Command Line Tools upon first execution

    enter image description here

    In the past, running command like "svn", when Command Line Tools were not installed, also triggered the installation.

  • Compile GCC

    > ./configure --prefix=$HOME/opt/usr/local
    > make all
    > make install
    
  • Alternatively, you can install using macOS package from GFortran

    gfortran-6.3-Sierra.dmg

Fully working sample with Fortran based MPI code:

http://www.owsiak.org/running-open-mpi-on-macos/

Scarletscarlett answered 10/1, 2018 at 9:0 Comment(7)
You mean, build GCC from source?Krein
No, just GFortranScarletscarlett
Fair enough (I was originally going to write gfortran but thought I'd ask wider). Perhaps it'd be useful to clarify the gfortran build in the answer? I originally couldn't tell you meant that, what with the reference to OpenMPI.Krein
How does one even compile gfortran without GCC? I don't think it is possible. How would you do that? Normally you do --enable-languages=c,c++,fortran and you cannot include Fortran without C.Discuss
You are right. I have mixed up things in the answer. Package for macOS (which I use from time to time) and GCC sources (which I also use in some cases).Scarletscarlett
@mko Thanks for your answer. I discovered the Gfortran binaries last night while trying unsuccessfully to install gcc via Homebrew. It solved my problems. Very nice.Imaginal
.oOo. Cool. Have fun with Fortran! .oOo.Scarletscarlett
C
2

If your gfortran was installed a long time ago and you have updated macOS since installing, it may need re-installing to get correctly aligned and linked with the latest macOS tools and libraries.

My advice would be to:

  • uninstall gfortran,
  • check that Xcode and its command line tools are up-to-date,
  • re-install gfortran.

Hints for each of those steps follow:

Note that gfortran is a part of GCC - the "GNU Compiler Collection".

If you installed gfortran via homebrew, you can remove it with:

brew rm gcc

You can update Xcode by by going to AppStore and clicking Updates at top-right.

The Xcode Command Line tools include make and git and command-line versions of the compilers. You can install/update the Xcode command line tools with:

xcode-select --install

You can install gfortran with homebrew using:

brew install gcc

When you are finished, you should make sure that your PATH includes /usr/local/bin near the start and that there are no errors when you run:

brew doctor

which is a brilliant utility that checks your homebrew configuration is correct.

Cammie answered 10/1, 2018 at 9:34 Comment(0)
M
0

All I had to do was change the path.

Initially, my PATH was something like

/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin

Because of this reason, the default assembler (as) was not called which is in the /usr/bin directory.

To enable the call to the right assembler (as), I had to add /usr/bin to the PATH in front of (before) /opt/local/bin, i.e. on a Mac this can be added by editing ~/.bash_profile such that one's $PATH looks like

/usr/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin

Once edited, execute at your command prompt:

source /etc/bash_profile

This worked for me.

Mayne answered 19/4, 2018 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.