Search code examples
cm4

m4 does not parse local variables


I have a problem with an m4 macro. The macro is

define(BARRIER, `
#if defined USE_PTHREAD_BARRIERS
barrier_wait(&$1,$2,$3);
#elif defined (USE_CENTRALIZED_BARRIERS)
central_barrier(&$1,$2,$3);
#endif
')

and in my .C file i have

BARRIER(Global->start, P, MyNum) where Global->start and P are globals and MyNum a local variable.

But when I execute

m4 macrosfile.m4 sourcefile > outputfile

in the output file there is this:

#if defined USE_PTHREAD_BARRIERS
barrier_wait(&Global->start,P,);
#elif defined (USE_CENTRALIZED_BARRIERS)
central_barrier(&Global->start,P,);

Something wrong with my macro, or just a limitation?

Thanks in advance.


Solution

  • You could try changing:

    BARRIER(Global->start, P, MyNum)
    

    to:

    BARRIER(`Global->start', `P', `MyNum')
    

    As for me, I'd try and avoid m4 by using an X-Macro or something like it:

    #if defined USE_PTHREAD_BARRIERS
    #define BARRIER_X(x_, y_, z_) barrier_wait(&x_, y_, z_)
    #elif defined (USE_CENTRALIZED_BARRIERS)
    #define BARRIER_X(x_, y_, z_) central_barrier(&x_, y_, z_)
    #endif
    

    ...

    BARRIER_X(Global->start, P, MyNum);