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

John Lawrence Aspden picture John Lawrence Aspden · Mar 14, 2012 · Viewed 14.2k times · Source

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.

Answer

rob05c picture rob05c · Mar 14, 2012

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))