I have a source file foo.c and a header file bar.h. How can I just expand the macros in bar.h without expanding macros in other header files?
$ cat foo.c
#include <stdio.h>
#include "bar.h"
int main()
{
#ifdef BAR_FUNC
printf("bar func\n");
#else
printf("foo func\n");
#endif
return 0;
}
$ cat bar.h
#define BAR_FUNC 1
What I want is:
$ EXPAND_MAGIC foo.c
#include <stdio.h>
int main()
{
printf("bar func\n");
return 0;
}
If I use `gcc -E, it expands <stdio.h> as well. But I just want to expand macros in bar.h. Is there an option in gcc doing that? If not, are there any other tools that can do such preprocessing?
Update: Above foo.c/bar.h is just an example. In reality, I have a few hundreds of macros defined in bar.h (pls consider config.h generated by autoconf in a fairly large project). And what I want is to expand all (and ONLY) these macros in more than 10K source files. Any suggestions?
You might look into the program
sunifdefwhich is mentioned in the answer to this question. It allows you specify macro definitions on the command line, and it eliminates#ifdeflines appropriately. Note that it doesn’t do macro expansion.That said, you shouldn’t use a tool like this for general development, but it’s quite useful for decoding or cleaning up a code that has gotten messy with many unused
#ifdefs‘s over the years.