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

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

问题描述

什么是背后编译C程序的主要步骤是什么?通过编译,我的意思是(也许错误地)获得由含有C code一个纯文本的二进制,使用gcc。

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


  1. 在这一天,我需要我的C code转化为专门我的CPU应该理解的语言的结束。那么,谁在乎知道我的 CPU特定说明?操作系统?


  2. 是gcc任何C转换为组装语言?


  3. 我知道(实际上猜的),对于每个处理器类型,我需要一个汇编程序将inter preT(?)大会code和翻译成我的CPU具体说明。这哪里是汇编(谁船吧)?它是否自带的操作系统?


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



解决方案

  

在这一天,我需要我的C code转化为专门我的CPU应该理解的语言的结束。那么,谁在乎知道我的CPU特定的指令?操作系统?


您还不是很清楚这里。如果你问,哪一种工具有你的CPU的具体指示的知识,它的汇编,反编译器,调试器,也许一些人。他们可以生成机器code或将其转换回拆卸。

如果你问谁在乎这是用来说明,这是一个需要执行它们的处理器,因为每个指令集重presents连这样普通的指令在完全不同的方式两个整数相加。


  

是gcc任何C转换成汇编语言?


是的,C(或程序中的任何其他支持的语言)由GCC转化为组装。有涉及许多步骤,并且在过程中使用的至少两个附加内部重新presentations。细节海合会内部文件解释。最后编译后端生成简单的模式重新组装presentation,由previous编译器生成的传递。您可以通过使用-S标志要求GCC输出本次大会。如果你没有特别要求它,(组装)下一步会自动执行,你只能看到最终的可执行文件。


  

我知道(实际上猜的),对于每个处理器类型,我需要一个汇编程序将inter preT(?)大会code和翻译成我的CPU的具体说明。这哪里是汇编(谁船吧)?它是否自带的操作系统?


首先注意到每个CPU的汇编语言不同,因为他们应该重新present CPU的机器语言1:1。然后汇编器翻译汇编code成机器code。谁船呢?任何人谁建造它。有了GNU工具链是的binutils 包的一部分,它通常是在默认情况下大多数Linux发行版安装。这不仅是可用的汇编。还要注意的是,尽管GNU套件(GCC / binutils的/ GDB)支持多种架构,则需要使用相应的端口为您的架构。例如台式PC的默认汇编不能编译/组装成ARM机code。


  

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


由于文本编辑器应该表明,0和1的文本重新presentation。文件假设每个角色需要,而不是单独显示位8位,他们之间的preT每个subseqent 8位单字符。如果你知道,在标准的8位ASCII字母按价值计算65 psented'A'是重新$ P $,你也可以转换这回二进制:01000001.这是一个有点容易十六进制再presentation转换回二进制。为此,您可以使用hexdump都可以(或类似)的工具。

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

  2. Is gcc converting any C to assembly language?

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

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

解决方案

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?

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.

Is gcc converting any C to assembly language?

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.

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?

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.

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

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天全站免登陆