Jarek C
Jarek C

Reputation: 1261

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.

Upvotes: 0

Views: 1698

Answers (2)

phuclv
phuclv

Reputation: 41794

One way is to fake the time that's perceived by MSVC cl.exe. This can be done by running the compiler inside a virtual machine (like Windows Sandbox) with a different time, or use some 3rd party solution like RunAsDate to change the time of a process:


But probably what you want is reproducible builds. It's very important nowadays so lots of projects are moving toward it including Windows. There are /experimental:deterministic, /d1nodatetime and /Brepro flags in MSVC for this

See also

Upvotes: 2

Jabberwocky
Jabberwocky

Reputation: 50775

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.

Upvotes: 2

Related Questions