gpt4 book ai didi

c++ - C++程序的编译阶段是什么?

转载 作者:IT老高 更新时间:2023-10-28 12:59:33 25 4
gpt4 key购买 nike

标准是否规定了 C++ 程序的编译阶段?

如果有,它们是什么?

如果不是,一个广泛使用的编译器的答案(我更喜欢 MSVS)会很棒。

我说的是预处理、标记化、解析等。它们的执行顺序是什么?它们具体做了什么?

编辑:我知道编译、链接和预处理做什么,我最感兴趣的是其他和顺序。当然,也欢迎对这些解释进行解释,因为我可能不是唯一对答案感兴趣的人。

最佳答案

Are the stages of compilation of a C++ program specified by the standard?

是和不是。

C++ 标准定义了 9 个“翻译阶段”。引自 the N3242 draft (10MB PDF),日期为 2011-02-28(在官方 C++11 标准发布之前),第 2.2 节:

The precedence among the syntax rules of translation is specified by the following phases [see footnote].

  1. Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set(introducing new-line characters for end-of-line indicators) ifnecessary. [SNIP]
  2. Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines toform logical source lines. [SNIP]
  3. The source file is decomposed into preprocessing tokens (2.5) and sequences of white-space characters (including comments). [SNIP]
  4. Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator expressions are executed. [SNIP]
  5. Each source character set member in a character literal or a string literal, as well as each escape sequence and universal-character-namein a character literal or a non-raw string literal, is converted tothe corresponding member of the execution character set; [SNIP]
  6. Adjacent string literal tokens are concatenated.
  7. White-space characters separating tokens are no longer significant. Each preprocessing token is converted into a token. (2.7). Theresulting tokens are syntactically and semantically analyzed andtranslated as a translation unit. [SNIP]
  8. Translated translation units and instantiation units are combined as follows: [SNIP]
  9. All external entity references are resolved. Library components are linked to satisfy external references to entities not defined in thecurrent translation. All such translator output is collected into aprogram image which contains information needed for execution in itsexecution environment.

[footnote] Implementations must behave as if these separate phases occur, although in practice different phases might be folded together.

[SNIP] 标记所示,我没有引用整个部分,只是足以理解这个想法。

需要强调的是,编译器不需要遵循这个确切的模型,只要最终结果与他们一样。

第 1-6 阶段或多或少对应于预处理器,第 7 阶段对应您通常认为的编译,第 8 阶段处理模板,第 9 阶段对应于链接。

(C的翻译阶段类似,但#8省略了。)

关于c++ - C++程序的编译阶段是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8833524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com