gcc下调试打印宏的串联问题
要完全禁用 c 源代码中的调试输出, 我通常定义以下简单宏 #1
#define dprintf(args)
为了启用调试输出,我定义宏 #2 或者
#define dprintf(args) printk##args
源代码中的用法如下所示:
dprintf(("Irqs:%lu\n",irqs));
如果我使用宏 #2,预处理器应创建以下行
printk("Irqs :%lu\n",irqs);
在Windows Visual c++下,没有问题。
在NETBEANS IDE 6.8下使用gcc版本4.4.1(Ubuntu 4.4.1-4ubuntu9), 我收到以下错误消息:
"printk" and "(" does not Give a valid preprocessing token
我在 Linux 下尝试了以下操作
#define dprintk(args...) printk(args)
这仅适用于 dprintf("Irqs:%lu\n",irqs);
Visual C++ 不知道 args...
我必须在 Windows 上编译源代码 和Linux(386)平台可选。
有人有主意吗?
To completely disable a debug output in c-source,
I usually define the following SIMPLE macro #1
#define dprintf(args)
To enable a debug output, I define macro #2 alternatively
#define dprintf(args) printk##args
The usage in source looks like:
dprintf(("Irqs:%lu\n",irqs));
A preprocessor should create following line if I use macro #2
printk("Irqs:%lu\n",irqs);
Under Windows Visual c++, there is no problem.
Using gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu9) under NETBEANS IDE 6.8,
I got the following error message:
"printk" and "(" does not give a valid preprocessing token
I tried the following under Linux
#define dprintk(args...) printk(args)
This works only withdprintf("Irqs:%lu\n",irqs);
Visual C++ however does not know args...
I have to compile source code on windows
and Linux(386) platform alternatively.
Does anyone have an idea ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不#define dprintf(args) print args?
可以添加双括号来替换 Visual C++ 中的可变参数宏:预处理器将处理宏调用,就好像只有一个参数一样。
Why not
#define dprintf(args) print args
?The double parenthesis could have been added to replace the variadic macro in visual C++ : the preprocessor will handle macro invocation as if there was only one parameter.
令牌粘贴运算符
##
只能用于连接令牌,正如其名称所暗示的那样。正如您现在所发现的,某些编译器(例如较新版本的 gcc)比其他编译器更严格地执行此操作。不过,正如 philippe 所说,在此特定示例中您实际上并不需要##
。The token pasting operator
##
can only be used to concatenate tokens, as its name implies. Some compilers, e.g. newer versions of gcc, enforce this more rigidly than others, as you have now discovered. As philippe says, though, you don't actually need##
in this particular example.