Print Date and Time In Visual Studio C++ build?

后端 未结 5 1118
故里飘歌
故里飘歌 2020-12-17 23:49

How would I print the date and time for the purposes of the build. Ie: When the console for my application starts up I want to do this:

Binary B         


        
相关标签:
5条回答
  • 2020-12-17 23:55

    One way of doing this would be using the built-in __DATE__ and __TIME__ macros. From MSDN (for VS 2005):

    __DATE__: The compilation date of the current source file. The date is a string literal of the form Mmm dd yyyy. The month name Mmm is the same as for dates generated by the library function asctime declared in TIME.H.

    __TIME__: The most recent compilation time of the current source file. The time is a string literal of the form hh:mm:ss.

    0 讨论(0)
  • 2020-12-17 23:58

    Similar to Virne's answer I created a simple header file called "BuildDate.h" with the following contents:

    #define BUILD_DATE __DATE__ " " __TIME__
    

    I touch the file using GnuWin32 touch command in my pre-build event:

    touch.exe BuildDate.h
    

    Then I include the header file in any code where I want access to the BUILD_DATE string. E.g.:

    #include "BuildDate.h"
    ...
    logger->Log("Build Date: " BUILD_DATE);
    
    0 讨论(0)
  • 2020-12-18 00:10

    Use preprocessor's __DATE__ and __TIME__.

    printf("Binary build date: %s @ %s\n", __DATE__, __TIME__);
    

    For making sure that cpp file that contains this code is really compiled, I use touch-utility for file as a pre-build step: touch file.cpp

    Touch.bat:

    @copy nul: /b +%1 tmp.$$$
    @move tmp.$$$ %1
    
    0 讨论(0)
  • 2020-12-18 00:10

    You can use the macros __TIME__ and __DATE__. Note the double underscores. These are unrolled at compile time and hence you will get the last compile time saved in your file(s).

    0 讨论(0)
  • 2020-12-18 00:13

    Note that the time and date macros only work as desired if the particular file containing them is guaranteed to be compiled during every build.

    0 讨论(0)
提交回复
热议问题