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

查看:29
本文介绍了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 命令行选项到您的命令行.该选项默认不启用(根据下面的 Vladimir F 评论),尽管它看起来像是使用 .F90 文件扩展名(即大写的 F,而不是 >.f90) 无需 -cpp 选项即可实现.

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), although it looks like using the .F90 filename extension (i.e. capital F, instead of .f90) will do the trick without the -cpp 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
#elif defined(<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.如果不指定值,则 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天全站免登陆