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], [ and ])dnl
It is worth highlighting again the liberal use of quoting. We wish to define a pair of macros whose names are literally
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
define , giving unexpected results.
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.
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 undefine([foo])dnl
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
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
defn combination shown above to rename all of the macros, one by one. This is how Autoconf makes the distinction clear.