Re: ifdef usage

Eric Blake <[email protected]>
Newsgroups gmane.comp.gnu.m4.general
Message-ID <[email protected]>
Mehul Sanghvi <mehul.sanghvi <at> gmail.com> writes:

>    In case this is the wrong place for sending this question, please
> point me in the correct direction.

This is the correct place.

>    testing.m4
>    ========
>    define('VNC')

Incorrect quotes.  By default, m4 uses "`" and "'", not "'" and "'".  
Therefore, you ended up defining a macro literally named "'VNC'", rather than 
the intended "VNC".

>    ifdef('VNC', 'vnc is DEFINED', 'vnc is NOT DEFINED')

M4 performs macro expansion during argument collection.  Since you aren't using 
proper quote characters, this means that you effectively called:

ifdef(<expansion of 'VNC'>, ..., ...)

and since the expansion of "'VNC'" was defined as the empty string, you are 
testing whether the empty string is currently defined as a macro name.  It 
isn't, hence your surprise.

> 
> If I comment out the define(), and then do the following:
> 
>    bash% m4 -DVNC testing.m4
>    #define('VNC')
>    'vnc is NOT DEFINED'

Your command-line usage defined the macro "VNC", as you wanted.  Now 
that "'VNC'" is not a macro, your ifdef usage is checking whether "'VNC'" is 
defined, which it is not, still explaining your surprise.

> 
> Am I missing something or have I misunderstood what the manual says
> about how to use ifdef and -D ?

You misunderstood the portion about quoting characters.  Let us know if there 
is any way to improve the manual to make it more obvious that they are 
intentionally distinct strings.

-- 
Eric Blake
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.