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
- 我有静态或动态 boost 库吗? 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01