Fortran 中的 CPP 预处理语句可以缩进吗? [英] Can CPP preprocessing statement in Fortran be indented?
问题描述
我对使用 Fortran 预处理语句还很陌生,并且有一个可能非常原生的问题.Fortran 预处理语句可以缩进吗?我在 Linux (openSUSE Leap) 上使用 Gfortran 4.8.1 进行了测试,结果发现它根本不能缩进.
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.
以下代码 main.f90 与 gfortran -cpp main.f90 -o main
一起使用:
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
但是下面会报错:
program main
implicit none
#ifdef DEBUG
print *, "I am in debug mode"
#endif
print *, "hello world!"
end program main
错误消息是 Error: Invalid character in name at (1)
.这是否意味着我们应该始终从行首开始编写预处理语句,或者它只是编译器特定的规则?任何帮助将不胜感激,并提前致谢!
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!
推荐答案
不,它们不能缩进,因为 gfortran 在 传统模式,不允许缩进.它们必须始终从第一列开始.
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.
您可以手动运行 CPP,但要非常小心.如果您在某处使用 //
字符串连接运算符,预处理器会将其视为注释.您必须使用@ewcz 在他/她的答案中显示的 -C
标志,该标志禁止丢弃评论.
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.
一些编译器提供他们自己的 FPP 预处理器,其行为不同.
Some compilers supply their own FPP preprocessor which behaves differently.
这篇关于Fortran 中的 CPP 预处理语句可以缩进吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!