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

 include/linux/stringify.h :

 

  1. #ifndef __LINUX_STRINGIFY_H  
  2. #define __LINUX_STRINGIFY_H  
  3.   
  4. /* Indirect stringification.  Doing two levels allows the parameter to be a  
  5.  * macro itself.  For example, compile with -DFOO=bar, __stringify(FOO)  
  6.  * converts to "bar".  
  7.  */  
  8.   
  9. #define __stringify_1(x...) #x  
  10. #define __stringify(x...)   __stringify_1(x)  
  11.   
  12. #endif  /* !__LINUX_STRINGIFY_H */  


看上述源代码,

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

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

 

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


 

假设我们这样使用  __ATTR:   

                       __ATTR(var_name, 777,  show_function, store_function)

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

posted @ 2012-11-28 20:52  云翔世界  阅读(575)  评论(0)    收藏  举报