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

后端 未结 2 1767
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-31 18:52

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?

2条回答
  •  攒了一身酷
    2021-01-31 19:10

    I'm not sure if this will do what you want, but if you're only interested in this to debug the occasional macro problem (so it's not something you need displayed in a message for each compile), the following might work for you. Use gcc's -E -dD option to dump #define directives along with preprocessing output. Then pipe that through grep to see only the lines you want:

    // test.c
    #include 
    #include 
    #define ADEFINE "23"
    #include 
    
    int main(int argc, char *argv[])
    {
    #undef ADEFINE
    #define ADEFINE 42
        return 0;
    }
    

    The command gcc -E -dD -c test.c | grep ADEFINE shows:

    #define ADEFINE "23"
    #undef ADEFINE
    #define ADEFINE 42
    

提交回复
热议问题