Warnings, errors and gfortran diagnostics
A compiler does three things with a program: it repairs what it can, it reports what it cannot repair, and it sometimes points at a line that is legal and still almost certainly a mistake. The three jobs produce three kinds of message, and a reader who can tell them apart reads a build log in seconds instead of minutes. This lesson covers the messages a beginner meets most often, with the real text gfortran prints.
Warnings, errors and notes
- An error stops the build. No binary is written, and the previous binary, if there is one, stays where it was.
- A warning does not. The program is built and runs, and the message says that something in it is suspicious: a variable nobody reads, a value read before it is written, a comparison that is always true.
- A note is attached to a warning and repeats the declaration the warning is about. It is there because the line that triggers a warning is rarely the line that caused it.
Every message is printed as a block: a line with the position file:line:column, the source line with a caret under the position, and then the message with its category. The category is the first word: Warning:, Error:, Fatal Error: or note:. A tool that reads build logs greps for those words, and so can a reader.
-Wall -Wextra decide which of these messages appear. Without them a build is quiet, and a quiet build is not a correct build: gfortran 15 reports an unused variable only when it is asked.
An unused declaration
Two integers are declared, one is used. The build succeeds and the binary runs; the warning says the second declaration has no reader.
The file unused.f90:
program warn_unused
implicit none
integer :: used, spare
used = 1
print *, used
end program warn_unused
gfortran -Wall -Wextra -std=f2018 unused.f90 -o unused
unused.f90:3:20:
3 | integer :: used, spare
| 1~~~~
Warning: Unused variable 'spare' declared at (1) [-Wunused-variable]
1~~~~ marks the extent of the name: the caret is the start, the tildes are the rest of it. The bracketed [-Wunused-variable] is the name of the check, and that name is the one to search for when a warning is silenced or explained.
A warning can be silenced with -Wno-unused-variable, and silencing it is almost always the wrong fix: either the declaration is dead and should go, or the declaration was meant to be used and the program has a bug.
A name used before it is set
This warning is more serious. An uninitialised variable holds whatever the memory held, and a program that reads it has an answer that depends on the run, the machine and the compiler flags.
The file uninit.f90:
program uninit
implicit none
integer :: total
print *, total + 1
end program uninit
gfortran -Wall -Wextra -std=f2018 -c uninit.f90
uninit.f90:5:20:
5 | print *, total + 1
| ^
Warning: 'total' is used uninitialized [-Wuninitialized]
uninit.f90:3:14:
3 | integer :: total
| ^~~~~
note: 'total' was declared here
The note is the useful half: the warning points at the read and the note at the declaration, and the two are three lines apart in a program of six. The build continues, and the binary would print one more than whatever the stack contained; this page does not show that number, because it is not a property of the program. The fix is to assign the variable before the read, and the compiler then stops warning.
The check that catches this at run time is -finit-local-zero, which zeroes the local variables, and the sanitizer family in a later lesson. Neither is a substitute for the assignment: a zero is as wrong as garbage when the program meant to compute the value.
A value of the wrong type
An error, not a warning: a character constant cannot become an integer. The message names both types and the length of the character value, which is the number of bytes the compiler counted.
The file mismatch.f90:
program mismatch
implicit none
integer :: count
count = "three"
print *, count
end program mismatch
gfortran -Wall -Wextra -std=f2018 mismatch.f90 -o mismatch
mismatch.f90:5:10:
5 | count = "three"
| 1
Error: Cannot convert CHARACTER(5) to INTEGER(4) at (1)
CHARACTER(5) is the type and the length of "three"; INTEGER(4) is the type and the number of bytes of the integer. Nothing about the assignment is ambiguous, and no flag turns this error off.
A shape that does not fit
Arrays are values in Fortran, and an assignment between arrays copies element by element. The shapes must agree, and the compiler checks them when both are known.
The file ranks.f90:
program ranks
implicit none
integer :: a(3), b(2)
a = [1, 2, 3]
b = a
print *, b
end program ranks
gfortran -Wall -Wextra -std=f2018 ranks.f90 -o ranks
ranks.f90:6:3:
6 | b = a
| 1~
Error: Different shape for array assignment at (1) on dimension 1 (2 and 3)
The message counts from the left: dimension 1 is the only dimension here, and the two shapes are 2 and 3. With a rank-2 array the message says which dimension disagreed, which is the part a reader needs when a matrix is transposed by mistake.
Reading the first error line
A long build log has one important line: the first error. Every later error can be a consequence of the first, because the compiler carries on with a broken symbol table and reports the damage it sees. The habit that saves time is to read the first Error: line and the source line above it, fix that, and build again.
Three things in a message are worth extracting in that read:
- The position file:line:column. It is where the compiler noticed the problem, which is not always where the problem is: a missing declaration is noticed at the first use.
- The category word. Fatal Error means the compiler could not continue at all, as when a module file is missing.
- The bracketed flag name, when there is one. It names the check, and the check can be looked up in the gfortran manual.
A build that produces warnings and no errors is a build that runs. That is exactly why -Wall -Wextra belong in every command: the compiler will not volunteer them, and a warning that appears once and is never read again is a bug in the making.
Exercises
- Build the unused program without -Wall. Does the warning appear?
- Delete spare instead of using it. What does the warning say then, and why is it a better fix?
- Assign total = 0 in the uninit program and rebuild. Which message disappears?
- Change b to b(3) in the ranks program. Does it build, and what does it print?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What happens to the binary when the build stops on an error?
Answer
No new binary is written, and the previous one stays where it was
The link is the last step, so a failed compile leaves the old binary in place.
-
Which switch turns on the unused variable warning?
Answer
-Wall
-Wall enables -Wunused-variable among others; without it the build is silent.
-
What does the note after an uninitialised value warning repeat?
Answer
The declaration of the variable the warning is about
The note points at the declaration, which is usually away from the line that reads the value.
-
What does the compiler report for
count = "three"withcountan integer?Answer
Error: Cannot convert CHARACTER(5) to INTEGER(4) at (1)
A character constant cannot become an integer, and the message names both types and the length.
-
What does
[-Wunused-variable]in a warning mean?Answer
The name of the check that produced the warning
The bracketed name is the warning option, and it can be searched or turned off by name.
-
What does this program print?
program p implicit none integer :: a(2), b(2) a = [1, 2] b = a print *, b(2) end program pAnswer
2
The two arrays have the same shape, so the assignment copies and b(2) is 2.
-
Why read only the first error line of a long log?
Answer
Later errors can be consequences of the first one
A broken declaration makes every later use of the name an error too.
-
Which message stops the build?
Answer
Error: Different shape for array assignment
Only the Error category stops code generation; warnings and notes do not.
-
What does
-Wno-unused-variabledo?Answer
It turns that one warning off
The -Wno- prefix disables the named check; the code stays as it is.
-
What does gfortran check when it assigns one whole array to another?
Answer
That the shapes agree on every dimension
An array assignment copies element by element, so the shapes must match.
The programs and the diagnostics on this page were produced with GNU Fortran (Alpine 15.2.0) 15.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev