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 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 formatb.f
to comply to fixed form.b.f
would then look likeFor 2) , you would convert the include files to free form. The include could then be written as:
If you cannot convert all files, I would suggest writing wrapper modules in fixed-form,
include
the source code, and from then onuse
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 likeFor subroutines and functions, you could also use modules: