是否允许 C++ 编译器发出不同的机器代码来编译相同的程序?

Is a C++ compiler allowed to emit different machine code compiling the same program?(是否允许 C++ 编译器发出不同的机器代码来编译相同的程序?)

本文介绍了是否允许 C++ 编译器发出不同的机器代码来编译相同的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一种情况.我们有一些特定的 C++ 编译器、一组特定的编译器设置和一个特定的 C++ 程序.

Consider a situation. We have some specific C++ compiler, a specific set of compiler settings and a specific C++ program.

我们使用该编译器和这些设置编译该特定程序两次,每次都进行干净编译".

We compile that specific programs with that compiler and those settings two times, doing a "clean compile" each time.

发出的机器代码应该是相同的(我不是说时间戳和其他花里胡哨,我的意思是只有将被执行的真实代码)还是允许从一个编译到另一个编译而有所不同?

Should the machine code emitted be the same (I don't mean timestamps and other bells and whistles, I mean only real code that will be executed) or is it allowed to vary from one compilation to another?

推荐答案

C++ 标准当然没有规定要防止这种情况发生.然而,实际上,编译器通常是确定性的,因此给定相同的输入,它将产生相同的输出.

The C++ standard certainly doesn't say anything to prevent this from happening. In reality, however, a compiler is normally deterministic, so given identical inputs it will produce identical output.

真正的问题主要是它将环境的哪些部分视为其输入——有一些似乎假设构建机器的特性反映了目标的特性,并改变了它们的输出基于构建环境中隐含的输入"而不是明确声明,例如通过编译器标志.也就是说,即使这样也相对不寻常.规范是输出依赖于显式输入(输入文件、命令行标志等)

The real question is mostly what parts of the environment it considers as its inputs -- there are a few that seem to assume characteristics of the build machine reflect characteristics of the target, and vary their output based on "inputs" that are implicit in the build environment instead of explicitly stated, such as via compiler flags. That said, even that is relatively unusual. The norm is for the output to depend on explicit inputs (input files, command line flags, etc.)

随便,我只能想到一件相当明显的自发"变化的事情:一些编译器和/或链接器将时间戳嵌入到它们的输出文件中,因此输出文件的几个字节将从一个构建更改为下一个构建--但这只会在嵌入文件的元数据中,而不是对生成的实际代码的更改.

Offhand, I can only think of one fairly obvious thing that changes "spontaneously": some compilers and/or linkers embed a timestamp into their output file, so a few bytes of the output file will change from one build to the next--but this will only be in the metadata embedded in the file, not a change to the actual code that's generated.

这篇关于是否允许 C++ 编译器发出不同的机器代码来编译相同的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:是否允许 C++ 编译器发出不同的机器代码来编译相同的程序?

基础教程推荐