程序员的自我修养-链接、装载与库,很好的一本书。最近在读,摘抄一些内容作为笔记。
1.c语言编译分为四个步骤:预处理、编译、汇编、链接
step1、预处理
gcc -E XX.c -o xx.i
或者
cpp xx.c > xx.i
//cpp(全部小写) - The C Preprocessor,终端输入man cpp可进一步理解cpp的作用
man gcc可对-E选项有进一步的了解
-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.
step2、编译(output:汇编代码)
gcc -S XX.c -o xx.s
作用:把预处理完的文件进行一系列词法分析、语法分析、语义分析及优化后生成相应的汇编代码。
man gcc中对选项的说明
-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.
step3、汇编(output:目标文件object file)
gcc -c XX.c -o xx.o
或者
as xx.s -o xx.o
作用:将汇编代码转变为机器可以执行的命令。
man gcc中对-c的说明
-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.
step4、链接
//困惑
2.个人理解
机器不懂汇编也不懂c语言;对我们人而言,理解汇编有点点麻烦,理解c语言的语句几乎没有问题。
用c语言编写代码,然后翻译(编译)为机器能理解的二进制代码,故编译的作用就是两个翻译的过程,
一是c语言翻译为汇编代码,二是汇编代码翻译为机器代码。