20

Possible Duplicate:
Running the GCC preprocessor

Is there a GCC option to make the GCC preprocessor generate C source code but filter out irrelevant source code?

For example, a C file has #define switch to define for many different platforms. I'm only intersted in one platform, so I want the C preprocessor to filter out unrelated code. Does GCC support this?

Community
  • 1
  • 1
richard
  • 1,607
  • 6
  • 15
  • 18
  • 2
    Exact duplicate (looks like same user, earlier today): [gcc preprocessor](http://stackoverflow.com/questions/3916979/gcc-preprocessor) – Paul R Oct 12 '10 at 17:43
  • @Paul Is indeed the same user; the original version of this question before Michael cleaned it up was word-for-word identical, including the *Richard Luo* sig. – user229044 Oct 12 '10 at 17:57

4 Answers4

43

Use gcc -E to only run the preprocessor part, e.g. give a file in.c

#if 0
0;
#endif

#if 1
1;
#endif

running

$ gcc -E in.c -o in.i

yields a file in.i

# 1 "in.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "in.cpp"





1;

i.e. the parts behind the #if 0 got removed. If you would have #include'd files they would have been pasted too though, so I am not sure how much help this is.

Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80
5

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

Christoffer
  • 12,712
  • 7
  • 37
  • 53
2

Yes - almost certainly your compiler provides certain default definitions in the environment that you can use to turn code on and off for different systems. __GNUC__ is a good one for GCC. For example:

#ifdef __GNUC__
#define SOME_VALUE 12
#else
#define SOME_VALUE 14
#endif

If you compile that block with GCC, SOME_VALUE will be 12, and if you compile with MSVC, for example, SOME_VALUE will be 14. A list of platform specific definitions is available at this question.

Community
  • 1
  • 1
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
-1

You probably can use:

gcc -CC -P -Uswitch -undef -nostdinc -fdirectives-only -dDI -E 

With switch the #define you know will be undefined.

Nicolas Viennot
  • 3,921
  • 1
  • 21
  • 22