Nice compiler options

There are lots of compiler options in various compilers, and also newer options are constantly added to more recent versions. So I would appreciate it if you share the information about options that you find useful in your actual coding :slight_smile:

In my case, I found -finit-derived in gfortran useful, which initializes components of derived types with some specified values (e.g., NaN for reals). For example, for this code

program main
    implicit none
    type Foo
        integer :: n
        real :: x
        complex :: z
        character(5) :: s
    type(Foo) :: f

    print *, f

“gfortran-10 test.f90” (with no option) gives

$ ./a.out
  1412855200   4.59163468E-41  (-3.031044989E-24,4.591634678E-41) ^@^@^@^@^@
$ ./a.out
  1563989408   4.59163468E-41  (-3.031044989E-24,4.591634678E-41) ^@^@^@^@^@
$ ./a.out
  1400161696   4.59163468E-41  (-3.031044989E-24,4.591634678E-41) ^@^@^@^@^@

while “gfortran-10 -finit-derived -finit-integer=-1 -finit-real=snan test.f90” gives

-1   NaN  (NaN,NaN)  ^@^@^@^@^@

which I think is useful to detect type components not assigned during execution.


For GFortran here is what I use for Debug mode:

-Wall -Wextra -Wimplicit-interface -Wno-unused-function -fPIC -g -fcheck=all -fbacktrace -ffpe-trap=invalid,zero,overflow -finit-real=snan -finit-integer=-9999999

The -finit-derived is a good one and should be used also, I didn’t know about it. The -ffpe-trap are important to stop a program when a NaN is generated, and the -fbacktrace is important to give you a nice stacktrace.


Gfortran is my main compiler and is excellent, but g95 is my sentimental favorite and has good compile-time checks. It does not support much of Fortran 2003 and beyond.

The result of compiling the code above with the options I use, g95 -c -Wall -Wextra -Wimplicit-none -Werror=100,113,115,137,146,147,159,163 -ftrace=full -fbounds-check -freal=nan -fmodule-private -Wno=112,167 is

In file xxmain.f90:9

type(Foo) :: f

Error (113): Variable ‘f’ at (1) is used but not set