c++visual-studiodatetimecompilationmacros

How to redefine (override) C++ __DATE__ and __TIME__ predefined macros in Visual Studio?


Is there any possibility to redefine __DATE__ and __TIME__ predefined C++ macros (give them other than default values) in Microsoft Visual Studio?

I tried compiler option /D "__DATE__=\"Feb 10 2021\"" but I'm getting:

pch.cpp : warning C4117: macro name '__DATE__' is reserved, '#define' ignored

and no effect. Any idea except modification of the code (or a confirmation 'it is not possible')?

Reason: C++ project, that I have, has its builds versioned by date macro values (every build gets its version from __DATE__/__TIME__ values). I need to simulate an "older" build - basically, to cheat this versioning system. I don't need to change the macro value format. I also know about an option to have another user-defined macro mentioned by @Jabberwocky.


Solution

  • No you can't. But you don't need to either. Don't use __DATE__ but e.g. BUILD_DATE and add this:

    #ifndef BUILD_DATE
    #define BUILD_DATE __DATE__
    #endif
    

    And compile with:

    /D "BUILD_DATE=\"Feb 10 2021\"" 
    

    Then you get the exact behaviour you want.

    But if you really can't replace __DATE__ with something of your own as suggested, you're out of luck.