在编译时使用 C++ 在目标代码中嵌入时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2526933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Embed timestamp in object code at compile time with C++
提问by theactiveactor
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?
我想printf()
在上次编译当前执行的代码时执行显示。C/C++ 是否提供了一个宏,让您在编译期间解析时间戳?
采纳答案by dalle
You could use __DATE__
and __TIME__
.
你可以使用__DATE__
和__TIME__
。
回答by GManNickG
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.
16.8 预定义宏名称[cpp.predefined]
1 以下宏名称应由实现定义:__LINE__
当前源行的行号(十进制常量)。__FILE__
源文件的假定名称(字符串文字)。__DATE__
源文件的翻译日期(“Mmm dd yyyy”形式的字符串字面量,其中月份名称与asctime函数生成的月份名称相同,dd的第一个字符为空格字符,如果值小于 10)。如果翻译日期不可用,则提供实现定义的有效日期。__TIME__
源文件的翻译时间(形式为“hh:mm:ss”的字符串文字,如 asctime 函数生成的时间)。如果翻译时间不可用,则提供实现定义的有效时间。__STDC__
是否__STDC__
是预定义的,如果是,它的值是什么,是实现定义的。在编译 C++ 翻译单元时__cplusplus
,名称__cplusplus
被定义为值 199711L。
You want __TIME__
and possibly __DATE__
.
你想要__TIME__
并且可能__DATE__
。
回答by FRotthowe
Use the macros __DATE__
and __TIME__
使用宏__DATE__
和__TIME__
回答by hhafez
If you are using gcc preprocessor then you will find what you are looking for in the __TIME__
and __DATE__
macros
如果您使用的是 gcc 预处理器,那么您将在__TIME__
和__DATE__
宏中找到您要查找的内容
Quotes from GNU C pre-processor documentation:
来自 GNU C 预处理器文档的引用:
__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".
__日期__
这个宏扩展为一个字符串常量,它描述了预处理器运行的日期。字符串常量包含 11 个字符,看起来像“Feb 12 1996”。如果一个月中的第几天小于 10,则在左侧填充一个空格。
...
__时间__
这个宏扩展为一个字符串常量,它描述了预处理器运行的时间。字符串常量包含八个字符,看起来像“23:59:01”。
回答by Thomas Matthews
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.
使用脚本或创建应用程序来生成包含构建日期和时间的 C++ 源文件。将此文件添加到构建设置中。程序的其他部分可以引用此文件中的数据。
This technique is also useful for embedding a version number into the program. The build process can control the version number.
这种技术对于将版本号嵌入到程序中也很有用。构建过程可以控制版本号。
回答by jamacoe
I am using this to send a message to DbgView:
我正在使用它向 DbgView 发送消息:
OutputDebugStringA(("ATTACHED VERSION: "+ string(__DATE__) + " " + string(__TIME__)).c_str());
回答by jwismar
It looks like compilers are expected to provide a __TIMESTAMP__
macro, which ought to work for your purposes.
看起来编译器__TIMESTAMP__
应该提供一个宏,它应该适合您的目的。