C ++程序的编译阶段是什么? [英] What are the stages of compilation of a C++ program?

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

问题描述

标准指定的C ++程序的编译阶段是什么?



如果是这样,它们是什么?



如果不是,广泛使用的编译器(我更喜欢MSVS)的答案将是巨大的。



我说的是预处理,标记化,解析等。



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

解决方案


标准指定的C ++程序的编译阶段是什么?


是和否。



C ++标准定义了9个翻译阶段。引用自2011年3月的 N3242草案(10MB PDF) -02-28(在官方C ++ 11标准发布之前),第2.2节:


语法规则之间的优先级的翻译由以下阶段指定:


  1. 映射物理源文件字符一个实现定义的方式,如果
    必要的话,对基本源字符集
    (引入行结束指示符的新行字符)。 [SNIP]

  2. 每个反斜杠字符(\)紧接着一个新行字符的实例都被删除,将实际源代码行拼接到
    形成逻辑源线。 [SNIP]

  3. 源文件被分解为预处理令牌(2.5)和空格字符序列(包括注释)。 [SNIP]

  4. 执行预处理指令,扩展宏调用,并执行_Pragma一元运算符表达式。 [SNIP]

  5. 字符文字或字符串文字中的每个源字符集成员以及每个转义序列和通用字符名
    在字符文字或非原始字符串文字中,转换为
    执行字符集的相应成员;

    [SNIP]

  6. 相邻的字符串字符串字符串。

  7. 分隔符号的白色空格字符不再重要。每个预处理令牌都将转换为令牌。 (2.7)。
    产生的令牌在语法和语义上被分析,
    被翻译为翻译单元。 [SNIP]

  8. 翻译单位和实例化单位合并如下: [SNIP]

  9. 所有外部实体参考都已解决。链接库组件以满足对未在
    当前翻译中定义的实体的外部引用。所有这样的翻译器输出被收集到
    程序映像中,其包含在其
    执行环境中执行所需的信息。



<



$ b

$ b

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



要强调的是,编译器不是必须遵循这个确切的模型,只要最终结果如同他们所做的那样。



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



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


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

If so, what are they?

If not, an answer for a widely-used compiler (I'd prefer MSVS) would be great.

I'm talking about preprocessing, tokenization, parsing and such. What is the order in which they are executed and what do they do in particular?

EDIT: I know what compilation, linking and preprocessing do, I'm mostly interested in the others and the order. Explanations for these are, of course, also welcomed since I might not be the only one interested in an answer.

解决方案

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

Yes and no.

The C++ standard defines 9 "phases of translation". Quoting from the N3242 draft (10MB PDF), dated 2011-02-28 (prior to the release of the official C++11 standard), section 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) if necessary. [SNIP]
  2. Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form 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-name in a character literal or a non-raw string literal, is converted to the 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). The resulting tokens are syntactically and semantically analyzed and translated 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 the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment.

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

As indicated by the [SNIP] markers, I haven't quoted the entire section, just enough to get the idea across.

To emphasize, compilers are not required to follow this exact model, as long as the final result is as if they did.

Phases 1-6 correspond more or less to the preprocessor, 7 to what you might normally think of as compilation, 8 deals with templates, and 9 corresponds to linking.

(C's translation phases are similar, but #8 is omitted.)

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

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