37

Is there an option that the GCC preprocessor could generate C source code and filter out irrelevant source code?

For example, a .c file has a #define switch to define for many different platforms. I'm only interested in one platform, and I want the C preprocessor to filter out unrelated code.

Does GCC support this?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Richard Luo
  • 371
  • 1
  • 3
  • 3

3 Answers3

37

Yes. Use the -E option:

gcc -E foo.c
JesperE
  • 61,479
  • 20
  • 135
  • 194
18

While the -E option will perform all pre-processing, it also produces some very 'raw' output that might not be what you want (depending on what you want).

If you need to debug a macro expansion that's not doing what you expect, E is a good way to go. If you simply want to filter out the 'inactive code', but leave the remaining code in more-or-less original form, you might want to look at the answers to the following Stack Overflow question:

Community
  • 1
  • 1
Michael Burr
  • 321,763
  • 49
  • 514
  • 739
5

It sounds like you want unifdef, not the GCC preprocessor.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Christoffer
  • 12,446
  • 7
  • 35
  • 52