How do I show the value of a #define at compile-time?

I am trying to figure out what version of Boost my code thinks it's using. I want to do something like this:


but the preprocessor does not expand BOOST_VERSION.

I know I could print it out at run-time from the program, and I know I could look at the output of the preprocessor to find the answer. I feel like having a way of doing this during compilation could be useful.

13.10.2009 18:26:18
For future visitors... Chris Barry provides the generalized solution at the end (devoid of Boost specific stuff).
jww 4.09.2015 01:41:42

I know that this is a long time after the original query, but this may still be useful.

This can be done in GCC using the stringify operator "#", but it requires two stages.

#define XSTR(x) STR(x)
#define STR(x) #x

The value of a macro can then be displayed with:

#pragma message "The value of ABC: " XSTR(ABC)

See: 3.4 Stringification in the gcc online documentation.

How it works:

The preprocessor understands quoted strings and handles them differently from normal text. String concatenation is an example of this special treatment. The message pragma requires an argument that is a quoted string. When there is more than one component to the argument then they must all be strings so that string concatenation can be applied. The preprocessor can never assume that an unquoted string should be treated as if it were quoted. If it did then:

#define ABC 123
int n = ABC;

would not compile.

Now consider:

#define ABC abc
#pragma message "The value of ABC is: " ABC

which is equivalent to

#pragma message "The value of ABC is: " abc

This causes a preprocessor warning because abc (unquoted) cannot be concatenated with the preceding string.

Now consider the preprocessor stringize (Which was once called stringification, the links in the documentation have been changed to reflect the revised terminology. (Both terms, incidentally, are equally detestable. The correct term is, of course, stringifaction. Be ready to update your links.)) operator. This acts only on the arguments of a macro and replaces the unexpanded argument with the argument enclosed in double quotes. Thus:

#define STR(x) #x
char *s1 = "abc";
char *s2 = STR(abc);

will assign identical values to s1 and s2. If you run gcc -E you can see this in the output. Perhaps STR would be better named something like ENQUOTE.

This solves the problem of putting quotes around an unquoted item, the problem now is that, if the argument is a macro, the macro will not be expanded. This is why the second macro is needed. XSTR expands its argument, then calls STR to put the expanded value into quotes.

29.08.2019 13:33:21
I'm curious as to why it requires two stages
Vincent Fourmond 22.01.2015 13:34:46
@VincentFourmond Without the XSTR stage, the macro isn't expanded. So if you did #define ABC 42 \n STR(ABC) you'd get "ABC". See
rob05c 12.02.2015 22:22:17
This also works great with Xcode 8, e.g. replacing ABC with __IPHONE_9_3.
funroll 11.10.2016 19:50:34
GCC terminology seems to have changed, and with it the URL, which is now
Chris Barry 29.08.2019 11:14:37

You could write a program that prints out BOOST_VERSION and compile and run it as part of your build system. Otherwise, I think you're out of luck.

13.10.2009 18:30:33
For the case of a software version defined in a header you're probably safe (and it's a good answer). But as a general solution, a possible downside would be in getting your test app and your real app to have the same value of the #define - depending on their include paths, other #defines that may be used to set the value of that one, the CFLAGS passed to the compiler, etc.
KeyserSoze 13.10.2009 18:57:02
Print it out from your real program. If graphical, put it in the "about" dialog. If command-line, make it an option (part of --version, maybe). If a daemon, write it to a log file. If embedded, find some other way.
divegeek 20.10.2009 15:29:47
@swillden - The OP wanted it at compile time, not at runtime.
Chris Lutz 20.10.2009 22:26:50
This also tends to break cross-compiler based builds
Craig Ringer 21.12.2015 03:24:12

As far as I know '#error' only will print strings, in fact you don't even need to use quotes.

Have you tried writing various purposefully incorrect code using "BOOST_VERSION"? Perhaps something like "blah[BOOST_VERSION] = foo;" will tell you something like "string literal 1.2.1 cannot be used as an array address". It won't be a pretty error message, but at least it'll show you the relevant value. You can play around until you find a compile error that does tell you the value.

13.10.2009 19:37:38
That didn't work, since BOOST_VERSION is an integer, but I got to see it with this statement: std::vector<BOOST_VERSION>; in gcc 4.4.1. Thanks!
Jim Hunziker 13.10.2009 19:38:36
Note that with Visual C++, you would have to use Bojan Resnik's answer.
Raphaël Saint-Pierre 13.10.2009 19:48:52
I tried to get this to work, but the error message GCC gave me were sadly undescriptive. But +1 for mentioning it.
Chris Lutz 13.10.2009 23:40:44

Are you looking for

#if BOOST_VERSION != "1.2"
#error "Bad version"

Not great if BOOST_VERSION is a string, like I've assumed, but there may also be individual integers defined for the major, minor and revision numbers.

13.10.2009 18:56:32
I think the submitter doesn't want to (just) enforce a particular value, they want to see what the current value is.
KeyserSoze 13.10.2009 19:00:59
This is the only thing that works for me. I can change the #if VARIABLE == 123 statement on the fly and the syntax highlighting tells me whether it's the value I think it is or not...
endolith 9.11.2018 21:04:24

You could also preprocess the source file and see what the preprocessor value evaluates to.

13.10.2009 18:58:53

Looking at the output of the preprocessor is the closest thing to the answer you ask for.

I know you've excluded that (and other ways), but I'm not sure why. You have a specific enough problem to solve, but you have not explained why any of the "normal" methods don't work well for you.

13.10.2009 19:02:18
This is probably the correct answer to the general problem.
jww 28.03.2016 05:54:36

BOOST_VERSION is defined in the boost header file version.hpp.

13.10.2009 19:04:03

If you are using Visual C++, you can use #pragma message:

#include <boost/preprocessor/stringize.hpp>

Edit: Thanks to LB for link

Apparently, the GCC equivalent is (not tested):

20.10.2009 15:22:14
That's called diagnostic pragmas,…
LB40 20.10.2009 15:05:03
Would be nice if you included the definition of BOOST_PP_STRINGIZE which is nice and short and copy/pasteable.
Timmmm 3.02.2017 09:46:50
Works fine under gcc :)
Thomas Legris 5.04.2017 09:10:12

Take a look at the Boost documentation as well, regarding how you are using the macro:

In reference to BOOST_VERSION, from

Describes the boost version number in XXYYZZ format such that: (BOOST_VERSION % 100) is the sub-minor version, ((BOOST_VERSION / 100) % 1000) is the minor version, and (BOOST_VERSION / 100000) is the major version.

13.10.2009 19:43:36

BOOST_PP_STRINGIZE seems a excellent solution for C++, but not for regular C.

Here is my solution for GNU CPP:

/* Some test definition here */
#define DEFINED_INT 3

/* definition to expand macro then apply to pragma message */
#define VALUE_TO_STRING(x) #x
#define VAR_NAME_VALUE(var) #var "="  VALUE(var)

/* Some example here */
#pragma message(VAR_NAME_VALUE(NOT_DEFINED))
#pragma message(VAR_NAME_VALUE(DEFINED_INT))
#pragma message(VAR_NAME_VALUE(DEFINED_STR))

Above definitions result in:

test.c:10:9: note: #pragma message: NOT_DEFINED=NOT_DEFINED
test.c:11:9: note: #pragma message: DEFINED_BUT_NO_VALUE=
test.c:12:9: note: #pragma message: DEFINED_INT=3
test.c:13:9: note: #pragma message: DEFINED_STR="ABC"

For "defined as interger", "defined as string", and "defined but no value" variables , they work just fine. Only for "not defined" variable, they displayed exactly the same as original variable name. You have to used to it -- or maybe someone can provide a better solution.

19.04.2012 11:29:58
excellent! Any experiences in ARM RVCT? it seems has no "Stringification" feature as GCC…
xdan 28.05.2013 07:07:48
Great solution. However, if I wish to display the size of a compile-time calculated value, e.g. the size of a complex struct, can this be done? The method suggested in this answer seems to generate DEFINED_INT=(sizeof(MY_STRUCT)), without the sizeof operator being evaluated.
Carl 16.03.2015 12:12:29
(Comment addition: not unexpected, since it is the compiler rather than the pre-processor that will evaluate sizeof, however, still curious if there's a clever way of achieving this.)
Carl 16.03.2015 12:21:49
@xdan Good solution, unfortunately is doesn't cater for things like #define masks {0xff, 0xaf, 0x0f}
Simon Bagley 20.08.2019 10:35:28
#define a <::BOOST_VERSION>
#include a
MSVC2015: fatal error C1083: Cannot open include file: '::106200': No such file or directory

Works even if preprocess to file is enabled, even if invalid tokens are present:

#define a <::'*/`#>
#include a
MSVC2015: fatal error C1083: Cannot open include file: '::'*/`#': No such file or directory
GCC4.x: warning: missing terminating ' character [-Winvalid-pp-token]
#define a <::'*/`#>
28.01.2018 18:20:10
Mine just says Build error: #include expects "FILENAME" or <FILENAME>. Sigh.
endolith 9.11.2018 21:02:03
@endolith what compiler and version?
Andry 12.11.2018 02:19:01
DP8051 Keil 9.51 :)
endolith 12.11.2018 04:27:32
@endolith Seems this compiler is very limited on preprocessing: But, on mine side it almost works as expected, i've just removed some of invalid characters like ': *** WARNING C318 IN LINE 2 OF test.c: can't open file '::*/`'
Andry 12.11.2018 10:25:01
Thank you, this saved me because the pragma message stuff wasn't implemented in the compiler I was using.
CodeMonkey 17.05.2019 08:08:40

Without boost :

  1. define same macro again and compiler HIMSELF will give warning.

  2. From warning you can see location of the previous definition.

  3. vi file of previous definition .

ambarish@axiom:~/cpp$ g++ shiftOper.cpp
shiftOper.cpp:7:1: warning: "LINUX_VERSION_CODE" redefined
shiftOper.cpp:6:1: warning: this is the location of the previous definition

#define LINUX_VERSION_CODE 265216

int main ()

5.03.2018 13:54:02
This one is easier and straightforward.
Tmx 8.11.2018 18:22:35
itself : compilers have no gender
Sky 14.11.2018 19:21:49
This does not work with predefined macros, such as __cplusplus.
ManuelAtWork 22.11.2019 09:55:20

In Microsoft C/C++, you can use the built-in _CRT_STRINGIZE() to print constants. Many of my stdafx.h files contain some combination of these:

#pragma message("_MSC_VER      is " _CRT_STRINGIZE(_MSC_VER))
#pragma message("_MFC_VER      is " _CRT_STRINGIZE(_MFC_VER))
#pragma message("_ATL_VER      is " _CRT_STRINGIZE(_ATL_VER))
#pragma message("WINVER        is " _CRT_STRINGIZE(WINVER))
#pragma message("_WIN32_WINNT  is " _CRT_STRINGIZE(_WIN32_WINNT))
#pragma message("_WIN32_IE     is " _CRT_STRINGIZE(_WIN32_IE))

and outputs something like this:

_MSC_VER      is 1915
_MFC_VER      is 0x0E00
_ATL_VER      is 0x0E00
WINVER        is 0x0600
_WIN32_WINNT  is 0x0600
_WIN32_IE     is 0x0700
NTDDI_VERSION is 0x06000000
26.11.2018 21:37:40