C preprocessor: expand macro in a #warning

前端 未结 4 1567
礼貌的吻别
礼貌的吻别 2020-12-12 20:39

I would like to print a macro value (expand the macro) in the #warning directive.

For example, for the code:

#define AAA 17
#warning AAA = ???
         


        
4条回答
  •  有刺的猬
    2020-12-12 21:19

    If you really want to emit a warning, the following will work, too. However, it depends on C99 being enabled (works with gcc 4.8.2 or later, not tested on earlier versions):

    #define N 77
    
    #define __STRINGIFY(TEXT) #TEXT
    #define __WARNING(TEXT) __STRINGIFY(GCC warning TEXT)
    #define WARNING(VALUE) __WARNING(__STRINGIFY(N = VALUE))
    
    #if N == 77
    _Pragma (WARNING(N))
    #endif
    

提交回复
热议问题