1

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.

17
  • 2
    XY problem. What's the problem using two steps? Commented May 19, 2017 at 23:04
  • @Olaf taking twice as long. The preprocessor is already invoked during compilation so why shouldn't this be possible? Commented May 19, 2017 at 23:05
  • That's not the same. On modern compilers there are not necessarily two distinct passes for preprocessing and compilation. And if there are two phases you most likely don't loose much performance. However, if you are after speeding up compilation, use a build tool; it will save much more time than what you seem to worry about. Commented May 19, 2017 at 23:11
  • according to this site regarding -E option: (...) and one of the options -c, -S, or -E to say where gcc is to stop - "to stop" does not leave much hope in doing that in one step. Commented May 19, 2017 at 23:12
  • 1
    @Barmar: It generates that as a special favor when it is asked. When it continues on normally, it never generates a text form. It is parsing the file into in-memory syntax trees from preprocessing and C/C++ parsing at the same time. Commented May 19, 2017 at 23:25

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.