How can gfortran tell if I am compiling f90 or f95

2020-02-03 05:38发布

问题:

I understand gfortran can compile f90 or f95? How does it know which one it is compiling? Also can it compile f77 code? Does ubuntu already have a fortran compiler or do I need to download gfortran?

回答1:

gfortran can guess certain things from the file extension; if the file has an extension of .f, .f90, f95, .f03, or .f08 it will assume fixed (.f) or free format with the appropriate standards. But you can force it to compile (say) fortran2003 code with the option -std=f2003. Eg, from the documentation,

-std=std

Specify the standard to which the program is expected to conform, which may be one of f95, f2003, f2008, f2018, gnu, or legacy. The default value for std is gnu, which specifies a superset of the latest Fortran standard that includes all of the extensions supported by GNU Fortran, although warnings will be given for obsolete extensions not recommended for use in new code. The legacy value is equivalent but without the warnings for obsolete extensions, and may be useful for old non-standard programs. The f95, f2003, f2008, and f2018 values specify strict conformance to the Fortran 95, Fortran 2003, Fortran 2008 and Fortran 2018 standards, respectively; errors are given for all extensions beyond the relevant language standard, and warnings are given for the Fortran 77 features that are permitted but obsolescent in later standards. -std=f2008ts allows the Fortran 2008 standard including the additions of the Technical Specification (TS) 29113 on Further Interoperability of Fortran with C and TS 18508 on Additional Parallel Features in Fortran.

Note that, with a few exceptions, the fortran standards are highly backwards-compatable, so that much "nice", standard conforming, fortran-77 code is also valid fortran 2003 code. The main problem with that is that much fortran code from the 80s and earlier is neither "nice" nor standard-conforming.



回答2:

Yes, gfortran can compile FORTRAN 77 and Fortran 90,95 etc. It has many features of 2003 & 2008 implemented. For Fortran 90/95/2003/2008 code a good extension to use is .f90. Some other compilers don't recognize .f95, .f2003, etc. And do you really want to rename your files when a new standard comes out? Use .f or .for for FORTRAN 77. The main difference is the source layout that the compiler will expect: free form for .f90, fixed form for .f or .for. (Upper-case filetypes will cause the preprocessor to be run.) You can force fixed-form layout with the options -ffixed-form -ffixed-line-length-none.