Fortran:有没有办法有条件地使用模块? [英] Fortran: Is there a way to conditionally use modules?
问题描述
if(condition)
use modA
else
使用modB
结束
我正在使用GNU Fortran编译器。
是的,您必须进行某种预处理。最常见的是GNU Fortran中包含的C预处理器。
#if(condition)
使用modA
#else
使用modB
#endif
预处理程序不理解您的Fortran代码,它只是一个文本。它有自己的一套指令,它有自己的一组变量。
另一个常见的指令是 #ifdef
在条件中只能使用预处理器变量,而不是Fortran变量。这是 #if defined
的变体。有关更多 https://gcc.gnu.org/onlinedocs/cpp/请参阅手册Traditional-Mode.html (gfortran以传统模式运行预处理器)。
要使预处理器使用 -cpp
标志或在Unix中,您可以在文件后缀中使用大写字母 F
。
Assume I have two Fortran modules called modA and modB. Is there a way to use one or the other in a program based on a conditional statement? Does this require some type of preprocessing? For example, I want to be able to do something like the following code:
if (condition)
use modA
else
use modB
end
I am using the GNU Fortran compiler.
Yes, you must do some kind of preprocessing. The most common is the C preprocessor included in GNU Fortran.
#if (condition)
use modA
#else
use modB
#endif
The preprocessor does not understand your Fortran code, it is only a text for it. It has it's own set of directives and it's own set of variables. Only the preprocessor variables can be used in the condition, not your Fortran variables.
Another common directive is #ifdef
which is a variant of #if defined
. See the manual for more https://gcc.gnu.org/onlinedocs/cpp/Traditional-Mode.html (gfortran runs the preprocessor in the traditional mode).
To enable the preprocessor use the -cpp
flag or in Unix you can use capital F
in the file suffix.
这篇关于Fortran:有没有办法有条件地使用模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!