Search code examples
visual-studiopreprocessorpragma

Why I get errors when I try to out a compiler defined macro using a pragma message?


I would like to know why the Visual C++ compiler gets me an warning/error if I use the following code:

#pragma message( "You have " _MSC_FULL_VER )

Here is what I get:

error C2220: warning treated as error - no 'object' file generated
warning C4081: expected ':'; found ')'

The problem reproduces for _MSC_FULL_VER or _MSV_VER but not if I try to use others like __FILE__ or __DATE__.

These macros are defined, they are documented on msdn


Solution

  • I think #pragma message needs C strings only. IIRC, _MSC_FULL_VER is a number, while __FILE__ and __DATE__ are C strings. Try this

    // BEWARE! Untested macro hackery ahead!
    #define STRINGIFY( L )       #L
    #define MAKESTRING( M, L )   M(L)
    #define STRINGIZE(X)         MAKESTRING( STRINGIFY, X )
    #pragma message( "You have " STRINGIZE(_MSC_FULL_VER) )
    

    (On a side note, this allows

    #define SHOWORIGIN            __FILE__ "(" STRINGIZE(__LINE__) "): "
    #pragma message( SHOWORIGIN "your message here" )
    

    which allows you to double-click on a message in VS's output pane and be taken to its file/line.)