C语言 预处理 编译 汇编 链接

2021/4/23 12:27:04

本文主要是介绍C语言 预处理 编译 汇编 链接,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

C语言 预处理 编译 汇编 链接

gcc help

$ 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

下面一一说明。

(1)Pre-processing

-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.

预处理的任务是:

  • Macro substitution 宏(#define)替换
  • Comments are stripped off 删注释
  • Expansion of the included files 展开包含文件(#include)的声明

预处理可以理解成:把你所写的源代码转换成扩展的完整源代码。

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 文件。

(2)Compilation

-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 文件。

(3)Assembly

-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.

汇编的任务是:

  • 把汇编指令翻译成二进制的目标文件(参见ELF格式)

本阶段只有本文件存在的代码才被翻译成二进制机器语言。

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)两个过程合称为编译。

(4)Linking

-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.

链接的任务是:

  • 把生成的目标文件(.obj)和库(lib)文件等链接,生成可执行(exe)文件、库(lib)文件、其他文件(如dll)等。

在前面几个阶段,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 编译器ccas

    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

 



这篇关于C语言 预处理 编译 汇编 链接的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程