Welcome to AE Resources
Converted document Converted document

COMPILING AND RUNNING

Getting answers from a Fortran code is a two-step process. First, the source code must compile be compiled to generate a version in machine language. This step is where most of the programming errors will be found (but not all of them). Once a compiled version of the code, known as an executable, exists, then it can be run to obtain the code results. Typically the code is compiled once, and then the executable is run multiple times to generate results from the compiled code. For example, in many commercial codes, only an executable version of the code will be provided.
Compilers can be found for free or for purchase on the internet. The website http://gcc.gnu.org/fortran/ has Fortran 95/2003/2008, as well as Fortran 77 and Fortran 90 compilers for free.
HOW TO COMPILE A FORTRAN PROGRAM
The command and options depend on the Fortran compiler that is used. The basic compile command also varies depending on the version of the compiler, as seen in Table 1↓.
Table 1 Sample compilers and commands
Compiler Name Compile Command
GNU Fortran 77 g77, f77
GNU Fortran 90 g90, gfortran
intel Fortran 90 ifort
NAG Fortran 90/95 nagfor
PGI Fortran 77 pgf77
PGI Fortran 90/95 pgf95, pgfortran
PGI Fortran pgfortran
The compile command has the form:
<compile command> [options] <yoursourcecodename>

Consider using GNU Fortran 90 to compile your source code that you named test.F:
gfortran test.F

will generate an executable code named a.out. a.out is the default executable name. The compiler option, -o  < filename >  can be invoked to rename the executable. Some useful, generic compiler options are included in Table 2↓, but one should look at the documentation for the compiler that is used.
Table 2 Basic compilers options
Name Description
-byteswapio, Swap between big and little Endian I/O format
-convert conversion
-c Compile to an interim output file, but do not link
-cpp, -ftpp Run C or Fortran preprocessor
-ffree-form Source code layout without Fortran 77 rules
-ffixed-form Source code layout similar to Fortran 77 rules
-Wall Enable most common compiler warnings
-fbounds-check, Run-time array subscript checking
-ffortran-bounds-check
-ffpe-trap=list Trap (locate) floating point exceptions
-i4, -i8 4 or 8 byte integers
-m32, -m64 compile with 32 or 64 bits, respectively
-o  < filename >  save executable as  < filename > 
-ON Optimization level (N=0 (default),1,2,3)
-r4, -r8 4 or 8 byte reals
-fsyntax-only Checks Fortran syntax only
Compilers typically can handle various versions of Fortran source files, and can be linked with routines written in C (for example for faster file input/output, known as I/O). The Fortran type is denoted via the file suffix. A few of the more well-known file formats are provided in Table 3↓.
Table 3 Basic file formats and suffixes
Suffix Description
.c C formats that must be preprocessed
but can be compiled with the Fortran routines
.f, .for, .ftn Fixed-form formats that are not preprocessed
.F, .FOR, .fpp, .FPP, .FTN Fixed-form formats that must be preprocessed
.f90, .f95, .f03, .f08 Free-form formats that are not preprocessed
.F90, .F95, .F03, .F08 Free-form formats that are not preprocessed
Next Page →
Next Page →