问题
I have just started getting involved with GCC.
Lets say we have a text file written by vim labaled helloworld.c
Why would SWIM (someone who isn't me) compile it with
gcc helloworld.c in comparison to gcc -c helloworld.c and then gcc -o helloWorld helloworld.o ?
回答1:
For one single file, there is no use. But if you compile a larger project, you may not want to recompile all files, if you only changed one for instance. And thus, gain compile time.
Using intermediate .o
files lets you only recompile what needs to be recompiled, and link the final binary with all those objects files.
Another usecase is if you want to use a custom link script, for instance to choose the location of your sections or code. Then you need to get the actual code from .o
files.
回答2:
There are some phases in compiler process:
Preprocessing -> Compiling -> Assembling -> Linking.
Phases:
- Preprocessing : include some test from your .h files into your .c files and create a preprocessed source code.
- Compiling : create assembler code from your preprocessed code.
- Assembling : create object modules from your assembler code.
- Linking : create executable files from your object modules.
When you use gcc [options]
:
If you use:
gcc -E
Stops after preprocessing phase and gives you preprocessed code.gcc -S
Stops after Compiling phase and gives you assembler code.gcc -c
Stops after Assembling phase and gives you a object module.
gcc -o
gives you a executable file with included name, by default gives you a.out
man gcc HERE
来源:https://stackoverflow.com/questions/28644317/what-is-the-difference-compiling-gcc-c-file-in-comparison-to-making-a-c-file-i