如何获取编译 C++ 文件的日期和时间?

How can I get the date and time a C++ file was compiled?

有什么方法可以使用编译 C++ 源文件的日期和时间,以便在代码中使用它,例如:

cout << "this file was compiled on " << CompileDate << " at " << CompileTime" << endl;

如何计算 CompileDateCompileTime

如果您使用的是 gcc 预处理器,那么您将在 TIMEDATE 宏 [=12= 中找到您要查找的内容]

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.

cout << __DATE__ << endl;
cout << __TIME__ << endl;

将打印出编译日期和时间。