gpt4 book ai didi

c++ - 是否可以在编译时获取时间(当天)和日期?

转载 作者:可可西里 更新时间:2023-11-01 12:41:08 29 4
gpt4 key购买 nike

有什么可能在编译时知道时间和日期 (BUILDTIME),以便以可移植的方式在二进制文件(可执行文件/库)中包含有关它们创建时刻的信息?

我们目前有一个使用 sh.exe 的解决方案,并且需要在 Windows 下安装 msys,但我想知道如果没有它是否可行。

搜索诸如“build time/date”、“compile time/date”之类的东西没有得到任何相关结果。

编辑:

当我开始了解 __TIME__ 时,可能会发现之前有人问过这个问题:Recording the time when you compile a source

最佳答案

标准宏 __DATE____TIME__ 完成这项工作。

请注意,这将为您提供使用它们的文件的编译日期。不是链接日期。因此,您必须在每次构建文件时触摸该文件,或者在 MSVC 中执行预构建步骤。

The C99 standard says :

6.10.8 Predefined macro names

The following macro names shall be defined by the implementation:

  • __DATE__: The date of translation of the preprocessing translation unit: 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 shall be supplied.
  • __TIME__: The time of translation of the preprocessing translation unit: 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 shall be supplied.

我在这里复制了 C99 文本,但是这些宏比 C99 旧得多...我没能找到旧 C 的标准文本...

关于c++ - 是否可以在编译时获取时间(当天)和日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17292873/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com