Bob
Bob

Reputation: 4970

GCC preprocessor output and compilation in one pass, v2

As this question says, I need a way to do

gcc -E -c main.cc -o main.o

On my compiler, from QNX but based on gcc 4.4.2, using -save-temps only gives me the assembly files (and no preprocessor files).

If I add -E like in the command above, the preprocessor output will be saved to main.o.

I need a solution that does both compilation AND outputs the preprocessor in one invocation of gcc with the above constraints.

Upvotes: 1

Views: 671

Answers (1)

Mike Kinghan
Mike Kinghan

Reputation: 61337

On my compiler, from QNX but based on gcc 4.4.2, using -save-temps only gives me the assembly files (and no preprocessor files).

I can't verify that for such an old version of GCC, or any QNX variant. Certainly all mainline GCC versions at least as old as 4.7 respect ... -c -save-temps foo.c by saving the preprocessed source in foo.i and the assembly in foo.s.

But if that's the case with your QNX GCC 4.4.2, there's a workaround.

From your comments it appears that your objection to invoking the compiler twice is that you do not want time to be wasted in preprocessing the source twice. But you can invoke the compiler twice, once to do the preprocessing only, and again to do the compiling only, so I presume that would be a satisfactory solution.

Your wished-for command line:

gcc -E -c main.cc -o main.o

shows a C++ source file being given to the C compiler. I assume that's a slip. The recipes for the outcome you're after are symmetrically different for C and for C++.

For C you want:

gcc -E main.c > main.i && gcc -c -o main.o main.i

For C++ you want:

g++ -E main.cc > main.ii && g++ -c -o main.o main.ii

This writes the preprocessed C[C++] output that you want to save to main.i[.ii] and then passes that preprocessed output to the compiler again for C[C++] compilation. gcc[g++] recognizes the file extension .i[.ii] as denoting C[C++] source code that should not be preprocessed. See 3.2 Options Controlling the Kind of Output; so it will not attempt to preprocess the source again.

This solution also has the merit of not generating the assembly files, which you don't want.

Upvotes: 2

Related Questions