编译的主要步骤是什么? [英] What are the main steps behind compiling?

查看:22
本文介绍了编译的主要步骤是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译C程序的主要步骤是什么?通过编译,我的意思是(可能是错误的)使用 gcc 从包含 C 代码的纯文本中获取二进制文件.

What are the main steps behind compiling a C program? By compiling, I mean (maybe wrongly) getting a binary from a plain text containing C code, using gcc.

我很想了解这个过程的一些关键点:

I would love to understand some key points of the process:

  1. 到一天结束时,我需要将我的 C 代码转换为一种我的 CPU 应该能够理解的语言.那么,谁在乎了解我的特定于 CPU 的指令?操作系统?

gcc 是否将任何 C 语言转换为汇编语言?

Is gcc converting any C to assembly language?

我知道(实际上是猜测)对于每种处理器类型,我都需要一个汇编器来解释(?)汇编代码并转换为我的 CPU 特定指令.这个汇编器在哪里(谁运送它)?它是否随操作系统一起提供?

I know (actually guess) that for each processor type I will need an assembler that will interpret (?) the assembly code and translate to my CPU specific instructions. Where is this assembler (who ships it)? Does it comes with the OS?

如果我用文本编辑器打开二进制文件,为什么我看不到 0 和 1?

Why exactly I can't see the 0s and 1s if I open the binary file with a text editor?

推荐答案

到一天结束时,我需要将我的 C 代码转换为一种我的 CPU 应该能够理解的语言.那么,谁在乎我的 CPU 特定指令呢?操作系统?

By the end of the day I need to transform my C code to a language that specifically my CPU should understand. So, who cares about knowing my CPU-specific instructions? The operating system?

你这里不是很清楚.如果您要问,哪个工具了解您的 CPU 特定指令,它是汇编器、反汇编器、调试器,也许还有其他一些工具.他们可以生成机器代码或将其转换回反汇编.

You are not very clear here. If you are asking, which tool has knowledge of your CPU specific instructions, it's the assembler, disassembler, debugger, and maybe some others. They can generate machine code or convert it back to disassembly.

如果您问谁在乎使用了哪些指令,那么需要执行它们的是处理器,因为每个指令集甚至以完全不同的方式表示诸如将两个整数相加"这样的常见指令.

If you are asking who cares about which instructions are used, it's the processor that needs to execute them, as each instruction set represents even such common instruction as "add two integers" in completely different manner.

gcc 是否将任何 C 语言转换为汇编语言?

Is gcc converting any C to assembly language?

是的,C(或任何其他受支持语言的程序)由 GCC 转换为汇编.涉及的步骤很多,过程中至少使用了两个额外的内部表示.GCC internals 文档中解释了详细信息.最后,编译器后端"生成简单模式"的汇编表示,由先前的编译器传递生成.您可以使用 -S 标志要求 GCC 输出此程序集.如果你没有特别要求,下一步(组装)会自动执行,你只能看到最终的可执行文件.

Yes, C (or program in any other supported language) is converted to assembly by GCC. There are many steps involved, and at least two additional internal representations used in process. Details are explained in GCC internals document. Finally compiler "backend" generates assembly representation of simple "patterns", generated by previous compiler passes. You can ask GCC to output this assembly by using -S flag. If you don't specifically ask for it, next step (assembling) is automatically executed and you only see your final executable file.

我知道(实际上是猜测)对于每种处理器类型,我都需要一个汇编程序来解释(?)汇编代码并转换为我的 CPU 特定指令.这个汇编器在哪里(谁运送它)?操作系统自带吗?

I know (actually guess) that for each processor type I will need an assembler that will interpret (?) the assembly code and translate to my CPU specific instructions. Where is this assembler (who ships it)? Does it comes with the OS?

首先要注意每个 CPU 的汇编语言是不同的,因为它们应该 1:1 代表 CPU 的机器语言.汇编程序然后将汇编代码翻译成机器代码.谁运送?任何建造它的人.对于 GNU 工具链,它是 binutils 包的一部分,并且通常默认安装在大多数 Linux 发行版上.这不仅是汇编程序可用.另请注意,尽管 GNU 套件" (GCC/binutils/gdb) 支持许多体系结构,但您需要为您的体系结构使用适当的端口.例如,您的台式 PC 的默认汇编器无法编译/汇编为 ARM 机器代码.

First take note that assembly languages for each CPU differ, as they are supposed to represent CPU's machine language 1:1. Assembler then translated assembly code into machine code. Who ships it? Anyone who builds it. With GNU toolchain it's part of binutils package and it's usually installed by default on most Linux distributions. This is not only assembler available. Also note, that although GNU "suite" (GCC/binutils/gdb) support many architectures, you need to use appropriate port for your architecture. Your desktop PC's default assembler for example can not compile/assemble into ARM machine code.

如果我用文本编辑器打开二进制文件,为什么我看不到 0 和 1?

Why exactly I can't see the 0s and 1s if I open the binary file with a text editor?

因为文本编辑器应该显示 0 和 1 的文本表示.假设文件中的每个字符需要 8 位,他们将每个后续 8 位解释为单个字符,而不是显示单独的位.如果您知道在标准的 8 位 ASCII 字母 'A' 由值 65 表示,您还可以将其转换回二进制:01000001.将十六进制表示转换回二进制会更容易一些.为此,您可以使用 hexdump(或类似)工具.

Because text editor is supposed to show text representation of that 0s and 1s. Assuming each character in file takes 8 bits they interpret each subseqent 8-bits as single character, instead of showing separate bits. If you know that in standard 8 bit ASCII letter 'A' is represented by value 65, you can also convert this back to binary: 01000001. It's a bit easier to convert hexadecimal representation back to binary. For this you can use hexdump (or similar) tool.

这篇关于编译的主要步骤是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆