Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




8.1.3 Quotation and Nested Macros

The examples below use the following macros:

     define([car], [$1])
     define([active], [ACT, IVE])
     define([array], [int tab[10]])

Each additional embedded macro call introduces other possible interesting quotations:

     =>ACT, IVE

In the first case, the top level looks for the arguments of car, and finds ‘active’. Because M4 evaluates its arguments before applying the macro, ‘active’ is expanded, which results in:

     car(ACT, IVE)

In the second case, the top level gives ‘active’ as first and only argument of car, which results in:

     =>ACT, IVE

i.e., the argument is evaluated after the macro that invokes it. In the third case, car receives ‘[active]’, which results in:


exactly as we already saw above.

The example above, applied to a more realistic example, gives:

     car(int tab[10];)
     =>int tab10;
     car([int tab[10];])
     =>int tab10;
     car([[int tab[10];]])
     =>int tab[10];

Huh? The first case is easily understood, but why is the second wrong, and the third right? To understand that, you must know that after M4 expands a macro, the resulting text is immediately subjected to macro expansion and quote removal. This means that the quote removal occurs twice—first before the argument is passed to the car macro, and second after the car macro expands to the first argument.

As the author of the Autoconf macro car, you then consider it to be incorrect that your users have to double-quote the arguments of car, so you “fix” your macro. Let's call it qar for quoted car:

     define([qar], [[$1]])

and check that qar is properly fixed:

     qar([int tab[10];])
     =>int tab[10];

Ahhh! That's much better.

But note what you've done: now that the arguments are literal strings, if the user wants to use the results of expansions as arguments, she has to use an unquoted macro call:


where she wanted to reproduce what she used to do with car:

     =>ACT, IVE

Worse yet: she wants to use a macro that produces a set of cpp macros:

     define([my_includes], [#include <stdio.h>])
     =>#include <stdio.h>
     error-->EOF in argument list

This macro, qar, because it double quotes its arguments, forces its users to leave their macro calls unquoted, which is dangerous. Commas and other active symbols are interpreted by M4 before they are given to the macro, often not in the way the users expect. Also, because qar behaves differently from the other macros, it's an exception that should be avoided in Autoconf.

  Published under the terms of the GNU General Public License Design by Interspire