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

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

问题描述

我想了解预处理器内联如何包含到 Fortran 代码中.使用 C,这很简单:

Test.c:

#include <stdio.h>诠释主要(无效){返回0;}

然后我编译使用:

gcc -E test.c

然后它按预期显示由 C 预处理器生成的内容.

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

Test.f:

程序测试包括mpif.h"调用 mpi_init调用 mpi_finalize结尾

然后我运行:

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

但我不会得到预期的结果,即生成的嵌入到代码中的包含.

相反,我有这个:

# 1 "test.f"# 1 "<内置>"#1 "<命令行>"# 1 测试.f"程序测试包括mpif.h"调用 mpi_init调用 mpi_finalize结尾

我在这里做错了什么?

解决方案

Fortran有自己的include 指令不能与预处理器指令 #include 混淆.据我了解,包含的代码没有嵌入到主文件中,而是编译器继续从包含文件编译,并在该文件的末尾返回到主文件.从这里:p><块引用>

INCLUDE 语句指示编译器停止读取语句从当前文件中读取包含的文件或文本中的语句模块.

另外,included 文件进一步预处理,而 #included 文件是.

请注意,还有一个命名约定,它只对带有大写后缀 *.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天全站免登陆