Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a "light" preprocessor for GCC

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.

like image 300
Claudiu Avatar asked Apr 11 '10 03:04

Claudiu


People also ask

How do I run a GCC preprocessor?

The -E option causes gcc to run the preprocessor, display the expanded output, and then exit without compiling the resulting source code. The value of the macro TEST is substituted directly into the output, producing the sequence of characters const char str[] = "Hello, World!" ; .

How do I enable macros in CPP?

1.1 Enable and disable macros during compilationMSVC (Microsft Visual C++ compiler) uses '/' slash for defining macros from command line. I this case the macro can be defined using /DMACRO=VALUE. Build without setting any preprocessor directive. Build defining the LOGGING macro.


1 Answers

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.

like image 53
Arkku Avatar answered Oct 05 '22 21:10

Arkku