Conditional compilation in gfortran

浪子不回头ぞ 提交于 2019-11-28 01:50:29

问题


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

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

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.


回答1:


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

As required.

There are more examples on this page with actual code.

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

gfortran -DCASE1=3 ...

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.



来源:https://stackoverflow.com/questions/41233923/conditional-compilation-in-gfortran

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!