我怎样才能知道我的源代码的编译日期?
How can I find out the date of when my source code was compiled?
是否可以存储和显示我的项目的编译日期?
我想在程序启动时打印这个日期,以便知道使用的是哪个版本。目前都是手工操作,比较麻烦
我正在使用 Visual Studio 2010.
C++ 指定有一个名为 __DATE__
的特殊预处理器宏,它是编译发生时的字符串文字。还有一个相应的 __TIME__
宏。
你可以这样使用:
const std::string compilation_date = __DATE__;
const std::string compilation_time = __TIME__;
...
std::cout << "This source file was compiled on date " << compilation_date
<< " and at the time " << compilation_time << '\n';
您可以使用 __DATE__
和 __TIME__
macros - see "Predefined macros" here。
作为示例,如下所示:
#include <iostream>
int main()
{
using namespace std;
cout << "Compiled on: " << __DATE__ << endl;
cout << "Compiled at: " << __TIME__ << endl;
}
您可以根据需要修改消息并使用。
您甚至可以考虑将其构建为更大的宏或命名变量。
#include <iostream>
const char* const COMPILED = __DATE__ " @ " __TIME__;
int main()
{
using namespace std;
cout << "Compiled: " << COMPILED << endl;
}
如果 "version" 信息与源代码控制位置或数据(例如提交编号)相关联,也可以通过构建步骤或命令行将该数据包含在构建中定义某种。您还应该考虑增量构建对日期和时间的影响。我假设 "release" 构建不是基于增量的,或者如果是这样,包含日期和时间数据的文件中有 "touch"。
有一个宏叫做
__DATE__
解析为类似 "Apr 1 2015" 的内容。一个人可以使用它。这是一个标准的预定义宏。
__ 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. If GCC cannot determine the current date, it will emit a warning message (once per compilation) and DATE will expand to "??? ?? ????". https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html
但是,此解决方案缺少格式。当然,您可以解析它,但也许有更简单、更像 C++ 的解决方案。
这有两个部分。第一个(答案中已经提到)是使用 __DATE__
。不幸的是,这只会告诉您该翻译单元的编译日期。如果您想要最后 Visual Studio 构建的日期,则需要强制重建包含 __DATE__
的翻译单元
一个简单的解决方案是始终更新该翻译单元的文件时间。假设您想要 Joachim 的解决方案,那么您可以创建一个单独的 builddate.cpp 文件
const std::string compilation_date = __DATE__;
const std::string compilation_time = __TIME__;
在 post 构建步骤中,调用 copy /b builddate.cpp+,,
。这意味着在每次构建之后,builddate.cpp 文件变得比可执行文件更新,并将在下一次构建时重新编译。
在 Linux 上,您将为此使用 touch
。
你的问题表明你没有使用版本控制系统。你应该,没有像 "my project too small I'll do it later when will work on something bigger" 或 "it is too complex" 这样的借口。 VCS 是每个开发人员都必须使用的,当您开始使用它时,您将无法想象没有它之前的生活。因此,当您开始使用 VCS 时,您的问题将是 "how to put comit or tag version into source?" 例如,在 svn 上,您可以使用:
const char *version = "$Id:$";
svn 会将其更改为当前提交版本。对于其他 VCS 系统,解决方案可能不同但接近,因为这个问题很常见。
是否可以存储和显示我的项目的编译日期?
我想在程序启动时打印这个日期,以便知道使用的是哪个版本。目前都是手工操作,比较麻烦
我正在使用 Visual Studio 2010.
C++ 指定有一个名为 __DATE__
的特殊预处理器宏,它是编译发生时的字符串文字。还有一个相应的 __TIME__
宏。
你可以这样使用:
const std::string compilation_date = __DATE__;
const std::string compilation_time = __TIME__;
...
std::cout << "This source file was compiled on date " << compilation_date
<< " and at the time " << compilation_time << '\n';
您可以使用 __DATE__
和 __TIME__
macros - see "Predefined macros" here。
作为示例,如下所示:
#include <iostream>
int main()
{
using namespace std;
cout << "Compiled on: " << __DATE__ << endl;
cout << "Compiled at: " << __TIME__ << endl;
}
您可以根据需要修改消息并使用。
您甚至可以考虑将其构建为更大的宏或命名变量。
#include <iostream>
const char* const COMPILED = __DATE__ " @ " __TIME__;
int main()
{
using namespace std;
cout << "Compiled: " << COMPILED << endl;
}
如果 "version" 信息与源代码控制位置或数据(例如提交编号)相关联,也可以通过构建步骤或命令行将该数据包含在构建中定义某种。您还应该考虑增量构建对日期和时间的影响。我假设 "release" 构建不是基于增量的,或者如果是这样,包含日期和时间数据的文件中有 "touch"。
有一个宏叫做
__DATE__
解析为类似 "Apr 1 2015" 的内容。一个人可以使用它。这是一个标准的预定义宏。
__ 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. If GCC cannot determine the current date, it will emit a warning message (once per compilation) and DATE will expand to "??? ?? ????". https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html
但是,此解决方案缺少格式。当然,您可以解析它,但也许有更简单、更像 C++ 的解决方案。
这有两个部分。第一个(答案中已经提到)是使用 __DATE__
。不幸的是,这只会告诉您该翻译单元的编译日期。如果您想要最后 Visual Studio 构建的日期,则需要强制重建包含 __DATE__
一个简单的解决方案是始终更新该翻译单元的文件时间。假设您想要 Joachim 的解决方案,那么您可以创建一个单独的 builddate.cpp 文件
const std::string compilation_date = __DATE__;
const std::string compilation_time = __TIME__;
在 post 构建步骤中,调用 copy /b builddate.cpp+,,
。这意味着在每次构建之后,builddate.cpp 文件变得比可执行文件更新,并将在下一次构建时重新编译。
在 Linux 上,您将为此使用 touch
。
你的问题表明你没有使用版本控制系统。你应该,没有像 "my project too small I'll do it later when will work on something bigger" 或 "it is too complex" 这样的借口。 VCS 是每个开发人员都必须使用的,当您开始使用它时,您将无法想象没有它之前的生活。因此,当您开始使用 VCS 时,您的问题将是 "how to put comit or tag version into source?" 例如,在 svn 上,您可以使用:
const char *version = "$Id:$";
svn 会将其更改为当前提交版本。对于其他 VCS 系统,解决方案可能不同但接近,因为这个问题很常见。