简体   繁体   English

gcc / g ++输出类型

[英]gcc/g++ output type

I know this is a very basic question but when I compile my c/c++ code with gcc/g++ what exactly is the type of the intermediate output before assembler comes into play to generate the machine code ? 我知道这是一个非常基本的问题,但是当我使用gcc / g ++编译我的c / c ++代码时,在汇编器发挥作用以生成机器代码之前,中间输出的确切类型是什么? Is it something like X86 instructions ? 它是否像X86指令?

GCC's processing chain is as follows: GCC的处理链如下:

  • your source code 你的源代码

  • preprocessed source code (expand macros and includes, strip comments) ( -E , .ii ) 预处理源代码(展开宏并包含,删除注释)( - -E.ii

  • compile to assembly ( -S , .s ) 编译成程序集( -S.s

  • assemble to binary ( -c , .o ) 汇编为二进制( -c.o

  • link to executable 链接到可执行文件

At each stage I've listed the relevant compiler flags that make the process stop there, as well as the corresponding file suffix. 在每个阶段,我都列出了使进程停止的相关编译器标志,以及相应的文件后缀。

If you compile with -flto , then object files will be embellished with GIMPLE bytecode, which is a type of low-level intermediate format, the purpose of which is to delay the actual final compilation to the linking stage, which allows for link-time optimizations. 如果使用-flto编译,那么目标文件将使用GIMPLE字节码进行修饰,这是一种低级中间格式,其目的是将实际的最终编译延迟到链接阶段,这允许链接时间优化。

The "compiling" stage proper is the actual heavy lifting part. 适当的“编译”阶段是实际的重型提升部分。 The preprocessor is essentially a separate, independent tool (although its behaviour is mandated by the C and C++ standards), and the assembler and linker are acutally separate, free-standing tools that basically just implement, respectively, the hardware's binary instruction format and the operating system's loadable executable format. 预处理器本质上是一个独立的独立工具(虽然它的行为是由C和C ++标准规定的),汇编器和链接器是实际上独立的独立工具,它们基本上分别只实现了硬件的二进制指令格式和操作系统的可加载可执行格式。

So, compilation of executable in GCC consists of 4 parts: 因此,GCC中可执行文件的编译由4部分组成:

1.) Preprocessing (gcc -E main.c > main.i; transforms *.c to *.i) Does include expansion, processes marcos. 1.)预处理(gcc -E main.c> main.i;将* .c转换为* .i)包括扩展,处理marcos。 Removes comments. 删除评论。

2.) Compilation (gcc -S main.i; transforms *.i to *.s, if successful) Compiles C-code to Assembler (on target x86 architecture it is x86-assembly, on target x86_64 architecture it is x64-assembly, on target arm architecture it is arm assembly, etc.) Most of Warnings and Errors happens during this part (eg does Error and Warning reporting) 2.)编译(gcc -S main.i;将* .i转换为* .s,如果成功)将C代码编译到Assembler(在目标x86架构上它是x86-assembly,在目标x86_64架构上它是x64-assembly ,在目标臂架构上,它是臂组件等。)大部分警告和错误发生在这一部分(例如,错误和警告报告)

3.) Assembly (as main.s -o main.o; transforms *.i to *.o, again if successful) Assemblies generated assembler to machine code. 3.)汇编(如main.s -o main.o;将* .i转换为* .o,如果成功则再次转换)程序集生成汇编程序到机器代码。 Though there are still relative address of procedures, and such. 虽然程序还有相对的地址等等。

4.) Linking (gcc main.o) Replaces relative addresses with absolute addresses. 4.)链接(gcc main.o)用绝对地址替换相对地址。 Removes useless text. 删除无用的文字。 Linking errors and warnings during this phase. 在此阶段链接错误和警告。 And in the end (if successful), we get executable file. 最后(如果成功),我们获得可执行文件。

So, to answer your question, the intermediate output you mean is actually so called assembly language - see wiki about that Assembly language wiki . 所以,为了回答你的问题,你所说的中间输出实际上就是所谓的汇编语言 - 请参阅关于汇编语言wiki的wiki

Here's a graphic representation of the gcc compilation steps by courtesy of redhat magazine : 以下是redhat杂志提供的gcc编译步骤的图形表示:

gcc编译步骤

Contrary to what other answers imply, there's no assembly step - rather, generating assembler code replaces the object code generation; 与其他答案所暗示的相反,没有汇编步骤 - 相反,生成汇编代码会替换目标代码生成; it doesn't make much sense to convert an in-memory representation to a textual one if what you really want is a binary representation. 如果您真正想要的是二进制表示,那么将内存中表示转换为文本表示没有多大意义。

It must be assembly code. 它必须是汇编代码。 You can get it using -S flag in command line for compilation. 您可以在命令行中使用-S标志来进行编译。

There is no "intermediate output". 没有“中间输出”。 The first output you get is machine code. 您获得的第一个输出是机器代码。 (Although you can get C/C++ intermediate output by invoking only the preprocessor with -E .) (尽管可以通过使用-E调用预处理器来获得C / C ++中间输出。)

GCC tool chain, compiles the program from source code down to machine code. GCC工具链,将程序从源代码编译为机器代码。 The compiler generates the assembly code which the assembler assembles into the machine code. 编译器生成汇编代码,汇编程序将汇编代码汇编到机器代码中。 Here is a good tutorial for beginners. 是一个很好的初学者教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM