views:

117

answers:

1

I've been tasked with maintaing some legacy fortran code, and I'm having trouble getting it to compile with gfortran. I've written a fair amount of Fortran 95, but this is my first experience with Fortran 77. This snippet of code is the problematic one:

      CHARACTER*22 IFILE, OFILE
      IFILE='TEST.IN'
      OFILE='TEST.OUT'
      OPEN(5,FILE=IFILE,STATUS='NEW')   
      OPEN(6,FILE=OFILE,STATUS='NEW')
      common/pabcde/nfghi

When I compile with gfortran file.FOR, all lines starting with the common statement are errors (e.g. Error: Unexpected COMMON statement at (1) for each following line until it hits the 25 error limit). I compiled with -Wall -pedantic, but fixing the warnings did not fix this problem.

The crazy thing is that if I comment out all 4 lines starting with IF='TEST.IN', the program compiles and works as expected, but I must comment out all of them. Leaving any of them uncommented gives me the same errors starting with the common statement. If I comment out the common statement, I get the same errors, just starting on the following line.

I am on OS X Leopard (not Snow Leopard) using gfortran. I've used this very system with gfortran extensively to write Fortran 95 programs, so in theory the compiler itself is sane. What the hell is going on with this code?


Edit: Compiling with g77 gives:

test.FOR: In program `MAIN__':
test.FOR:154: 
     IFILE='TEST.IN'
     1
test.FOR:158: (continued):
     common/pabcde/nfghi
     2
Statement at (2) invalid in context established by statement at (1)

Er, what context is established at (1)?

+4  A: 

I don't think you can put COMMON statements below the executable statements in FORTRAN 77, see the specification, Sec. 3.5. Just move the COMMON statement close to the beginning of the procedure, before any executable statement.

Yuji
Ahh, of course. I kind of thought of that, but I needed to move the executable statements waaay down, and didn't move them far enough initially. Reading the spec helped decipher some other errors I was getting, too.
notJim