Fortran: Is there a way to conditionally use modules?

Suppose I have two Fortran modules called modA and modB. Is there a way to use this or that program depending on the conditional statement? Is this some kind of preprocessing required? 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.

+5
source share
1 answer

Yes, you have to do some preprocessing. The most common is the C preprocessor, included with GNU Fortran.

 #if (condition) use modA #else use modB #endif 

The preprocessor does not understand your Fortran code, this is just the text for it. It has its own set of directives and its own set of variables. In the state, only preprocessor variables can be used, not 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 starts the preprocessor in traditional mode).

To enable the preprocessor, use the -cpp flag or on Unix, you can use capital F in the file suffix.

+6
source

All Articles