Embed timestamp in object code at compile time with C++
Asked Answered
V

8

44

I want to perform a printf() to display when the currently executing code was last compiled. Does C/C++ provide a macro that gives you that resolves to a timestamp during compilation?

Veritable answered 26/3, 2010 at 21:40 Comment(2)
which compiler are you using?Whorl
Useful if you want to parse it https://mcmap.net/q/196896/-convert-string-from-__date__-into-a-time_tIronwood
P
41

You could use __DATE__ and __TIME__.

Perforation answered 26/3, 2010 at 21:44 Comment(2)
@Inverse - You might modify your build process to recreate a file containing the __DATE__ and __TIME__ macros on every compilation.Rawlinson
What I do is program the makefile to touch the file containing the macro before compilation in order to force its rebuild (ideally, the file containing the macros should be such that few, if any other files depend on it.Hickman
R
32

16.8 Predefined macro names [cpp.predefined]
1 The following macro names shall be defined by the implementation:
__LINE__ The line number of the current source line (a decimal constant).
__FILE__ The presumed name of the source file (a character string literal).
__DATE__ The date of translation of the source file (a character string literal of the form "Mmm dd yyyy", where the names of the months are the same as those generated by the asctime function, and the first character of dd is a space character if the value is less than 10). If the date of translation is not available, an implementation-defined valid date is supplied.
__TIME__ The time of translation of the source file (a character string literal of the form "hh:mm:ss" as in the time generated by the asctime function). If the time of translation is not available, an implementation-defined valid time is supplied.
__STDC__ Whether __STDC__ is predefined and if so, what its value is, are implementation-defined.
__cplusplus The name __cplusplus is defined to the value 199711L when compiling a C++ translation unit.

You want __TIME__ and possibly __DATE__.

Ribosome answered 26/3, 2010 at 21:45 Comment(1)
@einpoklum: C has the same list of macros, and more, with some slight differences. For example, __STDC__ is "The integer constant 1, intended to indicate a conforming implementation.", and __cplusplus is never defined. The full list in C11 is at §6.10.8.1.Ribosome
W
8

Use the macros __DATE__ and __TIME__

Wildon answered 26/3, 2010 at 21:44 Comment(0)
W
6

If you are using gcc preprocessor then you will find what you are looking for in the __TIME__ and __DATE__ macros

Quotes from GNU C pre-processor documentation:

__DATE__

This macro expands to a string constant that describes the date on which the preprocessor is being run. The string constant contains eleven characters and looks like "Feb 12 1996". If the day of the month is less than 10, it is padded with a space on the left.

...

__TIME__

This macro expands to a string constant that describes the time at which the preprocessor is being run. The string constant contains eight characters and looks like "23:59:01".

Whorl answered 26/3, 2010 at 21:48 Comment(1)
Just to point out: this has nothing to do with GCC. This is a standard thing. So you're right that the macros are as you say but it's not just for GCC.Upbear
K
2

Use a script, or create an application, to generate a C++ source file containing the build date and time. Add this file to the build setup. The other parts of the program can reference the data in this file.

This technique is also useful for embedding a version number into the program. The build process can control the version number.

Kotta answered 27/3, 2010 at 0:23 Comment(0)
N
0

It looks like compilers are expected to provide a __TIMESTAMP__ macro, which ought to work for your purposes.

Nabonidus answered 26/3, 2010 at 21:44 Comment(1)
From MSDN: __TIMESTAMP__ gives you the date and time of last edit (!) made to file (not the compilation time).Turney
S
0

I am using this to send a message to DbgView:

    OutputDebugStringA(("ATTACHED VERSION: "+ string(__DATE__) + " " + string(__TIME__)).c_str());
Salley answered 20/12, 2016 at 17:3 Comment(1)
Just a hint for better Performance:"ATTACHED VERSION: " __DATE__ " " __TIME__ . works too.Cantle
B
-1

You can add below string to CMakeLists.txt

string(TIMESTAMP COMPILE_TIME %Y%m%d-%H%M%S)

and

you need to add below string to your "ProjectName.h.in" file

#define TIMESTAMP @COMPILE_TIME@

then when you compile your project, the below string will be written in your "ProjectName.h"

#define TIMESTAMP 20230220-203532
Bushman answered 26/5, 2023 at 7:21 Comment(2)
OP never said anything about using CMake ... Also, you'd need more in your cmake than what you posted, and that would be the last time CMake ran, not when it was compiled.Cosmonaut
emmm。May you write more detail about "you'd need more in your cmake than what you posted"?@CosmonautBushman

© 2022 - 2024 — McMap. All rights reserved.