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

[亡魂溺海] 提交于 2019-12-02 19:14:11

To display macros which aren't strings, stringify the macro:

#define STRINGIFY(s) XSTRINGIFY(s)
#define XSTRINGIFY(s) #s

#define ADEFINE 23
#pragma message ("ADEFINE=" STRINGIFY(ADEFINE))

If you have/want boost, you can use boost stringize to do it for you:

#include <boost/preprocessor/stringize.hpp>
#define ADEFINE 23
#pragma message ("ADEFINE=" BOOST_PP_STRINGIZE(ADEFINE))

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 <stdlib.h>
#include <stdio.h>
#define ADEFINE "23"
#include <string.h>

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!