17

Is there a way to run the GCC preprocessor, but only for user-defined macros?

I have a few one-liners and some #ifdef, etc. conditionals, and I want to see what my code looks like when just those are expanded.

As it is, the includes get expanded, my fprintf(stderr)s turn into fprintf(((__getreeent())->_stderr), etc.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Claudiu
  • 224,032
  • 165
  • 485
  • 680

5 Answers5

15

Call cpp directly, e.g.

$ cat >foo.c <<EOF
#define FOO
#ifdef FOO
foo is defined
#else
foo is not defined
#endif
EOF

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


foo is defined

Of course, if you include any headers then those will be included in the output. One way to avoid that might be to just grep -v out the lines with #includes (or perhaps just ones with #include < and allow #include "). Or you could specify the -nostdinc option to remove just standard includes (but possibly leave in local libraries unless you specify include paths so that they won't be found) - this would warn about missing headers, though.

Edit: Or use the preprocessor itself to make the inclusion of headers conditional, wrap them in something like #ifndef TESTING_PREPROCESSOR and use -DTESTING_PREPROCESSOR.

Arkku
  • 41,011
  • 10
  • 62
  • 84
  • 2
    +1 - The tip for putting the `#include` lines in an `#if` section is one of those simple yet effective things that might not occur to someone until a bunch of other effort was wasted trying other more complicated things. – Michael Burr Apr 11 '10 at 04:56
5
cpp -nostdinc program.c
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
3

One may use tools like unifdef, unifdefall — remove preprocessor conditionals from code

Marcin Zaluski
  • 687
  • 5
  • 10
1
gcc  -E inputfile.c > outputfile.c

outputfile.c will have your preprocessed code, but all macros will be expanded.

I find this trick very useful when debugging compilation of large systems with tons of includes, compiler flags, and makefile variables. It will expose include files that don't have header guards, and a bunch of other problems too.

Jay Godse
  • 15,163
  • 16
  • 84
  • 131
  • 1
    Isn't that just the standard way to run the preprocessor? How does this answer the question? Presumably the OP wants a non-standard behaviour(?). – Peter Mortensen Oct 13 '14 at 16:55
  • This is the standard way. If your program is heavy on user-defined macros, and light on non-user-defined macros, then the user-defined macros will be easy to spot. by running a diff of inputfile.c and outputfile.c. In many cases, gcc -E is good enough, though definitely perfect in detecting "only" user-defined macros. And it is a lot faster than using the perfect methods described in other answers. – Jay Godse Jul 07 '16 at 02:51
1

If you just want macro expansions and skip the #include handling, you can try this repo. It is a fork of the original pcpp. I added a --no-include option to skip the handling of #include directive. All other macros will be processed based on your marco input to pcpp.

smwikipedia
  • 61,609
  • 92
  • 309
  • 482