There are many options that can be specified to the compiler. For debugging, the most useful are:
- -O0 to turn off all optimisation which allows more precise diagnosis of run-time errors.
- -fbounds-check (gfortran only) to enable generation of run-time checks for array subscripts and against the declared minimum and maximum values. It also checks array indices for assumed and deferred shape arrays against the actual allocated bounds.
- -ffpe-trap= (gfortran only) which specifies that floating-point invalid, divide-by-zero, overflow, underflow and precision exceptions are enabled. If any such exceptions occur, execution is aborted with a core file which may be useful for debugging. This argument may be optionally followed by a comma-separated list of the exceptions to be trapped
More details of these, and other debugging options, are available in the man page for the compiler. It is important to understand that any compiler system only defines the correct operation of a correct program; errors such as out-of-bounds array references and uninitialised variable may be treated differently by different systems. All codes should be compiled and run with these options as part of the development cycle; in particular, codes that are being moved to this system from elsewhere and show different behaviour must be checked using these options.
To compile the program myprog.f90 with all of these options use the command
gfortran myprog.f90 -fbounds-check -ffpe-trap=invalid,zero,overflow,underflow,precision -O0