How do I show the value of a #define at compile time in gcc

后端 未结 2 1762
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-31 18:52

So far I\'ve got as far as:

#define ADEFINE \"23\"
#pragma message (\"ADEFINE\" ADEFINE)

Which works, but what if ADEFINE isn\'t a string?

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-31 19:25

    To display macros which aren't strings, stringify the macro:

    #define STRINGIFY(s) XSTRINGIFY(s)
    #define XSTRINGIFY(s) #s
    
    #define ADEFINE 23
    #pragma message ("ADEFINE=" STRINGIFY(ADEFINE))
    

    If you have/want boost, you can use boost stringize to do it for you:

    #include 
    #define ADEFINE 23
    #pragma message ("ADEFINE=" BOOST_PP_STRINGIZE(ADEFINE))
    

提交回复
热议问题