$ gcc --help -save-temps Do not delete intermediate files -E Preprocess only; do not compile, assemble or link -S Compile only; do not assemble or link -c Compile and assemble, but do not link -o <file> Place the output into <file>
对c文件,想要保留中间文件,执行下面命令:
$ gcc -Wall -save-temps hello.c // 会生成文件 hello.i hello.s hello.o a.out
下面一一说明。
-E 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. Input files that don't require preprocessing are ignored.
预处理的任务是:
预处理可以理解成:把你所写的源代码转换成扩展的完整源代码。
What the preprocessor does is convert the source code file you write into another source code file (you can think of it as a “modified” or “expanded” source code file).
使用 -save-temps
选项时,预处理的输出被存放进了 .i
文件。
-S Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified. By default, the assembler file name for a source file is made by replacing the suffix .c, .i, etc., with .s. Input files that don't require compilation are ignored.
编译的任务是:
使用 -save-temps
选项时,编译的输出被存放进了 .s
文件。
-c Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file. By default, the object file name for a source file is made by replacing the suffix .c, .i, .s, etc., with .o. Unrecognized input files, not requiring compilation or assembly, are ignored.
汇编的任务是:
本阶段只有本文件存在的代码才被翻译成二进制机器语言。
At this stage only the existing code is converted into machine language, the function calls like printf() are not resolved.
使用 -save-temps
选项时,汇编的输出被存放进了 .o
即目标文件。
注意:在不细分的情况下,可以把编译(compile)和汇编(assembly)两个过程合称为编译。
-o file Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code. If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its assembler file in source.s, a precompiled header file in source.suffix.gch, and all preprocessed C source on standard output.
链接的任务是:
在前面几个阶段,gcc可能并不知道某些函数等的定义(例如printf()
),只是在函数调用处放了个占位符(place-holder )。
在本阶段,printf()
的函数定义被解析出来,其真实地址被放入。
As discussed earlier, till this stage gcc doesn’t know about the definition of functions like printf(). Until the compiler knows exactly where all of these functions are implemented, it simply uses a place-holder for the function call. It is at this stage, the definition of printf() is resolved and the actual address of the function printf() is plugged in.
preprocessor 预处理器cpp
compiler 编译器cc
、as
It does this by turning the C source code into an object code file, which is a file ending in “.o” which contains the binary version of the source code. Object code is not directly executable, though.
In order to make an executable, you also have to add code for all of the library functions that were #included into the file (this is not the same as including the declarations, which is what #include does). This is the job of the linker.
linker 链接器ld
The job of the linker is to link together a bunch of object files (.o files) into a binary executable. This includes both the object files that the compiler created from your source code files as well as object files that have been pre-compiled for you and collected into library files. These files have names which end in
.a
or.so
, and you normally don’t need to know about them, as the linker knows where most of them are located and will link them in automatically as needed.
整个过程:
sourceFile --[preprocessor]--> newSourceFile --[compiler]--> objectFile --[linker+library]--> executableFile
http://www.thegeekstuff.com/2011/10/c-program-to-an-executable/
http://courses.cms.caltech.edu/cs11/material/c/mike/misc/compiling_c.html
预处理器: https://en.wikipedia.org/wiki/Preprocessor
编译器: https://en.wikipedia.org/wiki/Compiler
链接器: https://en.wikipedia.org/wiki/Linker_(computing)
ELF格式: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format