是否允许 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++ 编译器发出编译同一程序的不同机器代码?