# Nice compiler options

**URL:** <https://fortran-lang.discourse.group/t/nice-compiler-options/366>\
**Category:** Uncategorized\
**Created:** [October 24, 2020, 7:47pm UTC](https://fortran-lang.discourse.group/t/nice-compiler-options/366 "2020-10-24T19:47:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [October 24, 2020, 7:47pm UTC](https://fortran-lang.discourse.group/t/nice-compiler-options/366/1 "2020-10-24T19:47:11Z")

</div>

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 🙂

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

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

    print *, f
end

```

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

```auto
$ ./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

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

```

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

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [October 25, 2020, 2:03am UTC](https://fortran-lang.discourse.group/t/nice-compiler-options/366/2 "2020-10-25T02:03:10Z")

</div>

For GFortran here is what I use for Debug mode:

```nohighlight
-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.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [November 13, 2020, 11:14pm UTC](https://fortran-lang.discourse.group/t/nice-compiler-options/366/3 "2020-11-13T23:14:48Z")

</div>

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
             1

```

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

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [February 22, 2021, 12:15pm UTC](https://fortran-lang.discourse.group/t/nice-compiler-options/366/4 "2021-02-22T12:15:41Z")

</div>

I have started a new subproject in my Flibs project at [http://flibs.sourceforge.net](http://flibs.sourceforge.net) to examine the capabilities of compilers (and perhaps more dedicated tools) to identify via static analysis problems in the source code. It is something I have been thinking about for some time and with a discussion at work about static analysis of source code in mind I simply sat down for it. Far from complete, but at least there are a few test programs now :).

Main issue: get some framework up and running (like for chkfeatures) but one that reports a nice summary.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [February 22, 2021, 12:54pm UTC](https://fortran-lang.discourse.group/t/nice-compiler-options/366/5 "2021-02-22T12:54:17Z")

</div>

> [@Arjen](#):
>
> I have started a new subproject in my Flibs project at [http://flibs.sourceforge.net](http://flibs.sourceforge.net) to examine the capabilities of compilers (and perhaps more dedicated tools) to identify via static analysis problems in the source code.

Thanks for doing this. The compilers I see [discussed](http://flibs.sourceforge.net/chksys.html) are MicroSoft FORTRAN version 5.1 and Lahey. Presumably more modern compilers will be added. You comment favorably on Lahey, and I can confirm that Lahey/Fujitsu Fortran 95 was very good at flagging dubious code. There is an online [Fortran Source Check.](https://www.lahey.com/check.htm)

Could you mirror your work at GitHub, which I think is the most popular code repository? I do see an flibs [fork](https://github.com/philip-peterson/flibs).

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [February 22, 2021, 12:59pm UTC](https://fortran-lang.discourse.group/t/nice-compiler-options/366/6 "2021-02-22T12:59:18Z")

</div>

Up to now I have been too lazy (or awed?) to actually do that, but I am gaining more experience with Github and, frankly, SourceForge is evermore uncomfortable to work with.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [February 22, 2021, 1:10pm UTC](https://fortran-lang.discourse.group/t/nice-compiler-options/366/7 "2021-02-22T13:10:25Z")

</div>

By the way, suggestions for checking are welcome 🙂
