Repeatable object code generation c++
Asked Answered
S

4

5

When I build a project using a c++ compiler, can I make sure that the produced binary is not affected if there were no changes in source code? It looks like everytime I recompile my source, the binary's md5 checksum is affected. Is the time of compilation somehow affecting the binary produced? How can I produce compilation results which are repeatable?

Stefansson answered 24/2, 2009 at 12:35 Comment(1)
Maybe what you need is a better way to keep track of versions apart from md5 the binary?Greenberg
S
3

One can disassemble the binaries and run md5 on the output

Example on MacOSX

otool -tV a.out | md5
ee2e724434a89fce96aa6b48621f7220

But, one misses out on the global data...(might be a parameter to include too)

I'm answering on the problem of md5 checking a binary...how you manage your sources and build system as others have written about is also a thing to look at

Sealskin answered 24/2, 2009 at 12:46 Comment(0)
C
2

I suspect it'll heavily depend on your toolchain and OS. For example, if one of the executable headers contains a timestamp then you're always going to find the resulting MD5 is different.

What's the end result you're trying to achieve (ie why is it so important that they're identical)..?

Cadwell answered 24/2, 2009 at 12:42 Comment(1)
I am looking for answers primarily on Visual Studio and Xcode/gcc. We rebuild our product on a daily basis. I'd like to reliably find out what dlls have changed between two builds. Can I do this by setting some flags in the compiler/linker to not use timestamp information when generating binaries?Stefansson
H
2

You can't do a md5 checksum comparison for visual studio. For a normal Release version .exe file from visual studio there will be 3 locations that change with each recompile. 2 of them are timestamps and the third is a unique GUID that visual studio uses to match versions of the .exe with helper files to ensure they are in sync.

It might be possible to write a tool that will zero out the 3 changing fields, but I'm not sure how easy it would be to parse the file.

Also, if you are calling any .dlls, if I recall right, you will get more unique identifiers in the generated file.

The Debug version is a different story. I think there are many, many more differences.

Herrmann answered 24/2, 2009 at 22:11 Comment(0)
N
1

Use an incremental build system - such as make to ensure you don't recompile your code if the source doesn't change.

It may be possible to get your compile to make identical binaries from the same source - or it may not - it depends on the compiler. Most will embed the current time in the generated binary somewhere.

Nevlin answered 24/2, 2009 at 12:39 Comment(2)
incremental builds may append additional data to your object files which will create differences. Instead you might want to use non incremental build which builds from scratch every time. Check this MS article : msdn.microsoft.com/en-us/library/4khtbfyf.aspxIinden
@Iinden That's a different meaning of incremental than mine. By my definition, an incremental build system makes no difference to the output objects: They are just only rebuilt if their sources change.Nevlin

© 2022 - 2024 — McMap. All rights reserved.