Combining F77 and F95 fortran code

岁酱吖の 提交于 2019-12-17 21:13:19

问题


I'm working on some scientific code that is mostly F77 but also some F95. In places, I need to include F77 code into my F95 code. Is there a way to get this code to play nicely within my code by using a particular compiler flag or something? I'm using gfortran and occasionally ifort. It is possible for me to modify the legacy code but I would need to do it in a sensible way to maintain backwards compatibility with other F77 code while also being forwards compatible with F95 code.

I get errors like:

cstruc:16.12:
    Included at mod_op.f90:6:

      REAL*8
            1
Error: Invalid character in name at (1)
cstruc:17.6:
    Included at mod_op.f90:6:

     &       RH, RH1,      ! ln rho
      1
Error: Invalid character in name at (1)
cstruc:18.6:
    Included at mod_op.f90:6:

     &       RHP, RHP1,    ! d ln rho / d ln p
      1
Error: Invalid character in name at (1)
cstruc:19.6:
    Included at mod_op.f90:6:

     &       RHT, RHT1,    ! d ln rho / d ln T
      1
Error: Invalid character in name at (1)

cstruc looks like this:

  REAL*8
 &       RH, RH1,      ! ln rho
 &       RHP, RHP1,    ! d ln rho / d ln p
 &       RHT, RHT1,    ! d ln rho / d ln T
 &       PSI,          ! ln Lambda (for degenerate gas)
 &       RHPSI,        ! d ln rho / d PSI
 &       RHPSIP,       ! d2 ln rho / d PSI d ln P
 &       RHPSIT,       ! d2 ln rho / d PSI d ln T
 &       PL,           ! P at J1
 &       TONI          ! T at J1

Any help is much appreciated. Thanks!


回答1:


With some exceptions, Fortran 77 code is Fortran 95 code. I guess that your errors come from that you are attempting to include fixed-form source code (your F77 code in cstruc) into a free-form source code file mod_op.f90. This is unlikely to end well.

Most compilers will assume a file ending in ".f90" is free-form, so if you really are using fixed-form then you will need a compiler flag to override the assumption.

It is possible to combine free- and fixed-form code into a final object (each compiled separately), but a good suggestion as to how to resolve the problems you are seeing can come only with more detail.

However, if you are attempting with your include to create a module to replace a common block, then there is no reason why you can't use the F95 feature with fixed-form. Just do that selectively.

Alternatively, you can see the answer by Vladimir F which explains how to write source code that is valid as both free-form and fixed-form source. You can use this to modify the Fortran 77 fixed-form code to be include-able by the Fortran 90 free-form code while still being compilable as fixed-form (but not valid Fortran 77).




回答2:


I suggest to try the "intersection" form from http://fortranwiki.org/fortran/show/Continuation+lines

It is legal as both free and fixed source form.



来源:https://stackoverflow.com/questions/22048719/combining-f77-and-f95-fortran-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!