【问题标题】:An alternative to Fortran CPP-like preprocessorsFortran CPP 类预处理器的替代方案
【发布时间】:2017-05-18 13:14:37
【问题描述】:

我正在我的 Fortran 90 代码中寻找 #ifdef #else #enddef 的替代方法。 是否有另一种方法来控制调用模块时执行的 use 语句?我想摆脱复杂系统中的#include 文件。

例如,这就是我现在所拥有的。

#include "defs.h"
module X
#ifdef Sys
use ....
#else
use ....
#endif
implicit none

snip...
#ifdef Sys
some block of code...
#else
some block of code...
#endif
end module X

在 defs.h 中定义了 Sys。我想找到使用 defs.h 来控制代码的#ifdef.... 部分的替代方法。

感谢您对此的任何想法。

【问题讨论】:

  • #include 的使用与#ifdef 等的使用不是完全分开的吗?你想避免这两种结构还是只使用#include
  • 我正在使用 FCM 编译一个 fortran 系统,这是一个对我来说非常新且未知的系统。即使路径正确,它也找不到我添加的包含文件。我想控制使用#ifdef XXX 控制的某些“使用”命令和块代码的执行,其中XXX#include 文件中定义。所以是的,如果可能的话,我想避免这两种结构。对于代码的块部分执行此操作很容易,但序言,即use .... 更难,而且我遇到问题的部分。
  • 展示代码示例并说明您正在做什么会非常有帮助。否则人们会开始提出建议,而您会说由于某些您未提及的原因它不适用。
  • 好的。我添加了一个例子。我希望这可以减少歧义。
  • 我应该注意到您的真正问题似乎是在使用 FCM 时cannot find a include file that I added even though the paths are correct.,最好专注于解决此问题而不是想出解决方法?

标签: fortran c-preprocessor fortran90 preprocessor


【解决方案1】:

我将首先重复 cmets 中指出的内容:真正解决您的实际问题的正确方法是弄清楚为什么在使用 FCM 时编译器找不到您希望包含的文件。

为了解决您的具体问题,由于无法成功include 提供各种定义的给定文件,我们可以改为通过传递给编译器的参数来定义符号。

考虑以下,存储在test.fpp

#ifdef Sys
#warning "This messages tells you Sys is defined"
#else
#warning "This messages tells you Sys is NOT defined"
#endif

program test
  implicit none
  write(*,'("For clarity we will now print defined if Sys is defined or not defined if Sys is not defined")')
#ifdef Sys
  write(*,'("Defined")')
#else
  write(*,'("Not defined")')
#endif
end program test

我们可以使用gfortran -ffree-form test.fpp -o test 编译它。这将产生:

test.fpp:4:2: warning: #warning "This messages tells you Sys is NOT defined" [-Wcpp]
 #warning "This messages tells you Sys is NOT defined"
  ^

并且做./test 给出输出

For clarity we will now print defined if Sys is defined or not defined if Sys is not defined
Not defined

如果我们现在使用gfortran -ffree-form test.fpp -o test -DSys 编译,我们会看到消息

test.fpp:2:2: warning: #warning "This messages tells you Sys is defined" [-Wcpp]
 #warning "This messages tells you Sys is defined"
  ^

运行./test 给了我们

For clarity we will now print defined if Sys is defined or not defined if Sys is not defined
Defined

【讨论】:

  • @d_1999 太棒了。我不知道我可以绕过 defs.h 文件并通过 -D 标志传递定义。这就是我一直在寻找的。我已向 FCM 人员发出查询,但这可能需要数周时间。根据我的经验,很多人不知道 FCM 是什么,以及制作的替代方法,尽管相似。但我被这个系统困住了,所以我寻求了一个快速的解决方法。鉴于这种情况,我认为这不是一个真正的 XY 问题——尽管我很感激你的启发 :-) 这就是为什么我试图把这个问题表述为不是问题而是另一种方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
相关资源
最近更新 更多