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)?
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.
Related
I am editing an old project that uses fixed form Fortran and compiling with IVF compiler. The current issue I have is with continuation lines in a list:
format(//, 10x,'*******************************************',/, &
10x,'* DIAGONALS OF THE RESIDUAL COV. MATRIX *',/, &
10x,'*******************************************',//, &
2x,'MEASUREMENT',7X,' RESIDUAL COVARIANCE', /)
For some reason, the ampersand is not working for me and I keep getting the error:
unrecognized token '&' skipped
For smaller lines, increasing the fixed form line length and making the two lines one worked but there are instances where the lines are too large for this. The code was written around 15 years ago and in fixed form Fortran but I am unfamiliar with Fortran and how the new compiler and settings affect the code.
Converting to free form causes a series of errors with other formatting and the code does not seem broken so I do not think converting to free form is necessary. I have tried other methods of indenting, such as an ampersand at the end of a line and at the beginning of next, an asterisk, and a slash that other forums suggested using and they produce the error:
error #5082: Syntax error, found END-OF-STATEMENT when expecting one
of: <HOLLERITH_CONSTANT> <CHAR_CON_KIND_PARAM> <CHAR_NAM_KIND_PARAM>
<CHARACTER_CONSTANT> ) ...
Is there some sort of formatting I am missing or is there any settings I could edit to fix these errors?
In fixed form Fortran, you continue a line with any character in column 6 of the next line, not an & at the end of the 1st line. Try:
format(//, 10x,'*******************************************',/,
c 10x,'* DIAGONALS OF THE RESIDUAL COV. MATRIX *',/,
c 10x,'*******************************************',//,
c 2x,'MEASUREMENT',7X,' RESIDUAL COVARIANCE', /)
Or use the compiler switch -free with .for or .f
Or use the compiler switch -fixed -132 with a .F90 .
In your case I would preserve the .f and cp that to .F90 and then explicitly have the makefile compile the .F90 ...
I normally use -fixed -132 with a .F90 as I often have -d-lines that I retain in the code, and I could not get -d-lines. To work with -free.
Is there any command line argument to list the total number of errors in the particular compilation. This would be useful when solving a hell lot of errors or when the sources are large in number.
IDE's like Eclipse used to list the total number of errors and warnings.
Is there any command line argument to list the total number of errors in the particular compilation
No there isn't. The feature was requested by GCC bug 26061, Feb. 2006,
which has never been accepted.
I've noticed that when running gcc, if the compile failes, most of the time it returns an exit status code of '1'. However, it sometimes returns a status code of '4' (for example, if the input file does not exist). I haven't been able to find anything in the gcc documentation that covers what different error codes might mean -- does anyone know of any?
According to the official documentation of the command line switch -pass-exit-codes:
Normally the gcc program exits with the code of 1 if any phase of the compiler returns a non-success return code. If you specify -pass-exit-codes, the gcc program instead returns with the numerically highest error produced by any phase returning an error indication. The C, C++, and Fortran front ends return 4 if an internal compiler error is encountered.
I just changed .f to .f90 and compiled with gfortran on my Mac (OSX 10.7) and found it fails compilation on the following line:
PartPos(1:3,1+nstart:nstart+npart(1))= pos(1:3, 1 + npart(0)):sum(npart(0:1)))
further up it is allocated and defined:
real*4,allocatable :: PartPos(:,:)
...
allocate(PartPos(1:3, 1:Ntot))
so it should run fine. The code compiles perfectly when I comment out the first line. My understanding is that the only difference between the two is the formatting (which could affect me). Could someone tell me what I'm missing. Sorry, I'm new to Fortran and have been searching for a solution for quite a while before I came here. Thanks in advance.
My (somewhat useless) error output:
PartPos(1:3,1+nstart:nstart+npart(1))= pos(1:3, 1 + npart(0)):sum(npart(0
1
Error: Unclassifiable statement at (1)
Compiled using:
>> gfortran program_test.f90 -o program_test
Your RHS appears to be missing a parenthesis [I see 4 "(" and 5 ")"].
I would like to compile a Fortran 90 (fixed format) library under Windows. However, I cannot understand the error of prepocessor variables.
Say the sample file is VF_TestPreprocessor.F:
program VF_TestPreprocessor
implicit Integer(A-Z)
Parameter (TestAlpha=22,TestBeta=TestGamma)
print *, TestBeta
end program VF_TestPreprocessor
Under Linux, I can use ifort VF_TestPreprocessor.F -DTestGamma=25 to compile, and run.
However, under windows, I cannot use ifort VF_TestPreprocessor.F /DTestGamma=25 to compile. The error message is error #6592: This symbol must be a defined parameter, an enumerator, or an argument of an inquiry function that evaluates to a compile-time constant. [TestGamma]. Could you help to analyse the error?
It looks like ifort doesn't run the preprocessor. I have no experience with ifort, but this page (the first hit in Google on ifort preprocessor) says that on Windows the preprocessor is only run on files ending in an extension of .fpp.
So, I guess there are (at least) two solutions:
Rename your files to end in .fpp;
invoke ifort with the /fpp switch.