Tags

, ,

The modern Visual Studio has a powerful feature called “Go to Definition”, which allows navigating to the location where some symbol (function, class, etc.) is defined. It works for macros too. For example, if you put the cursor at _MAX_PATH symbol and press <F12>, then you will see the corresponding #define line of stdlib.h file where the value is defined.

However, Visual Studio does not always manage to perform the navigation. Sometimes it cannot determine the right definition and therefore displays a list of possibilities.

Or, if you try determining the value of _DEBUG symbol, you will get an error, since it is declared in Project Properties, not in sources.

In order to display the macro at compile-time, define these helper macros:

#define M(m) #m “: “ M2(m)
#define M2(m) #m

Then, to display some macro, enter a line like this:

#pragma message(M(SOMEMACRO))

Start the compilation and watch the Output window.

Examples:

#pragma message(M(WINVER)) // Shows a value like “WINVER: 0x0603”
#pragma message(M(_DEBUG)) // Shows “_DEBUG: 1” in Debug builds
#pragma message(M(EOF)) // Shows “EOF: (-1)”
#pragma message(M(WEOF)) // Shows “WEOF: (wint_t)(0xFFFF)”

You can also show the meaning of macros that have arguments. Just specify some parameters. For example, if you include Windows.h, then you can try this:

#pragma message(M(_T(“test”))) // In Unicode builds displays _T(“test”): L”test”
#pragma message(M(MAKEINTRESOURCE(1234))) // Shows “MAKEINTRESOURCE(1234): ((LPWSTR)((ULONG_PTR)((WORD)(1234))))”
#pragma message(M(max(a, b))) // shows “max(a, b): (((a) > (b)) ? (a) : (b))”