了解 C 中的宏和预处理器指令
understanding Macros in C and preprocessor directives
我不明白那些宏,它是如何工作的?还有 irc_##name 是什么?..我从 insobot IRC 机器人那里得到了这段代码,这是代码 https://github.com/baines/insobot/blob/master/src/insobot.c
#define IRC_CALLBACK_BASE(name, event_type) static void irc_##name ( \
irc_session_t* session, \
event_type event, \
const char* origin, \
const char** params, \
unsigned int count \
)
#define IRC_STR_CALLBACK(name) IRC_CALLBACK_BASE(name, const char*)
#define IRC_NUM_CALLBACK(name) IRC_CALLBACK_BASE(name, unsigned int)
##
是标记连接运算符:它在这些宏定义中用于通过在第一个参数的值之前添加 irc_
来创建回调函数名称的标识符宏 IRC_STR_CALLBACK
and/or IRC_NUM_CALLBACK
查看第 183 行的宏调用:
IRC_STR_CALLBACK(on_join);
此源代码行扩展为
static void irc_on_join ( irc_session_t* session, event_type event, const char* origin, const char** params, unsigned int count );
宏用于以一致的方式声明处理程序,而无需显式编写原型,这很方便,因为此源文件中有很多处理程序。
我不明白那些宏,它是如何工作的?还有 irc_##name 是什么?..我从 insobot IRC 机器人那里得到了这段代码,这是代码 https://github.com/baines/insobot/blob/master/src/insobot.c
#define IRC_CALLBACK_BASE(name, event_type) static void irc_##name ( \
irc_session_t* session, \
event_type event, \
const char* origin, \
const char** params, \
unsigned int count \
)
#define IRC_STR_CALLBACK(name) IRC_CALLBACK_BASE(name, const char*)
#define IRC_NUM_CALLBACK(name) IRC_CALLBACK_BASE(name, unsigned int)
##
是标记连接运算符:它在这些宏定义中用于通过在第一个参数的值之前添加 irc_
来创建回调函数名称的标识符宏 IRC_STR_CALLBACK
and/or IRC_NUM_CALLBACK
查看第 183 行的宏调用:
IRC_STR_CALLBACK(on_join);
此源代码行扩展为
static void irc_on_join ( irc_session_t* session, event_type event, const char* origin, const char** params, unsigned int count );
宏用于以一致的方式声明处理程序,而无需显式编写原型,这很方便,因为此源文件中有很多处理程序。