Back: Discarding input
Forward: Conditionals
FastBack: Conditionals
Up: Features of M4
FastForward: Writing macros within the GNU Autotools framework
Top: Autoconf, Automake, and Libtool
Contents: Table of Contents
Index: Index
About: About this document

21.4.2 Macro management

A number of built-in macros exist in M4 to manage macros. We shall examine the most common ones that you're likely to encounter. There are others and you should consult the GNU M4 manual for further information.

The most obvious one is define, which defines a macro. It expands to the empty string:

define([foo], [bar])dnl
define([combine], [$1 and $2])dnl

It is worth highlighting again the liberal use of quoting. We wish to define a pair of macros whose names are literally foo and combine. If another macro had been previously defined with either of these names, m4 would have expanded the macro immediately and passed the expansion of foo to define, giving unexpected results.

The undefine macro will remove a macro's definition from M4's macro table. It also expands to the empty string:


Recall that once removed from the macro table, unmatched text will once more be passed through to the output.

The defn macro expands to the definition of a macro, named by the single argument to defn. It is quoted, so that it can be used as the body of a new, renamed macro:

define([newbie], defn([foo]))dnl

The ifdef macro can be used to determine if a macro name has an existing definition. If it does exist, ifdef expands to the second argument, otherwise it expands to the third:

ifdef([foo], [yes], [no])dnl

Again, yes and no have been quoted to prevent expansion due to any pre-existing macros with those names. Always consider this a real possibility!

Finally, a word about built-in macros: these macros are all defined for you when m4 is started. One common problem with these macros is that they are not in any kind of name space, so it's easier to accidentally invoke them or want to define a macro with an existing name. One solution is to use the define and defn combination shown above to rename all of the macros, one by one. This is how Autoconf makes the distinction clear.

This document was generated by Gary V. Vaughan on February, 8 2006 using texi2html