gfortran flags and warnings
A compiler flag is a question asked of the compiler, and the two that matter most in Fortran are -Wall and -Wextra: between them they catch a missing variable, a silent conversion and an equality test on reals. This lesson builds one small program three ways, then looks at the flags that change the language dialect and at the three that decide where the output files go.
-Wall and -Wextra
The program below has three defects that a compiler can see and a person often does not: a variable declared and never used, an assignment that converts a double to an integer, and an equality test on a real. It is otherwise a correct program, which is the point: the warnings are about code that compiles and runs.
The file flags.f90:
program flags
implicit none
integer :: i, unused
real :: x
real(8) :: d
i = 5
x = i * 1.5
d = x
i = x + d
print *, i
print *, x == 7.5
end program flags
gfortran -Wall -Wextra -std=f2018 flags.f90 -o flags
flags.f90:10:6:
10 | i = x + d
| 1
Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
flags.f90:12:10:
12 | print *, x == 7.5
| 1
Warning: Equality comparison for REAL(4) at (1) [-Wcompare-reals]
flags.f90:3:17:
3 | integer :: i, unused
| 1~~~~~
Warning: Unused variable 'unused' declared at (1) [-Wunused-variable]
Three warnings, three different kinds of defect, and the build succeeded: a warning is not a verdict, it is a question. The caret under 1~~~~~ in the last one covers the whole name, which tells a reader which of two variables on the line is meant.
-Werror
A project that has decided these three are bugs makes the build fail on them, and the messages change from Warning to Error with the flag in brackets:
gfortran -Wall -Wextra -Werror -std=f2018 flags.f90 -o flags_werror
flags.f90:10:6:
10 | i = x + d
| 1
Error: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Werror=conversion]
flags.f90:12:10:
12 | print *, x == 7.5
| 1
Error: Equality comparison for REAL(4) at (1) [-Werror=compare-reals]
f951: all warnings being treated as errors
The last line is the one to read: f951 is the Fortran front end, and the sentence says that the compile failed because of warnings rather than because of an error of its own. A single warning can be allowed with -Wno-conversion instead of dropping -Werror for the whole project.
-std=f2018 and the extensions
The dialect flags decide which language the compiler accepts. The default is GNU Fortran with its extensions; -std=f2018 is the standard and nothing else. A declaration that was an extension in every gfortran before it became common is refused outright:
program ext
implicit none
integer*4 :: i
i = 5
print *, i
end program ext
gfortran -Wall -Wextra -std=f2018 -c ext.f90
ext.f90:3:12:
3 | integer*4 :: i
| 1
Error: GNU Extension: Nonstandard type declaration INTEGER*4 at (1)
Without -std=f2018 the same line compiles without a word, which is why the course writes the flag in every build line: a page that shows a program compile here is a page whose program is standard Fortran. The other useful value is -std=legacy, which accepts the old dialect on purpose and is what the fixed-form lessons use.
-c, -o and -J
Three flags decide what the compiler writes. -c compiles without linking, so the output is an object file. -o names that file or the executable. -J names the directory for the .mod files, which otherwise land next to the source and clutter it.
gfortran -Wall -Wextra -std=f2018 -c m.f90 -J build -o build/m.o
ls build/
m.mod
m.o
A build directory then holds everything the compiler generated and the source tree holds the sources, which is what the makefile of lesson 112 does. The module file must be found by every user of the module, so a project passes the same -J to every compile; it is also the flag that makes a parallel build of a module and its user fail, which is the next lesson.
The flags in a table
| flag | what it does | what it caught here |
|---|---|---|
| -Wall | the common warnings | the unused variable |
| -Wextra | the ones -Wall leaves out | with -Wall, the conversion and the equality test |
| -Wconversion | a conversion that may lose value | double to integer |
| -Wcompare-reals | equality between reals | x == 7.5 |
| -Werror | every warning is an error | the two above |
| -std=f2018 | the standard dialect | INTEGER*4 |
| -std=legacy | the old dialect on purpose | nothing: it accepts more |
| -c | compile, do not link | build/m.o |
| -o | name the output | build/m.o, flags, flags_werror |
| -J | where the .mod files go | build/m.mod |
| -g | debug information | lesson 113 reads it in gdb |
| -fcheck=all | runtime checks | lesson 114 catches an out-of-bounds read |
| -O0 / -O2 / -O3 | optimisation level | lesson 121 measures them |
| -ffpe-trap=... | stop on a floating-point exception | lesson 115 |
The table is a route through the rest of this part rather than a list to memorise. A project needs the first five rows in every build, the dialect flag to keep the source honest, and the output flags as soon as it has more than one file; the last four are for the days when something is wrong or slow.
Exercises
- Add -Wno-unused-variable to the -Werror build. Which warnings are still errors, and which line of the output disappears?
- Compile flags.f90 with -std=legacy. Are the three warnings still there?
- Write the build commands that put every object and every module file of a two-module project into build/. How many -J flags do you need?
- Try -std=f2008 on ext.f90. Is INTEGER*4 accepted there?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What do -Wall and -Wextra add?
Answer
A set of warnings about code that compiles: unused names, conversions, real equality
They are questions about code that builds.
-
What does -Werror do?
Answer
Turns every warning into a build error
The compile stops with "all warnings being treated as errors".
-
What does
-std=f2018do withINTEGER*4?Answer
Rejects it as a GNU extension
The dialect flag accepts the standard and nothing else.
-
What does
-cproduce?Answer
An object file, without linking
The link is a separate step.
-
What does
-J builddo?Answer
Writes the .mod files into build/
Every user of the module needs the same flag.
-
What is the default dialect of gfortran?
Answer
GNU Fortran with extensions
The extensions compile unless a -std flag says no.
-
Which flag keeps a single warning from failing a -Werror build?
Answer
-Wno-<name> for that warning
The narrow flag is better than dropping -Werror.
-
What does this command print?
gfortran -Wall -Wextra -std=f2018 -c m.f90 -J build -o build/m.o ls build/Answer
m.mod and m.o
-J moves the module file and -o names the object.
-
Which flag adds debug information?
Answer
-g
It is what gdb reads in lesson 113.
-
Why write -std=f2018 in every build line of a course?
Answer
So every program shown is standard Fortran and not an extension
A page that compiles is a page whose source is standard.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev