How to define function-like macro?

Bernd Petrovitsch bernd at petrovitsch.priv.at
Tue Nov 26 07:50:31 EST 2013


On Die, 2013-11-26 at 10:50 +0800, 乃宏周 wrote:
> I read manual already:
> 
> -D name=definition The contents of definition are tokenized and processed
> as if they appeared during translation phase three in a ‘#define’
> directive. In particular, the definition will be truncated by embedded
> newline characters.
> 
> If you are invoking the preprocessor from a shell or shell-like program you
> may need to use the shell's quoting syntax to protect characters such as
> spaces that have a meaning in the shell syntax.
> 
> If you wish to define a function-like macro on the command line, write its
> argument list with surrounding parentheses before the equals sign (if any).
> Parentheses are meaningful to most shells, so you will need to quote the
> option. With sh and csh, -D'name(args...)=definition' works.
> I wrote this in my makefile:
> 
> *def_dpf = 'dpf(a...)=printk(KERN_ALERT a)'*

Variadic macros needs something slightly different on the right side -
see also http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html (if we
assume gcc).

Send the gcc invocation or at least a real snippet of the real Makefile
so that no one has to guess what's really there (and where).
At least - following the above "manual": -D'dpf(...)=printk(KERN_ALERT
__VA_ARGS__)'
Though I write such macros more like
-D'dpf(fmt, ...)=printk(KERN_ALERT fmt, __VA_ARGS__)'
if only for clarity.

> My code uses `dpf` macro like this:
> 
> *dpf("current value=%d\n",var);*
>
> When I compiling my program, I got this error:
> 
> *error: ‘Da’ undeclared (first use in this function)*

No idea where the 'Da' could come from. Which leads to the conclusion
that at least comething is missing ....

> Even manual says *sh* and *csh* can works with that definition,

It' just about the quoting and pretty any shell should work with
single-quotes.
If not, you need to fix the Makefile - you can set the shell to be used
there.

> *bash*should support that machanism, I guess. How should I do? Any
> suggestion?

And what has that to do with any/the shell?

Kind regards,
	Bernd
-- 
Bernd Petrovitsch                  Email : bernd at petrovitsch.priv.at
                     LUGA : http://www.luga.at




More information about the Kernelnewbies mailing list