views:

252

answers:

4

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?

+1  A: 

I'd bet it would vary every time due to some metadata compiler writes (for instance, c# compiled dlls always vary in some bytes even if I do "build" twice in a row without changing anything). But anyways, I would never rely on that it would not vary.

František Žiačik
+4  A: 

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.)

Jerry Coffin
What I would give for a non deterministic compiler that invented optimizations on the fly.
Martin York
Visual Studio offers Profile Guided Optimization.
DeadMG
+4  A: 

There is no guarantee that they will be the same. Also according to http://www.mingw.org/wiki/My_executable_is_sometimes_different

My executable is sometimes different, when I compile and recompile the same source. Is this normal?

Yes, by default, and by design, ~MinGW's GCC does not produce ConsistentOutput, unless you patch it.

EDIT: Found this post that seems to explain how to make them the same.

Romain Hippeau
I followed the link, but there was no explanation of why!
Martin York
@martin York updated my post with another link to make them the same.
Romain Hippeau
+2  A: 

According to the as-if rule in the standard, as long as a conforming program (e.g., no undefined behavior) cannot tell the difference, the compiler is allowed to do whatever it wants. In other words, as long as the program produces the same output, there is no restriction in the standard prohibiting this.

From a practical point of view, I wouldn't use a compiler that does this to build production software. I want to be able to recompile a release made two years ago (with the same compiler, etc) and produce the same machine code. I don't want to worry that the reason I can't reproduce a bug is that the compiler decided to do something slightly different today.

KeithB