<p>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 program:</p>

<blockquote>
  <p>This build XXXX was compiled at dd-mm-yy, hh:mm.</p>
</blockquote>

<p>where date and time reflect the time when the project was built. They should not change with each successive call of the program, unless it's recompiled.</p>

[{'answer_id': 25780, 'author': 'sparkes', 'author_id': 269, 'author_profile': 'https://Stackoverflow.com/users/269', 'pm_score': 3, 'selected': False, 'text': '<pre><code>__DATE__ \n__TIME__\n</code></pre>\n\n<p>are predefined as part of the standards for C99 so should be available to you.  They run once with the preprocessor.</p>\n'}, {'answer_id': 25802, 'author': 'Eric Scrivner', 'author_id': 2594, 'author_profile': 'https://Stackoverflow.com/users/2594', 'pm_score': 0, 'selected': False, 'text': "<p>Visual C++ also supports <code>__TIMESTAMP__</code> 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 <code>__TIMESTAMP__</code> is used on every rebuild.  Not sure if there's a way to set this up in Visual C++ though.</p>\n"}, {'answer_id': 25813, 'author': 'Jim Buck', 'author_id': 2666, 'author_profile': 'https://Stackoverflow.com/users/2666', 'pm_score': 5, 'selected': False, 'text': '<p>Though not your exact format, <strong>DATE</strong> will be of the format Mmm dd yyyy, while <strong>TIME</strong> 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:</p>\n\n<pre><code>const char *buildString = "This build XXXX was compiled at " __DATE__ ", " __TIME__ ".";\n</code></pre>\n\n<p>(Note on another answer: <strong>TIMESTAMP</strong> only spits out the modification date/time of the source file, not the build date/time.)</p>\n'}, {'answer_id': 25869, 'author': 'HS.', 'author_id': 1398, 'author_profile': 'https://Stackoverflow.com/users/1398', 'pm_score': 1, 'selected': False, 'text': '<p>I think, the suggested solutions to use <strong>DATE</strong>, <strong>TIME</strong> or <strong>TIMESTAMP</strong> 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.</p>\n'}, {'answer_id': 25887, 'author': 'Joe Schneider', 'author_id': 1541, 'author_profile': 'https://Stackoverflow.com/users/1541', 'pm_score': 3, 'selected': False, 'text': "<p><code>__TIME__</code> and <code>__DATE__</code> can work, however there are some complications. </p>\n\n<p>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.</p>\n\n<p>A slightly better approach is to make GetBuildTimeStamp() prototypes in a .h file, and put the <code>__TIME__</code> and <code>__DATE__</code> 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.</p>\n\n<p>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 <code>__DATE__</code> and <code>__TIME__</code> 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.</p>\n"}, {'answer_id': 26297, 'author': 'Mat Noguchi', 'author_id': 1799, 'author_profile': 'https://Stackoverflow.com/users/1799', 'pm_score': 3, 'selected': False, 'text': "<p>Well... for Visual C++, there's a built in symbol called <code>__ImageBase</code>. Specifically:</p>\n\n<p><code>EXTERN_C IMAGE_DOS_HEADER __ImageBase;</code></p>\n\n<p>You can inspect that at runtime to determine the timestamp in the PE header:</p>\n\n<p><code>const IMAGE_NT_HEADERS *nt_header= (const IMAGE_NT_HEADERS *)((char *)&amp;__ImageBase + __ImageBase.e_lfanew);</code></p>\n\n<p>And use <code>nt_header-&gt;FileHeader.TimeDateStamp</code> to get the timestamp, which is seconds from 1/1/1970.</p>\n"}]
['https://Stackoverflow.com/questions/25771', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']