Is a C++ compiler allowed to emit different machine code compiling the same program?(是否允许 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++ 编译器发出编译同一程序的不同机器代码?
基础教程推荐
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 从 std::cin 读取密码 2021-01-01