Contents
When does a variadic macro expand to nothing?
If the variable argument has any tokens, then a __VA_OPT__ invocation expands to its argument; but if the variable argument does not have any tokens, the __VA_OPT__ expands to nothing: __VA_OPT__ is also available in GNU C and GNU C++.
How are the arguments to a macro expanded?
All arguments to a macro are completely macro-expanded before they are substituted into the macro body. After substitution, the complete text is scanned again for macros to expand, including the arguments. This rule may seem strange, but it is carefully designed so you need not worry about whether any function call is actually a macro invocation.
How does the stored expansion work in a macro?
Further, in the macro’s stored expansion each occurrence of a parameter is replaced with a special token of type CPP_MACRO_ARG. Each such token holds the index of the parameter it represents in the parameter list, which allows rapid replacement of parameters with their arguments during expansion.
How to create a macro with an argument?
1 Macros with arguments To create a macro with arguments, put them in parentheses separated by commas after the macro name, e.g. 2 Multiple macros One macro can expand to another; for example, after defining Toggle line numbers 1 #define FOO BAR 2 #define BAR (12) 3 it will be the case 3 Macro tricks 3.1.
Can you have named arguments in a variadic macro?
You cannot use __VA_ARGS__ and this extension in the same macro. You can have named arguments as well as variable arguments in a variadic macro. We could define eprintf like this, instead: This formulation looks more descriptive, but historically it was less flexible: you had to supply at least one argument after the format string.
What do you mean by Macro in make?
A macro is just another way of defining a variable in make, and one that can contain embedded newlines! The GNU make manual seems to use the words variable and macro interchangeably. In this book, we’ll use the word macro specifically to mean variables defined using the define directive and variable only when assignment is used.
How are variables and macros used in GNU make?
Variables and macros get much more complicated and give GNU make much of its incredible power. Before we go any further, it is important to understand that make is sort of two languages in one. The first language describes dependency graphs consisting of targets and prerequisites. (This language was covered in Chapter 2 .)