zl程序教程

您现在的位置是:首页 >  系统

当前栏目

linux系统中的“ __stringify() ”宏定义 详解

Linux系统 详解 定义 __ stringify
2023-09-14 09:09:19 时间

 include/linux/stringify.h :

#ifndef __LINUX_STRINGIFY_H
#define __LINUX_STRINGIFY_H
 
/* Indirect stringification.  Doing two levels allows the parameter to be a
 * macro itself.  For example, compile with -DFOO=bar, __stringify(FOO)
 * converts to "bar".
 */
 
#define __stringify_1(x...) #x
#define __stringify(x...)   __stringify_1(x)
 
#endif  /* !__LINUX_STRINGIFY_H */

看上述源代码,

__stringify(x...) 这个宏的实际展开为: #x 

其作用实际上就是 把  x 直接转换为字符串。其返回值就是字符串,而不是变量名。

如:

#define __ATTR(_name,_mode,_show,_store) { /
     .attr = {.name = __stringify(_name), .mode = _mode }, /
     .show = _show,     /
     .store = _store,     /
    }

假设我们这样使用  __ATTR:   

                       __ATTR(var_name, 777,  show_function, store_function)

那么,实际上 复制给  .attr.name 的值是 "var_name" ,而不是var_name 变量所代表的值。