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,738
  • 21
  • 105
  • 131
Richard Luo
  • 371
  • 1
  • 3
  • 3
  • 1
    Note that after running the preprocessor, you'll also have all `#include` s at the top of the file (so prepare for a lot of scrolling). –  Oct 12 '10 at 16:45
  • There is a duplicate of this question at http://stackoverflow.com/questions/3917316/gcc-preprocessor with additional answers and discussion. – Kevin Vermeer Mar 28 '11 at 21:18
  • Visual studio: http://stackoverflow.com/questions/277258/c-c-source-file-after-preprocessing – Ciro Santilli OurBigBook.com Jan 17 '16 at 11:42

3 Answers3

39

Yes. Use the -E option:

gcc -E foo.c
JesperE
  • 63,317
  • 21
  • 138
  • 197
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
  • 333,147
  • 50
  • 533
  • 760
5

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Christoffer
  • 12,712
  • 7
  • 37
  • 53