简体   繁体   English

编译器什么时候把代码编译成机器码?

[英]when does the compiler compile the code into machine code?

As far as I know, the compiler compiles the code by converting it to a language that a computer can understand which is the machine language and this is done before running the code.据我所知,编译器通过将代码转换为计算机可以理解的语言来编译代码,这是机器语言,这是在运行代码之前完成的。

So, does the compiler compile my code each time I write a character in the file?那么,每次我在文件中写入一个字符时,编译器会编译我的代码吗?

And if so, does it check the whole code?如果是这样,它会检查整个代码吗? Or just the line that updated.或者只是更新的行。

An important part to this question is the type of programming language(PL) we are talking about.这个问题的一个重要部分是我们正在谈论的编程语言(PL)的类型。 Generally speaking, I would categorize PL into 3 groups:一般来说,我会将 PL 分为 3 组:

Traditional PLs.传统的PL。 Ex: C, C++, Rust例如:C、C++、Rust

The compiler compiles the code into machine language when you hit the "build" button or the "run" button.当您点击“构建”按钮或“运行”按钮时,编译器会将代码编译成机器语言。 It doesn't compile every time you change the code, but a code linter does continuously observe your code and check it for errors.它不会在您每次更改代码时编译,但代码 linter 会持续观察您的代码并检查它是否有错误。

Another note, when you change part of the code and compile it, the compiler doesn't recompile everything.另请注意,当您更改部分代码并对其进行编译时,编译器不会重新编译所有内容。 It usually only recompile the current assembly file (or module or whatever you call them).它通常只重新编译当前的程序集文件(或模块或你称之为的任何东西)。

It is also important to note that a lot of modern IDEs, compile when you save the files.还需要注意的是,许多现代 IDE 在您保存文件时进行编译。

There is also the hot reload feature.还有热重载功能。 It is a smart compiler feature that can swap certain parts of the code while it is running.它是一种智能编译器功能,可以在运行时交换代码的某些部分。

Interpreted PLs Ex: python, JS and PHP解释的 PL 例如:python、JS 和 PHP

Those languages never get compiled;这些语言永远不会被编译; Rather, they get interpreted or translated into native code on the fly and in-memory when you run them.相反,当您运行它们时,它们会在运行中和内存中被解释或翻译成本机代码。 Those languages usually employee a cache to accelerate the subsequent code execution.这些语言通常使用缓存来加速后续代码执行。

Intermediary Code PL.中介代码 PL。 Ex: Kotlin, java, C#例如:Kotlin、java、C#

Have 2 stages of compilation:有2个编译阶段:

  • Build time compilation.构建时编译。
  • Just in time (run-time) compilation.及时(运行时)编译。

Build time compilation converts the code into intermediary language (IL) machine code, which is special to the run-time.构建时编译将代码转换为中间语言 (IL) 机器代码,这是运行时所特有的。 This code only understood by the run time like Java runtime or dot net runtime此代码仅由运行时理解,例如 Java 运行时或点网运行时

The second compilation happens when the programs get installed or ran for the first time.第二次编译发生在程序第一次安装或运行时。 This is called just in time compilation (JIT)这称为即时编译 (JIT)

The run-time convert the code into native code specific to the run-time OS.运行时将代码转换为特定于运行时操作系统的本机代码。

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

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