What is the difference compiling gcc .c file in comparison to making a .c file into an .o file and linking and assembling?

半腔热情 提交于 2020-01-06 01:25:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!