本文介绍了具有C/C ++样式宏的Fortran 90(例如#define SUBNAME(x)s ## x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在处理F90代码项目.我使用gfortran(Linux和MinGW的)进行编译.文件loct.F90中有一些有趣的东西.

I am recently working with a F90 code project. I am using gfortran (linux and MinGW) to compile it. There is something interesting in file loct.F90.

#  define TYPE real(4)
#  define SUBNAME(x) s ## x
#  include "loct_inc.F90"
#  undef SUBNAME
#  undef TYPE

#  define TYPE real(8)
#  define SUBNAME(x) d ## x
#  include "loct_inc.F90"
#  undef SUBNAME
#  undef TYPE
...

在loct_inc.F90文件看起来像这样:

The loct_inc.F90 file looks like this:

subroutine SUBNAME(loct_pointer_copy_1)(o, i)
...
end subroutine SUBNAME(loct_pointer_copy_1)


subroutine SUBNAME(loct_pointer_copy_2)(o, i)
...
end subroutine SUBNAME(loct_pointer_copy_2)


...

我认为该文件中loct.F90作者使用套宏指令(C/C ++样式).每一组用于定义数据类型(例如,实数(4),实(8),字符等).文件loct_inc.F90提供了一组函数,除了变量的类型外,其余函数相同.这两个文件一起工作,如C ++在我看来的模板.

I think in the file loct.F90 the author used sets of macros (C/C++ style). Each set is used to define a data type (e.g. real(4), real(8), character, etc). The file loct_inc.F90 provide a set of function which is the same except the type of the variables.These two files works together as a template of c++ in my opinion.

在结束一个应当具有一组子程序:

In the end one should have a set of subroutines:

sloct_pointer_copy_1(o, i)
sloct_pointer_copy_2(o, i)
...

dloct_pointer_copy_1(o, i)
dloct_pointer_copy_2(o, i)
...

但是,当我试图编译loct.F90(gfortran -c loct.F90),我得到了一些错误.

But when I tried to compile loct.F90 (gfortran -c loct.F90), I get some errors.

子例程s ## loct_pointer_copy_1(o,i) 1个错误:语法错误在SUBROUTINE语句在(1)

subroutine s ## loct_pointer_copy_1(o, i) 1 Error: Syntax error in SUBROUTINE statement at (1)

似乎gfortran用s ## loct_pointer_copy_1(o,i)替换了SUBNAME(loct_pointer_copy_1)(o,i).但是根据c ++宏,正确的替换应该是sloct_pointer_copy_1(o,i).

It seems gfortran replace SUBNAME(loct_pointer_copy_1)(o, i) with s ## loct_pointer_copy_1(o, i). But according to c++ macro, the correct replace should be sloct_pointer_copy_1(o, i).

谁能告诉我为什么会这样吗?

Could anyone tell me why this happened?

推荐答案

GNU Fortran在传统模式下使用GNU C预处理器,在该模式下,宏粘贴运算符##不可用.这就是为什么它被写入也与GNU的Fortran编译工具链的项目中额外的Makefile目标进行明确的预处理,例如所有是第一预处理的与临时档,然后编译.

GNU Fortran uses the GNU C Preprocessor in traditional mode, in which mode the macro pasting operator ## is not available. That's why Fortran projects which were written to also compile with the GNU toolchain perform explicit preprocessing in additional Makefile targets, e.g. all *.F90 are first preprocessed with cpp to temporary .f90 files which are then compiled.

这篇关于具有C/C ++样式宏的Fortran 90(例如#define SUBNAME(x)s ## x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 06:16