Using Do-Loops in Include Files

465 views Asked by At

In my fortran code, I'm attempting to include a DO loop, but I'm ending up with a "DO loop or BLOCK IF not closed." It appears if any other DO loops after the INCLUDE statement are opened it treats them as nested loops, indicating to me the included opening DO line is interpreted correctly, but not the END DO statement. I've reduced the included code to bare-bones to make sure it's the loop and not the statements in the loop giving a problem. The control variable is declared before the DO loop.

DO A = 1,3
END DO

Does Fortran77 not allow for DO loops in INCLUDE files?

I'm using gfortran for my compiler if it changes much.

Edit: grammar

Edit2: I'm using GCC 4.6.2. Now to note, if this makes a difference, gfortran is being run from a makefile made by PSCAD. I can provide info on that if it's pertinent.

Here's code that is experiencing this issue:

TEST.F:

SUBROUTINE TESTFX() 
INTEGER A 
INCLUDE '../HDR.INC' 
END

HDR.INC:

DO A = 1,3 
END DO

Edit3: Edited typos in code and removed RETURN from subroutine.

2

There are 2 answers

1
Vladimir F Героям слава On

Your INCLUDE must be placed on a new line, it is not part of the statement declaring A. Why do you have apostrophes at your ENDs? They cannot be there. The RETURN statement before the END is totally superfluous also. Try:

TEST.F:

SUBROUTINE TESTFX() 

INTEGER A

INCLUDE '../HDR.INC' 

END SUBROUTINE

HDR.INC:

DO A = 1,3 
END DO
2
Steve Lionel On

Fortran 77 doesn't have INCLUDE at all. That first made its appearance in a Fortran standard in Fortran 90. That said, INCLUDE was available as an extension in pretty much all Fortran 77 compilers and the behavior was the same - it is essentially the same as inserting the included file in the source file where the INCLUDE appears. There are no restrictions on what can be there, though I have seen compilers struggle with issues related to block constructs that straddle an include file boundary.

Perhaps if you included both the source file and the include file text and told us exactly which version of gfortran you're using, a more definitive response could be provided.