Is there a way to have the C Preprocessor resolve macros in an #error statement?

后端 未结 3 1302
甜味超标
甜味超标 2021-02-20 04:57

Just as the title says. I want to use a preprocessor macro in the text of an #error statement:

#define SOME_MACRO 1

#if SOME_MACRO != 0
    #error \"SOME_MACRO          


        
3条回答
  •  眼角桃花
    2021-02-20 05:52

    In Visual Studio you can use pragmamessage as follows:

    #define STRING2(x) #x
    #define STRING(x) STRING2(x)
    
    #define SOME_MACRO 1
    
    #if SOME_MACRO != 0
        #pragma message ( "SOME_MACRO was not 0; it was " STRING(SOME_MACRO) )
        #error SOME_MACRO was not 0;
    #endif
    

    This will generate two messages, but you'll get the value of SOME_MACRO. In G++ use the following instead (from comments: g++ version 4.3.4 works well with parenthesis as in the code above):

    #pragma message "SOME_MACRO was not 0; it was " STRING(SOME_MACRO)
    

提交回复
热议问题