1

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Bergwolf
  • 55
  • 7
  • Use -D option such as -Dmacro=[defn] ..do man gcc and search this preprocessor Options – Omkant Oct 30 '12 at 06:28
  • @Omkant I have hundreds of macros defined in bar.h. So -D is not an option. Plus what I want is more like text processing, and gcc -D is going to compile the source code. – Bergwolf Oct 30 '12 at 06:35

4 Answers4

1

You might look into the program sunifdef which is mentioned in the answer to this question. It allows you specify macro definitions on the command line, and it eliminates #ifdef lines 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.

Community
  • 1
  • 1
Dale Hagglund
  • 16,074
  • 4
  • 30
  • 37
  • Downloaded sunidef and its successor [coan] (http://coan2.sourceforge.net/index.php). Both are working perfectly for me. Thanks very much! – Bergwolf Oct 30 '12 at 07:50
1

Unfortunately for you the proeprocessos is all-or-nothing. Either you run it and it includes all requested files and expands all macros.

However you can kind of work around it by using the conditional compilation features of the preprocessor:

#ifndef INCLUDE_ONLY_BAR
# include <stdio.h>
#endif
#include "bar.h"

int main()
{
#ifdef BAR_FUNC
    printf("bar func\n");
#else
    printf("foo func\n");
#endif
    return 0;
}

You can "compile" it as such:

$ gcc -E -DINCLUDE_ONLY_BAR foo.c
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

You can use an IDE like eclipse. It disables the part of the code which is not compiled. You might not be able to see 'exactly' what you want to see, but you can see a clear difference between disabled code and normal code. Please ignore this option if you don't have luxury of using GUI.

Eclipse also helps in expanding macros which can be very handy when you have complex macros.

CCoder
  • 2,305
  • 19
  • 41
  • @grhedge, thanks for recommending eclipse... but as you guessed, I can't use GUI and I want to do real text processing rather than just displaying the expanded macros in GUI w/o changing the code. – Bergwolf Oct 30 '12 at 06:45
0

Try:
Normally compile: gcc -include stdio.h -imacros bar.h foo.c
Compile to expand ONLY MACROS in bar.h: gcc -E -imacros bar.h foo.c

anishsane
  • 20,270
  • 5
  • 40
  • 73