5

I have a question about the source-code binary on Windows.

#include <stdio.h>

int main()
{
    printf("Hello, world!\n");

    return 0;
}

The same source code, I compiled twice on Windows (VS 2008 Cmmand Prompt: "CL"), but I got different binaries.

cl new.cpp

Can you guys tell me why, and how to avoid that?

durron597
  • 31,968
  • 17
  • 99
  • 158
Peter Lee
  • 12,931
  • 11
  • 73
  • 100
  • 1
    See http://stackoverflow.com/questions/1221185/identical-build-on-different-systems and http://stackoverflow.com/questions/1277877/gcc-compiled-binaries-w-different-sizes - basically no compiler guarantees binaries will be identical. –  Jul 08 '10 at 16:46
  • 1
    Why do you need the binaries to be identical? – jalf Jul 08 '10 at 17:08

3 Answers3

4

The timestamp is part of PE format. You'll always get different values regardless if compiling as release or not.

josuegomes
  • 451
  • 4
  • 15
  • How can I avoid that? Is it possible to remove the timestamp? Because in Linux, if I use g++/gcc, the final binaries are the same. – Peter Lee Jul 08 '10 at 16:34
  • 2
    @Peter: ELF (Linux's executable format) does not have a timestamp field. Why does it matter if the output is different? – Billy ONeal Jul 08 '10 at 16:56
  • @Billy: You are right that ELF might not have the timestamp field. It's just very annoying that same source code different binaries. – Peter Lee Jul 08 '10 at 17:25
  • @Peter: Why is that annoying? – Billy ONeal Jul 08 '10 at 17:32
  • The only problem I see from this is if you're trying to debug a crash dump (aka core dumps). Windows debugging tools associate the dump with a binary checksum, so you can't debug the dump with a different binary, even if there is no code changes. Some tools can update the checksum in the dump file but usually this is not a good idea. BTW, I have no idea how does this work on Unix world. – josuegomes Jul 09 '10 at 18:26
1

Did you compile as release? Debug has timestamps built in which can change your exe per compile

Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
  • Compiling as directed above yielded a difference of exactly 1 byte. The LSB of the embedded timestamp. – mocj Jul 08 '10 at 16:28
1

I googled, and found a mid-way solution:

DUMPBIN  /RAWDATA  MyApp.EXE > first.txt
DUMPBIN  /RAWDATA  MyApp.EXE > second.txt

http://support.microsoft.com/kb/164151 How to compare binary images of the same project builds

Peter Lee
  • 12,931
  • 11
  • 73
  • 100