Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

c preprocessor - Can CPP preprocessing statement in Fortran be indented?

I am fairly new to use Fortran preprocessing statement and have a question which is probably pretty native. Can Fortran preprocessing statement be indented? I tested using Gfortran 4.8.1 on Linux (openSUSE Leap) and it turned out I it can not be indented at all.

The following code main.f90 works with gfortran -cpp main.f90 -o main:

program main
    implicit none
#ifdef DEBUG
    print *, "I am in debug mode"
#endif 
    print *, "hello world!"
end program main

But the following throws an error:

program main
    implicit none
    #ifdef DEBUG
    print *, "I am in debug mode"
    #endif 
    print *, "hello world!"
end program main

The error message is Error: Invalid character in name at (1). Does this mean that we should always write the preprocessing statement from the first beginning of the line or it is just a compiler specific rule? Any help would be greatly appreciated and thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, they cannot be indented because gfortran runs CPP in traditional mode which does not allow indentation. They must always start in the first column.

You could run CPP manually, but be very very careful about that. If you use the // string concatenation operator somewhere the preprocessor would treat it as a comment. You must use the -C flag as shown by @ewcz in his/her answer which disables discarding of comments.

Some compilers supply their own FPP preprocessor which behaves differently.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...