带换行符的Fortran预处理器宏
我试图在预处理器宏中放入换行符 我希望这样做的一个原因是取消分配许多变量,但首先检查以确保它们已分配。如果它们没有被分配,那么应该有一个有用的错误。在本例中:带换行符的Fortran预处理器宏,fortran,c-preprocessor,preprocessor,Fortran,C Preprocessor,Preprocessor,我试图在预处理器宏中放入换行符 我希望这样做的一个原因是取消分配许多变量,但首先检查以确保它们已分配。如果它们没有被分配,那么应该有一个有用的错误。在本例中: $ cat main.F90 program main implicit none integer :: n = 10 integer, allocatable, dimension(:) :: x, y allocate(x(n)) #define check_deallocate(QQQ) \ if
$ cat main.F90
program main
implicit none
integer :: n = 10
integer, allocatable, dimension(:) :: x, y
allocate(x(n))
#define check_deallocate(QQQ) \
if (.not. allocated(QQQ)) then \
write(*,*) '** Error, QQQ is not allocated' \
error stop \
else \
deallocate(QQQ) \
endif
check_deallocate(x)
check_deallocate(y)
end program
对于这段代码,我得到以下信息:
$ gfortran -E main.F90 #-E outputs the preprocessed source
...
if (.not. allocated(x)) then write(*,*) '** Error, x is not allocated' error stop else deallocate(x) endif
if (.not. allocated(y)) then write(*,*) '** Error, y is not allocated' error stop else deallocate(y) endif
...
SUFFIXES:
.SUFFIXES: .f90 .o
.f90.o:
fpp -P $/n/g' > $*.ftn
ifort -free -c $*.ftn
显然,这是不可取的,因为没有遵守132个字符的限制。因此我的问题是:在预处理的源代码中包含换行符的语法是什么?
我发现有些东西不能完全回答我的问题
- 我不想定义另一个宏(如
)并使用\uuuuuu CR\uuuu
或其他实用程序将其替换为sed
。我希望解决方案存在于“典型”预处理器中(例如ifort和gfortran)\n
如果我所要求的是不可能的,这也是我问题的答案。另外,我知道fortran预处理器缺乏标准化,但我仍然要求提供一个相对通用的解决方案。这本身是不可能的,但如果允许使用单独的预处理步骤,您可以使用以下方法:
$ gfortran -E main.F90 #-E outputs the preprocessed source
...
if (.not. allocated(x)) then write(*,*) '** Error, x is not allocated' error stop else deallocate(x) endif
if (.not. allocated(y)) then write(*,*) '** Error, y is not allocated' error stop else deallocate(y) endif
...
SUFFIXES:
.SUFFIXES: .f90 .o
.f90.o:
fpp -P $/n/g' > $*.ftn
ifort -free -c $*.ftn
(未经测试。我在上找到此建议)我看到了该链接,但我不理解
-p
参数对fpp的实际作用。你能解释一下吗?此外,(如问题中所述),我正试图避免使用其他实用程序或增加复杂性,例如通过所需的独立fpp调用。如果您可以为您的第一个短语提供来源(或甚至是答案代表一段时间),仅凭它自己是不可能的,这对我来说就足够了。不过,我觉得有点奇怪……链接的文章似乎表明这是不可能的。这是我对该语句的唯一来源。@Ross:P指令用于防止混淆,因为行号可以通过下面的命令更改。ifort只支持C/C++预处理器指令的一个子集: