相关疑难解决方法(0)

#define宏用于C中的调试打印?

尝试创建一个宏,可以在定义DEBUG时用于打印调试消息,如下面的伪代码:

#define DEBUG 1
#define debug_print(args ...) if (DEBUG) fprintf(stderr, args)
Run Code Online (Sandbox Code Playgroud)

如何用宏实现这一目标?

c c-preprocessor

196
推荐指数
7
解决办法
18万
查看次数

C/C++如何在引号之间将宏参数扩展为文本

这就是我所拥有的(message()是来自第三方库的专用日志记录功能):

#define LOG(fmt, ...) message("%s %s(): #fmt", __FILE__, __func__, __VA_ARGS__);
Run Code Online (Sandbox Code Playgroud)

所以我希望能够做到这样的事情:

LOG("Hello world")
LOG("Count = %d", count)
Run Code Online (Sandbox Code Playgroud)

并将它扩展到:

message("%s %s(): Hello world", __FILE__, __func__);
message("%s %s(): Count = %d", __FILE__, __func__, count);
Run Code Online (Sandbox Code Playgroud)

但#fmt的东西不起作用.它不会评估宏参数并打印为"#fmt".有可能做我想做的事吗?

c c++ macros

3
推荐指数
1
解决办法
1090
查看次数

标签 统计

c ×2

c++ ×1

c-preprocessor ×1

macros ×1