I am trying to include a file 'a.h' into a Fortran program 'b.f' The contents of the files are as follows:
a.h
c This is a comment
k = 10
100 format( I5 )
b.f
program test_include
include 'a.h'
write(*,100) k
end program test_include
When I try to compile the file 'b.f' using the following command
gfortran -ffree-form b.f
The compiler gives the error
Included at b.f:2:
c This is a comment
1
Error: Unclassifiable statement at (1)
But when I change the comment line to
!c This is a comment
gfortran compiles it successfully and the program runs correctly.
Can someone tell me how to make gfortran recognize lines beginning with 'c' in a '*.h' file as a comment. I am trying to include a similar file (with comments beginning with 'c') from a library into my free-form fortran code, and I can't really make all the comments beginning with 'c' in that file, to begin with '!'.
The include file is in fixed-form! You cannot mix free and fixed form in a single file. Since
[the] effect of the INCLUDE line is as if the referenced source text physically replaced the INCLUDE line priorto program processing [,]
the combined source text needs to be either fixed or free form, but not a mixture of both. [Source: Fortran 2008 Standard, Cl. 3.4 (6)]
This leaves you two options:
For 1), you need to specify -ffixed-form
, and format b.f
to comply to fixed form. b.f
would then look like
program test_include
include 'a.h'
write(*,100) k
end program test_include
For 2) , you would convert the include files to free form. The include could then be written as:
! This is a comment
k = 10
100 format( I5 )
If you cannot convert all files, I would suggest writing wrapper modules in fixed-form, include
the source code, and from then on use
the wrapper modules instead. In case of the snippet you provided, this would require further thought, but in case of include files with only variables/parameters/interfaces this could look like
module wrapper_vars
include 'vars.h'
end module
For subroutines and functions, you could also use modules:
module wrapper_subroutines
contains
include 'subroutines.h'
end module