Q: How to find gfortran version number in a portable way with autoconf? I am trying to use autoconf to check the version of gfortran used to compile my code. How to get the version number safely in a portable way?
The easiest way would be to simply use gfortran -dumpversion, but I read that there is some problems with this approach (on my machine for example, it does not return the patchlevel number). The option -dumpfullversion I found in the documentation does not seem to be implemented (I get a fatal error).
Instead, I am trying to use gfortran --version or gfortran -v. My first attempt is the following:
GFORTRAN_VERSION=`gfortran -v 2>&1 | $AWK 'END { print }' | $AWK '{ print $3 }'`

where $AWK contains the awk-type program found by AC_PROG_AWK. It works perfectly on all the linux machines I have tested. However, it does not look reasonable to assume that gfortran -v will always return the version number in the third position of the last line of its output. For instance, will this approach work on MacOS or Windows?
Any advice would be very welcome, thank you very much in advance!

A: You can write a short program that will use the preprocessor definitions as follows
program p
  implicit none

  write(*,*) __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__
end program p

see What are the gcc predefined macros for the compiler's version number?
You might customize the formatting if you like.

A: I don't understand why you suspect that your current implementation won't work for gfortran on other platforms.  This page even refers the reader to the GCC documentation for "non-Fortran-specific" options (e.g. -v).  With documentation like that, I'd expect it to be extremely similar to gcc.
You should be able to set your autoconf tests (e.g. AC_LANG or AC_LANG_PUSH) and use regular autoconf macros (e.g. AC_COMPILE_IFELSE).  But running a test program for output has a downside -- it breaks cross-compilation in the general case.
You may find it instructive to look at what they are doing in this macro which is a C/C++ implementation (for many compilers) of what you are trying to do in Fortran (for just gfortran).

A: Recent versions of gfortran support the Fortran 2008 standard library routine COMPILER_VERSION().  Before calling this routine your program must have a USE ISO_FORTRAN_ENV statement.
