gfortran -cpp sometimes confused by '/*'

sam tygier samtygier@yahoo.co.uk
Fri Jun 26 11:47:00 GMT 2015


On 25/06/15 17:23, Steve Kargl wrote:
> On Thu, Jun 25, 2015 at 04:34:31PM +0100, sam tygier wrote:
>> I am trying to use some #ifdef in some fortran code in order to smooth
>> over a gfortran/ifort difference. This requires using the flag -cpp.
>> However this causes a line containing '\*' to be misinterpreted giving
>> the error:
>> Error: unterminated comment
>> I have tried with 5.1.1 and 4.7.2.
>>
>> I can reduce the issue down to the program:
>>          PROGRAM a
>>           WRITE(*,FMT='( '' /* '' )')
>>           WRITE(*,FMT='(
>>        >  '' /* '' )')
>>          ENDPROGRAM
>> This compile and runs fine with out -cpp, but the second write statement
>> fails with -cpp. Maybe because the parser in cpp detects the first
>> instance as being within a string. In ifort it does not cause a problem.
>>
>> While I can work around this, it seems like the wrong behaviour. Is this
>> a bug, or just an inherent limitation.
>>
> Yes, no, maybe.
>
> The -cpp is instructing gfortran to pass your code through
> the C pre-processor before it actually passes the code
> through the Fortran compiler.  What a C preprocessor does
> to the code is outside of the scope of the Fortran standard.
> A programmer that wishes to use a C preprocessor needs
> to ensure the output of that preprocessing is actual Fortran.
> In your case, the pre-processing output is obviously 'not Fortran'.
>
> So, the C pre-processor appears to not understand splitting
> a string across lines via Fortran's continuation mechanism.
>
So is there a need for a fortran preprocessor, or a fortran mode in cpp? 
I.e. one that either understood fortran's syntax enough to decide 
whether something is inside a string literal or not, or one that does 
not remove c/c++ style comments.

Thanks,
Sam



More information about the Fortran mailing list