1

I use m4 in my fortran code to generate specific code based on flags defined in my parameters file.

I know very little pre-processing and hence don't know M4 very well. I am trying to write code where I need to define things based on three cases: isothermal, barotropic, neither. I wrote the following code:

#ifdef isothermal
    do something (1)
#elif barotropic
    do something (2)
#else
    do something (3)
#endif

Now when I compile the code, it compiles fine with isothermal [do something (1)] and (without isothermal and barotropic defined) [do something (3)]. But when I define barotropic, it falls back to [do something (3)] instead of [do something (2)].

Any pointers on how to deal with such a situation in m4?

Thanks!

toylas
  • 437
  • 2
  • 6
  • 19

3 Answers3

2

Spurred by a downvote and comment, I see my understanding of the question was flawed. So I downloaded m4 and reworked my answer. A nested ifdef() seems to do the trick:

ifdef(`isothermal',do something (1),ifdef(`barotropic',do something (2),do something (3)))

Saving this in a file triplecond.f and processing with m4:

$ m4 triplecond.f 
do something (3)

$ m4 -Disothermal triplecond.f 
do something (1)

$ m4 -Dbarotropic triplecond.f 
do something (2)

$ 
Digital Trauma
  • 15,475
  • 3
  • 51
  • 83
0

m4 is NOT cpp!

#ifdef is a C preprocessor feature. ifdef(name, string, optional string) is the m4 version.

Demi
  • 3,535
  • 5
  • 29
  • 45
0

I was also looking for this, and ended up writing my own. Here is a link to the code on my wiki:

http://www.eugeneweb.com/wiki/Sites/M4Macros

I defined the names without the #'s eg. IF, ELSE, ENDIF, etc... Also M4 requires parens to hold the arguments where CPP (mostly) dose not. I also added a selective invoke to protect macros with side effects from invocation in an unselected block.

Mr Clif
  • 26
  • 2