How can I insert compilation timestamp information into an executable I build with Visual C++ 2005? I want to be able to output something like this when I execute the progra
__TIME__
and __DATE__
can work, however there are some complications.
If you put these definitions in a .h file, and include the definitions from multiple .c/.cpp files, each file will have a different version of the date/time based on when it gets compiled. So if you're looking to use the date/time in two different places and they should always match, you're in trouble. If you're doing an incremental build, one of the files may be rebuilt while the other is not, which again results in time stamps that could be wildly different.
A slightly better approach is to make GetBuildTimeStamp() prototypes in a .h file, and put the __TIME__
and __DATE__
macros in the implementation(.c/.cpp) file. This way you can use the time stamps in multiple places in your code and they will always match. However you need to ensure that the .c/.cpp file is rebuilt every time a build is performed. If you're doing clean builds then this solution may work for you.
If you're doing incremental builds, then you need to ensure the build stamp is updated on every build. In Visual C++ you can do this with PreBuild steps - however in this case I would recommend that instead of using __DATE__
and __TIME__
in a compiled .c/.cpp file, you use a text-based file that is read at run-time during your program's execution. This makes it fast for your build script to update the timestamp (no compiling or linking required) and doesn't require your PreBuild step to understand your compiler flags or options.
Well... for Visual C++, there's a built in symbol called __ImageBase
. Specifically:
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
You can inspect that at runtime to determine the timestamp in the PE header:
const IMAGE_NT_HEADERS *nt_header= (const IMAGE_NT_HEADERS *)((char *)&__ImageBase + __ImageBase.e_lfanew);
And use nt_header->FileHeader.TimeDateStamp
to get the timestamp, which is seconds from 1/1/1970.
Though not your exact format, DATE will be of the format Mmm dd yyyy, while TIME will be of the format hh:mm:ss. You can create a string like this and use it in whatever print routine makes sense for you:
const char *buildString = "This build XXXX was compiled at " __DATE__ ", " __TIME__ ".";
(Note on another answer: TIMESTAMP only spits out the modification date/time of the source file, not the build date/time.)
Visual C++ also supports __TIMESTAMP__
which is almost exactly what you need. That being said, the tough part about build timestamps is keeping them up to date, that means compiling the file in which __TIMESTAMP__
is used on every rebuild. Not sure if there's a way to set this up in Visual C++ though.
__DATE__
__TIME__
are predefined as part of the standards for C99 so should be available to you. They run once with the preprocessor.
I think, the suggested solutions to use DATE, TIME or TIMESTAMP would be good enough. I do recommend to get a hold of a touch program to include in a pre-build step in order to touch the file that holds the use of the preprocessor variable. Touching a file makes sure, that its timestamp is newer than at the time it was last compiled. That way, the date/time in the compiled file is changed as well with each rebuild.