c/c++ # and ## in macros以及宏debug
可变参数宏
1999年的ISO C标准里规定了可变参数宏,语法和函数类似,比如:
#define debug(format, ...) fprintf (stderr, format, __VA_ARGS__)
其中的"…"表示可变参数,实际调用时,它们会替代宏体里的__VA_ARGS__。GCC支持更复杂的形式,可以给可变参数取个名字,如下所示。
#define debug(format, args...) fprintf (stderr, format, args)
有了名字之后,代码显得更具有可读性。内核中的例子为:
++++ include/linux/kernel.h
244 #define pr_info(fmt,arg...) \
245 printk(KERN_INFO fmt,##arg)
其中的pr_info和上面的debug形式除了"##"外,几近相同。"##"主要针对参数为空的情况。既然称为可变参数,那传递空参数也是可以的。如果没有使用"##",传递空参数时,比如:
debug ("A message");
宏展开后,其中的字符串后面会多个多余的逗号,而"##"则会使预处理器去掉这个多余的逗号。
扩展宏定义中, "#" 和 "##"的含义。如下:
https://stackoverflow.com/questions/4364971/and-in-macros
https://stackoverflow.com/questions/1644868/define-macro-for-debug-printing-in-c
#define INT64CONST(x) (x##L)
#define UINT64CONST(x) (x##UL)
#define LOCAL_FCINFO(name, nargs) \
/* use union with FunctionCallInfoBaseData to guarantee alignment */ \
union \
{ \
FunctionCallInfoBaseData fcinfo; \
/* ensure enough space for nargs args is available */ \
char fcinfo_data[SizeForFunctionCallInfo(nargs)]; \
} name##data; \
FunctionCallInfo name = &name##data.fcinfo
#define forfour(cell1, list1, cell2, list2, cell3, list3, cell4, list4) \
for (ForFourState cell1##__state = {(list1), (list2), (list3), (list4), 0}; \
multi_for_advance_cell(cell1, cell1##__state, l1, i), \
multi_for_advance_cell(cell2, cell1##__state, l2, i), \
multi_for_advance_cell(cell3, cell1##__state, l3, i), \
multi_for_advance_cell(cell4, cell1##__state, l4, i), \
(cell1 != NULL && cell2 != NULL && cell3 != NULL && cell4 != NULL); \
cell1##__state.i++)
#define makeNode(_type_) ((_type_ *) newNode(sizeof(_type_),T_##_type_))
"#" 代表和一个字符串相连接
When a macro parameter is used with a leading ‘#’, the preprocessor replaces it with the literal text of the actual argument, converted to a string constant.
"##" 代表和一个符号相连接,符号可以是变量,或另一个宏符号。
就相当于如下:
for (ForFourState lct__state = {(list1), (list2), (list3), (list4), 0}; multi_for_advance_cell(lct, lct__state, l1, i), \ multi_for_advance_cell(cell2, lct__state, l2, i), \ multi_for_advance_cell(cell3, lct__state, l3, i), \ multi_for_advance_cell(cell4, lct__state, l4, i), \ (cell1 != NULL && cell2 != NULL && cell3 != NULL && cell4 != NULL); lct__state.i++)
#define multi_for_advance_cell(cell, state, l, i) \
(cell = (state.l != NIL && state.i < state.l->length) ? \
&state.l->elements[state.i] : NULL)
宏debug
gcc本身是支持宏debug的,机制类似内联函数。
gcc -gdwarf-2 -g3 test.c
-g3级别支持debuginfo中包含可被gdb识别的宏信息,参见https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/pdf/developer_guide/red_hat_enterprise_linux-7-developer_guide-en-us.pdf。
https://developer.apple.com/library/archive/documentation/DeveloperTools/gdb/gdb/gdb_10.html
展开宏定义:
(gdb) macro exp MACRO3(0)
expands to: (((++(0))+100)+200)
打印宏定义:
(gdb) p CLIENT6_BEGIN_FLG
$1 = "<Client6>"
C和C++编译器是集成的,编译一般分为四个步骤:
- 预处理(preprocessing) ----------------- cpp/ gcc -E
- 编译(compilation) ------------------ cc1 / gcc -S
- 汇编(assembly) -------------------- as
- 连接(linking) --------------------- ld
但是通过clion没有debug进去,应该是没有专门到-E阶段,vs的优势是支持debug到宏内。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2018-12-04 ORA-12801/ORA-12853: insufficient memory for PX buffers: current 274880K, max needed 19722240K/ORA-04031解决方法
2018-12-04 关于oracle result_cache
2016-12-04 2016年12月数据库流行度排行榜
2016-12-04 excel查看VBA代码快捷键