I have a number of debug statements defined in a program, and I want to be able to make a copy of the source without these statements.
In order to do this I first looked at GCC's -E command line argument, which only runs the preprocessor, however this did far more than I wanted, expanding the included files and adding #line statements.
For example:
#include <stdio.h>
#ifdef DEBUG
#define debug( s ) puts ( s );
#else
#define debug( s )
#endif
int main( int argc, char* argv[] )
{
debug( "Foo" )
puts( "Hello, World!" );
return 0;
}
I'd want this to be processed to:
#include <stdio.h>
int main( int argc, char* argv[] )
{
puts( "Hello, World!" );
return 0;
}
I could then tidy that up with something like astyle and no manual work would be needed to get exactly what I want.
Is there a directive I'm missing for GCC or is there a tool capable of doing this?
There's no direct way to do that with the gcc preprocessor, though if you only include system headers, you might have some luck with
gcc -E -nostdinc
.However, you can comment out the
#include
directives, and other preprocessor directives you don't want processed, and run the code through the preprocessor (gcc -E
orcpp
) , that way only the macro you want expanded(the ones not commented out) gets expanded.I know the question is old, but it does have an answer now. The "C Partial Preprocessor" does exactly this.
http://www.muppetlabs.com/~breadbox/software/cppp.html
For reference, if someone else still wonders (I did and found this page).
gcc -E -nostdinc test.c
producesand an error to stderr
You can easily filter out the # lines ... and re-add the includes.
One may use tools like unifdef, unifdefall — remove preprocessor conditionals from code. (Run a "light" preprocessor for GCC)
If
-E
is not helping, then try using-fdump-tree-all
and if you don't see what you want the that is not-available-in (or) not-provided-byGCC
.OTOH, this question has been discussed in SO as follows, please refer the below to get some ideas.
Hope it helps!
Hi Mat,
I saw your comment to @nos. But I have one such script handy and so sharing it with you. You can try reading my answer for a similar question here
Copy the below code in a file, say
convert.sh
. Assign execute permission to that file,chmod +x convert.sh
and run it as follows:The
<filename>.c.done
will have what you need!Hope this helps!