gfortran 无法识别 .h 文件中第一列以 'c' 开头的注释

gfortran does not recognize comments in .h file that begin with 'c' in first column

我正在尝试将文件 'a.h' 包含到 Fortran 程序中 'b.f' 文件内容如下:

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

当我尝试使用以下命令编译文件 'b.f' 时

gfortran -ffree-form b.f

编译器报错

    Included at b.f:2:
c     This is a comment
1
Error: Unclassifiable statement at (1)

但是当我把注释行改成

!c    This is a comment

gfortran编译成功,程序运行正常

谁能告诉我如何让 gfortran 识别“*.h”文件中以 'c' 开头的行作为注释。我正在尝试将库中的一个类似文件(注释以 'c' 开头)包含到我的自由格式的 Fortran 代码中,但我无法真正在该文件中制作所有以 'c' 开头的注释, 以 '!' 开头。

包含文件是固定格式的!您不能在一个文件中混合使用自由格式和固定格式。自

[the] effect of the INCLUDE line is as if the referenced source text physically replaced the INCLUDE line priorto program processing [,]

合并的源文本需要是固定格式或自由格式,但不能是两者的混合。 [来源:Fortran 2008 标准,Cl。 3.4 (6)]

这给您留下了两个选择:

  1. 将主程序转换为固定形式,或者
  2. 将包含文件转换为自由格式。

对于1),需要指定-ffixed-form,格式b.f以符合固定格式。 b.f 看起来像

      program test_include
          include 'a.h'
          write(*,100) k
      end program test_include

对于 2) ,您会将包含文件转换为自由格式。包含可以写成:

!     This is a comment
      k = 10
100  format( I5 )

如果您不能转换所有文件,我建议以固定形式编写包装器模块,include 源代码,然后 use 包装器模块。如果是您提供的代码片段,则需要进一步考虑,但如果包含仅包含 variables/parameters/interfaces 的文件,则可能看起来像

      module wrapper_vars
          include 'vars.h'
      end module

对于子例程和函数,您还可以使用模块:

      module wrapper_subroutines
      contains
          include 'subroutines.h'
      end module