Tips for reading gfortran compiler error messages 


The compiler tries to tell you what line the error happened.  It also points to where in the line the error was found.  And tries to explain the problem.  

Here are some examples.


Code Example A - compile time errors

Note: I have added line numbers, which are not part of the fortran code.  See that even comment lines are included in the numbering.

1 !Demo of erroneous code
2 program A
3     implicit none
4     write(*,*,advance="no") "Enter your nickname: "
5     read(*,*) nickname
6 end program A


Compiled with the following command

gfortran A.f95 -o Arun

Raw Compiler Message

 In file A.f95:4

 write(*,*,advance="no") "Enter your nickname: "
                  1
Error: List directed format(*) is not allowed with a ADVANCE=specifier at (1).
 In file A.f95:5

 read(*,*) nickname
                  1
Error: Symbol 'nickname' at (1) has no IMPLICIT type


Interpretation

It tells you the file name for the source code (A.f95), and the line number where the error was found (line 4):
 In file A.f95:4


It then prints a copy of that line...
 write(*,*,advance="no") "Enter your nickname: "
                  1

... and prints a digit (1) under the line, positioned so that it points to the place in the line where (or just at the end of where) the error happened.


Then it explains what the error is:
Error: List directed format(*) is not allowed with a ADVANCE=specifier at (1).

.
----------------
.

Apparently, I have another error in this code, this time at line 5:
 In file A.f95:5

So it prints a copy of the line, and points to the error from below:
 read(*,*) nickname
                  1

It explains the error:
Error: Symbol 'nickname' at (1) has no IMPLICIT type

Edits Needed

The first error says that I can't use the second "*" in the write statement (the "*" means "list directed").  So instead I need to give it a format.  For this program, I am writing a string, so I can use format specifier "(a)" which means alphanumeric string.

The second error is because I declared "implicit none" at the start of the program.  Therefore, all variables must be declared in advance; namely, the "implicit none" command tells the compiler to enforce strong typing.  So I need to add a line of code after "implicit none" that says "character (len=20) :: nickname" .   The alternative fix, of removing the "implicit none" line, is NOT allowed in this course.  It is good programming practice to always enforce strong typing.

Fixed Code

!Demo of erroneous code that was fixed
program A
    implicit none
    character (len=20) :: nickname
    write(*,"(a)",advance="no") "Enter your nickname: "
    read(*,*) nickname
end program A


...and indeed it compiles and runs fine.

End Code A




Code Example B - compile time errors

1 !An example of erroneous code
2 program B
3     integer :: i, j, k        !dummy indices
4     do i=1,3
5         j = j+1
6         write(*,*) i, j
7     write(*,*) "Bye"
8 end program B

Compiled with the following command

gfortran B.f95 -o Brun

Raw Compiler Message

In file B.f95:8

end program B
  1
Error: Expecting END DO statement at (1)
Error: Unexpected end of file in 'B.f95'


Interpretation

It shows line 8, and points to the "end" program word.  It explains that it patiently waited to find and "END DO" statement to terminate the "DO" loop that you started earlier in the code, but it got all the way to the end of your whole program and never found it.   Also, it was unhappy because it found your "END PROGRAM" statement unexpectedly early, because it was still waiting for your "END DO" line.  

So both of the error messages refer to the same problem in this case.  It is often the case that the compiler gets confused, and then prints a VERY large number of error messages all stemming from the same one problem.  So if you find an unexplainable error message, it might relate to a problem from an earlier error message.  So fix the stuff you know to be wrong, and then recompile and see of the other error messages also disappear.

Edits Needed

Add an " END DO " line just before the "Bye" line.

Partially Fixed Code

 1 !An example of erroneous code
 2 program B
 3     integer :: i, j, k        !dummy indices
 4     do i=1,3
 5         j = j+1
 6         write(*,*) i, j
 7     enddo
 8     write(*,*) "Bye"
 9 end program B



This compiles just fine, but when you run it, the output is:

           1    15399846
           2    15399847
           3    15399848
 Bye


In this case there was neither a "compile error" nor a "run-time error" (i.e., the program didn't crash), but the programmer blundered nonetheless by forgetting to initialize "j" before adding numbers to it.  Thus, '"j" started with whatever garbage bit pattern happened to be stored at that location in memory.  
 
A fix would be to initialize "j=15" when it was declared.

Partially Re-fixed Code

!An example of erroneous code
program B
    integer :: i, j=15, k        !dummy indices
    do i=1,3
        j = j+1
        write(*,*) i, j
    enddo
    write(*,*) "Bye"
end program B


This compiles and runs just fine, and gives the desired output:

           1          16
           2          17
           3          18
 Bye


Not Quite Bug Free

Although the final code example above runs fine, it still contains a variable "k" that was declared but not used. The code is dirty.  It needs to be cleaned up. 

Some compilers have options that you can set to provide you with "Warnings" of things that are questionable, but not wrong.   Another good option to set is:  array index out of bounds (check your compiler manual to see how to set such an option).

End Code B



Code Example C - a runtime error

Sometimes fortran code will compile OK, but will fail when the compiled code is run.  Sometimes the failure is graceful and error messages are provided (as shown below), and other time the computer just crashes.

integer :: i
real, dimension(120) :: P, z, T, Td
...
write(*,"(I3,4F12.2)")  P(i), z(i), T(i), Td(i)
...

Compiled with the following command

gfortran skewt8.f95 -o srun8

The code compiled with no error messages, and produced the output file srun8 .  When this executable was run, it produced the following:

Raw Runtime Error Message

At line 108 of file skewt8.f95
Fortran runtime error: Expected INTEGER for item 1 in formatted transfer, got REAL
(I3,4F12.2)
 ^

Interpretation

At line 108 of file skewt8.f95
Fortran runtime error: Expected INTEGER for item 1 in formatted transfer, got REAL
(I3,4F12.2)
 ^

The caret ^ in the error message points to the problem.  The format in the write statment specified the first item as integer, but all the items in the list to be printed are all real numbers.

Edits Needed

I forgot that I had wanted to print the array index i first.  So I added i to the output list.

Fixed Code

...
write(*,"(I3,4F12.2)") i, P(i), z(i), T(i), Td(i)    !echo line to screen
...

End Code C


Code Example D - another runtime error

.

real, dimension(5) :: P
integer :: i
...
do i = 1, 10
   P(i) = real(i)
enddo
...

Compiled with the following command

gfortran skewt9.f95 -o srun9

The code compiled with no error messages, and produced the output file srun9 .  When this executable was run, it produced the following:

Raw Runtime Error Message

Segmentation error.

Interpretation

When this error happens, it often doesn't tell you which line it was on.  The cause is that you tried to access a part of the computer memory that you shouldn't have.  

In the example above, it was because my array index  i  went out of bounds of the array.  Namely, I had declared the array to hold 5 elements, but my do loop tries to access more than 5.  

You can also get this error if your array index hasn't been declared, or hasn't been initialized or otherwise defined.  For example, in the following code, I made the mistake of not declaring  i  as an integer.  Also, I didn't set  i  to anything before I tried to use it as an array index.  So if i was some random bit pattern, then it could have been not-a-number, or a large number outside of the array.

implicit none
real, dimension(5) :: P
...
x = P(i)
...

Segmentation errors can also happen when a pointer points to the wrong memory location. 

Edits Needed

Change the array declaration for P to hold 10 elements.

Fixed Code

real, dimension(10) :: P
integer :: i
...
do i = 1, 10
   P(i) = real(i)
enddo
...

A good compiler option to set is to check for array index out of bounds (check your compiler manual to see how to set such an option).

End Code D


Copyright © 2007 by Roland Stull.
Revised Feb 2008.