Output compile time stamp in Visual C++ executable?

前端 未结 6 955
借酒劲吻你
借酒劲吻你 2021-01-04 02:08

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

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-04 02:44

    __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.

提交回复
热议问题