Search code examples
cgccmacrosc-preprocessorpragma

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


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?

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

causes:

warning: malformed ‘#pragma message’, ignored

Ideally I'd like to be able to deal with any value, including undefined.


Solution

  • 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 <boost/preprocessor/stringize.hpp>
    #define ADEFINE 23
    #pragma message ("ADEFINE=" BOOST_PP_STRINGIZE(ADEFINE))