Antonio Antonio - 5 months ago 20
C Question

Is there a way to omit the definitions (line markers) at the top of the C-preprocessor output?

If I process the following

input file with
gcc -C -x c -E test.def

#define TEST foo
int TEST;

I would like the output to be just:

int foo;

Instead I get:

# 1 "test.def"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.def"

int foo;

Is there a way I can omit those extra lines at the top?

Answer Source

These are not just on the top - but instead they're the line markers that the C preprocessors use to convey the source code positions where certain lines come from, to the C compiler.

With GCC this is easy, as GCC supports the -P switch:

-P. Inhibit generation of linemarkers in the output from the preprocessor. This might be useful when running the preprocessor on something that is not C code, and will be sent to a program which might be confused by the linemarkers.

Thus, use gcc -E -P -x c.

Also, I'd not use -C (retain comments), as it seems that with it gcc adds some comments from implicit header files.