gfortran中的条件编译 [英] Conditional compilation in gfortran

查看:165
本文介绍了gfortran中的条件编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以选择Fortran 95例程的不同部分进行编译。

I want to know if it is possible to select different parts of my Fortran 95 routine to compile.

例如,如果我将某些标志传递给gfortran,则编译器选择将哪个部分用于特定功能。我知道我可以在例程中使用 if 来做到这一点,但是缺点是我不希望程序运行 if 这一个

For example, if I pass certain flag to gfortran, then the compiler chooses which section to use for a certain function. I know I can do it using if inside the routine, but the drawback is that I don't want the program to run the if all the time due to speed concerns. I suppose solution should be similar to this one

我专门使用一个程序来计算多体系统(例如,一百万个系统)中的能量。然后,我不想每次在编译时需要使用不同的能量定义时都放置 if

I am working specifically with a program that calculates energies in a many-body system (say, a million). Then I don't want to put an if each time that I need to use a different energy definition at compilation time.

我希望这是可能的,而且我的问题很清楚。

I hope this is possible and that my question is clear.

推荐答案

您可以使用类似于C的预处理器。 -cpp 命令行选项。默认情况下未启用该选项。 (根据以下弗拉基米尔F的评论)

You can use the C like preprocessor. The -cpp command line option to your command line. That option is not turned on by default. (As per Vladimir F comment below)

有关该选项的详细信息:

Details about the option:

https://gcc.gnu.org/onlinedocs/gfortran/Preprocessing-Options。 html

然后,您可以按照所指出的进行操作,因此:

Then you can do the same as you pointed out, so the:

#ifdef <some-var>
  code when <some-var> is true
#elsif <other-var>
  code when <other-var> is true
#endif

根据需要。

此页面上有更多示例带有实际代码。

此外,与C / C ++一样,您可以在命令行上使用 -D定义宏选项:

Also, like with C/C++, you can define macros on your command line with the-D option:

gfortran -DCASE1=3 ...

这将定义值为值 3 CASE1 c>。如果未指定该值,则 1 将自动分配给该宏。记录在同一页面上。

This will define CASE1 with the value 3. If you do not specify the value, then 1 is automatically assigned to the macro. This is documented on the same page.

这篇关于gfortran中的条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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