Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed timestamp in object code at compile time with C++

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?

like image 289
theactiveactor Avatar asked Mar 26 '10 21:03

theactiveactor


3 Answers

You could use __DATE__ and __TIME__.

like image 104
dalle Avatar answered Oct 15 '22 01:10

dalle


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

like image 33
GManNickG Avatar answered Oct 15 '22 03:10

GManNickG


Use the macros __DATE__ and __TIME__

like image 8
FRotthowe Avatar answered Oct 15 '22 03:10

FRotthowe