Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28381 Discussions

Include files compatible for Fortran 77 and 90

Ben136KBC
Novice
2,125 Views

Supposed you have some .f90 and .for and .f files that include similar snippets. One such snippet might be:

ASYRNG (IXNUM,IXLOW,IXHIGH) = IXNUM.EQ.MAXASS.OR. &
(IXLOW.LE.IXNUM.AND.IXHIGH.GE.IXNUM)

or

         ASYRNG (IXNUM,IXLOW,IXHIGH) = IXNUM.EQ.MAXASS.OR.
      | (IXLOW.LE.IXNUM.AND.IXHIGH.GE.IXNUM)

Note notice the & continuation character at the end of the line. Some versions use a + in column six. Fortran 90 does not like the +.

So is there a continuation style that would work for a snippet included in both Fortran 90 and 77 files?

If not, is there a way to tell in the code via an !ifdef if it is compiling F90 or not? It appears not.

I have seen one place where people do this:

         ASYRNG (IXNUM,IXLOW,IXHIGH) = IXNUM.EQ.MAXASS.OR. &
      & (IXLOW.LE.IXNUM.AND.IXHIGH.GE.IXNUM)

that is, they put an ampersand at the end of the line and in column 6 on the next line. Appears to work?

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
2,061 Views

See Source Code Useable for All Source Forms (intel.com)

 

Blanks
Treat as significant (see Free Source Form).
Statement labels
Place in column positions 1 through 5 
(or before the first tab character)
.
Statements
Start in column position 7 
(or after the first tab character)
.
Comment indicator
Use only !. Place anywhere 
except
 in column position 6 
(or immediately after the first tab character)
.
Continuation indicator
Use only &. Place in column position 73 of the initial line and each continuation line, and in column 6 of each continuation line 
(no tab character can precede the ampersand in column 6)
.

View solution in original post

0 Kudos
17 Replies
Ben136KBC
Novice
2,124 Views

On the same topic, comment styles. I gather using an exclamation mark in the first column is universally accepted?

0 Kudos
mecej4
Honored Contributor III
2,097 Views

The following code is accepted whether you name the file containing it with suffix .f or .f90.

 

      program tst
      implicit none
      integer :: ixnum, ixlow, ixhigh, maxass
      real, dimension(2,2,2) :: AsyRNG
!2345678901234567890123456789012345678901234567890123456789012345678901234567890
!        1         2         3         4         5         6         7
      ASYRNG (IXNUM,IXLOW,IXHIGH) = IXNUM.EQ.MAXASS.OR.                 &
     &   (IXLOW.LE.IXNUM.AND.IXHIGH.GE.IXNUM)
      end program

 

Note that the Fortran 90+ continuation character, '&', is placed beyond column 72 on line 7.  Look up the rules for Fortran source in fixed and free form and infer why this code snippet works.

Some of the include files used in MKL and other software packages exploit such features to allow *.INC files to be inserted in both fixed and free form source files.

There are compiler switches as well as directives that you can place in your source files to control whether to regard a portion of or the entire source  file to be regarded as being in a particular form, ignoring the file suffix conventions. These switches and directives are compiler-specific.

0 Kudos
mecej4
Honored Contributor III
2,095 Views

Universally accepted? For a sufficiently narrow universe, perhaps.

The '&' and '!' characters were not included in the Fortran character set in earlier versions of Fortran. See, for instance, section 3.1 of the Fortran IV Standard .

0 Kudos
andrew_4619
Honored Contributor II
2,079 Views

But at a tangent, the code snippets shown if used a lot might be better as a function that can be called where needed. Much better than a clunky solution of lots of include files that insert repeat code. There are a whole list of reasons, code length, compile time, build cascades, maintenance, ...

0 Kudos
FortranFan
Honored Contributor II
2,061 Views
@Ben136KBC wrote:
.. So is there a continuation style that would work for a snippet included in both Fortran 90 and 77 files?

If not, is there a way to tell in the code via an !ifdef if it is compiling F90 or not? It appears not...

 

@Ben136KBC ,

This is more an FYI than anything else: take a look at FREEFORM compiler directive.

If one is forced to INCLUDE files (only under very particular conditions that get rarer with time one can hope), then one may preferably have them in free-form source (what you call F90) format for convenience.  And such INCLUDE segments can be encoded in !DIR$ FREEFORM .. !DIR$ NOFREEFORM directives in the fixed form (what you call 77) source files:

C fixed-form source file: .for or .f
C234567
      ..
!DIR$ FREEFORM
include 'asyrng.i90
!DIR$ NOFREEFORM
      ..     

 

P.S.> current Fortran standard - which is Fortran 2018 i.e., way beyond Fortran 90 - still supports both free-form and fixed-form source formats.  Thus it's not really a 90 vs 77 matter.  And it will be better if you can update your terminology can use free-form source vs fixed-source terms.

0 Kudos
Ben136KBC
Novice
2,004 Views

I like the FREEFORM idea, except what if the includes are in many files? Then you want to put the FREEFORM in the included files, but will the NOFREEFORM at the end cause problems if included in a .f90 file?

0 Kudos
Steve_Lionel
Honored Contributor III
2,062 Views

See Source Code Useable for All Source Forms (intel.com)

 

Blanks
Treat as significant (see Free Source Form).
Statement labels
Place in column positions 1 through 5 
(or before the first tab character)
.
Statements
Start in column position 7 
(or after the first tab character)
.
Comment indicator
Use only !. Place anywhere 
except
 in column position 6 
(or immediately after the first tab character)
.
Continuation indicator
Use only &. Place in column position 73 of the initial line and each continuation line, and in column 6 of each continuation line 
(no tab character can precede the ampersand in column 6)
.
0 Kudos
Ben136KBC
Novice
2,005 Views

Sadly, .for files with Intel Fortran do not seem to follow the continuation rule, observe:

..\Petrofine\includes\METHOD_NOS.INC(14): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( <IDENTIFIER> <CHAR_CON_KIND_PARAM> <CHAR_NAM_KIND_PARAM> <CHARACTER_CONSTANT> <INTEGER_CONSTANT> ...
      & pkpmvc,pkltmo,pklvit,pklcan,pklpps,pklppe,pkpci4, &

--------------------------------------------------------------------------------^

compilation aborted for ..\kbclibv2\FortranSource\bdmeth2.for (code 1)

 

And her I carefully made sure there are no tabs, and an & in column 6 and 73.  The ^ points just part the ampersand in column 73.

I can make the comments work in all the Fortran files by using ! in the first column. CHECK.

I can make variable and common declaration continuations work by editing them to not have any continuations. CHECK

But with data statements, I cannot find a way that works for .f90 and .for files, and you have to use continuations with no way that seems to work in all Fortran files.  EEEEEP!!

0 Kudos
Ben136KBC
Novice
1,993 Views

The funny thing is that the ampersand in those two places do work. Sometimes.  But not if in an included .INC file maybe?

0 Kudos
Steve_Lionel
Honored Contributor III
1,991 Views

Please attach a file which you say is not accepted. Attach it, do not paste into a reply.

0 Kudos
Ben136KBC
Novice
1,986 Views

I shall prepare one, have a few meetings.

0 Kudos
Ben136KBC
Novice
1,984 Views

Is there a way to rewrite this snippet (it is in an .INC file which is included in various other files) to not have any continuation characters ?

      DOUBLE PRECISION LV18(18)
      DATA LV18 / 0.0D0,  1.0D0, 2.0D0,  5.0D0, 10.0D0, 15.0D0, 20.0D0, 
     +           30.0D0, 40.0D0, 50.0D0, 60.D0, 70.0D0, 80.0D0, 90.0D0,
     +           95.0D0, 98.0D0, 99.0D0, 100.0D0 /

 

0 Kudos
Steve_Lionel
Honored Contributor III
1,952 Views
D:\Projects>type t.inc
      DOUBLE PRECISION LV18(18)
      DATA LV18 / 0.0D0,  1.0D0, 2.0D0,  5.0D0, 10.0D0, 15.0D0, 20.0D0, &
     &           30.0D0, 40.0D0, 50.0D0, 60.D0, 70.0D0, 80.0D0, 90.0D0, &
     &           95.0D0, 98.0D0, 99.0D0, 100.0D0 /
D:\Projects>type t.f90
include 't.inc'
print *, lv18(18)
end
D:\Projects>type t.for
      include 't.inc'
      print *, lv18(18)
      end
D:\Projects>ifort -c t.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.2.0 Build 20210228_000000
Copyright (C) 1985-2021 Intel Corporation.  All rights reserved.


D:\Projects>ifort -c t.for
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.2.0 Build 20210228_000000
Copyright (C) 1985-2021 Intel Corporation.  All rights reserved.
0 Kudos
Ben136KBC
Novice
1,925 Views

Lionel, upon CAREFUL scrutiny, you are right.  But it matters precisely whether you have spaces and how many or tabs (arg) and what continuation character you use.  You have to have it exactly right then it works.

0 Kudos
Steve_Lionel
Honored Contributor III
1,921 Views

Don't use tabs. You do need to have the left hand continuation & characters in column 6. The right-hand & can be anywhere between 73 and 132, but 73 makes the most sense.

0 Kudos
Ben136KBC
Novice
1,865 Views

Yes, any single tab anywhere and the ampersand approach doesn't work. But now that I know that, I can safely and reliably fix all my include files to work in .FOR and .F90 files. THANK YOU Lionel! Black belt well deserved.

0 Kudos
andrew_4619
Honored Contributor II
1,838 Views
Tabs are not allowed in standard conforming Fortran. If you set the standards checking option /stand they give compile errors.
0 Kudos
Reply