Fortran codes won't compile on Mac with gfortr

2019-03-04 13:34发布

问题:

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.

回答1:

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.



回答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

    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/



回答3:

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.