Search code examples
fortrangfortran

Combining F77 and F95 fortran code


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!


Solution

  • 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).