包括使用Fortran预处理器进行显示 [英] Includes revealing with Fortran preprocessor

查看:217
本文介绍了包括使用Fortran预处理器进行显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解预处理器内联包含在Fortran代码中的方式。使用C,它非常简单:

Test.c:
$ b

  #include< stdio.h> 

int main(void){
return 0;
}

然后我编译使用:

  gcc -E test.c 

然后它显示由C预处理器生成的内容,如预期的那样。



现在假设我有这个Fortran代码:

Test.f:

 程序测试
包含mpif.h
call mpi_init
call mpi_finalize
end

然后我运行:

  gfortran -E -cpp test.f //由于某些原因,我在Fortran中使用-E时需要-cpp 

但是我不会有预期的结果,这是生成的包含嵌入到代码中的结果。



取而代之的是:

 #1test.f
# 1<内置>
#1< command-line>
#1test.f
程序测试
包含'mpif.h'

调用mpi_init

调用mpi_finalize
结束

我在这里做错了什么?

解决方案

Fortran有自己的 include 指令,它不能与预处理指令 #include 混淆。据我了解,包含的代码并未嵌入到主文件中,但编译器继续从包含文件进行编译,并返回到该文件末尾的主文件。从此处: p>


INCLUDE语句指示编译器停止从当前文件中读取语句
,并在包含文件或文本中读取语句

另外, include d文件不是 / em>进一步预处理,而 #include d个是。注意,还有一个命名约定,它使预处理器仅适用于大写后缀 *。F 的文件。 *。F90 。如果要预处理 * .f * .f90 文件,则需要指定在编译选项中,例如 -cpp 用于 gfortran -fpp 用于 ifort

I would like to understand how the preprocessor inlines includes into the code in Fortran. With C, it's pretty simple:

Test.c:

#include <stdio.h>

int main(void) {
    return 0;
}

Then I compile using:

gcc -E test.c

Then it displays the content generated by the C preprocessor, as expected.

Now assume I have this Fortran code:

Test.f:

program test
include "mpif.h"
call mpi_init
call mpi_finalize
end

Then I run:

gfortran -E -cpp test.f // For some reason I need -cpp when using -E in Fortran

But I won't have the expected result, which is the generated include embedded into the code.

Instead, I have this:

# 1 "test.f"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.f"
   program test
   include  'mpif.h'

   call mpi_init

   call mpi_finalize
   end

What am I doing wrong here?

解决方案

Fortran has its own include directive which must not be confused with the preprocessor directive #include. As far as I understand it, the included code is not embedded into the master file, but the compiler instead continues to compile from the include file, and returns to the master file at the end of that file. From here:

The INCLUDE statement directs the compiler to stop reading statements from the current file and read statements in an included file or text module.

Also, included files are not preprocessed further, while #included ones are.

Note, that there is also a naming convention that enables the preprocessor only on files with capital suffixes *.F and *.F90. If you want to preprocess *.f or *.f90 files, you need to specify that in a compile option, e.g. -cpp for gfortran, and -fpp for ifort.

这篇关于包括使用Fortran预处理器进行显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆