GCC preprocessor output and compilation in one pas

2019-02-12 18:15发布

Is it possible to generate preprocessor output and compilation in one step with GCC?

Something like:

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

that would generate main.o and main.i

2条回答
闹够了就滚
2楼-- · 2019-02-12 18:56

Yes.

Look at gcc -save-temps option.

It compiles the source file and saves the result of the preprocessing in a .i file. (It also saves the result of the assembler phase to a .s file).

gcc -save-temps -c main.cc -o main.o

will generate main.o but also main.i and main.s.

main.i is the result of the preprocessing.

查看更多
唯我独甜
3楼-- · 2019-02-12 19:06

No, not with -E itself, the -s, -c and -E options are called "stop" options. They actually halt the process at a specific point so you can't keep going.

If you want to do that, you have to do it in two passes, or use -save-temps to keep copies of the temporary files normally deleted during compilation.

From the gcc manpage, stuff discussing -E (slightly paraphrased):

If you only want some of the stages of compilation, you can use -x (or filename suffixes) to tell gcc where to start, and one of the options -c, -S, or -E to say where gcc is to stop. Note that some combinations (for example, -x cpp-output -E) instruct gcc to do nothing at all.

-E means: stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output (or to the output file if -o is specified).

If you use the -E option, nothing is done except preprocessing.

And a description of -save-temps:

-save-temps

Store the usual "temporary" intermediate files permanently; place them in the current directory and name them based on the source file.

Thus, compiling foo.c with -c -save-temps would produce files foo.i and foo.s, as well as foo.o.

This creates a preprocessed foo.i output file even though the compiler now normally uses an integrated preprocessor.

查看更多
登录 后发表回答