我怎样才能知道我的源代码被编译的日期? [英] How can I find out the date of when my source code was compiled?

查看:76
本文介绍了我怎样才能知道我的源代码被编译的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以存储和显示项目编译的日期?

Is it possible to store and display the date of when my project was compiled?

我想在程序启动时打印此日期,以便知道使用哪个版本.目前,我正在手动执行此操作,这非常麻烦.

I'd like to print this date when the program starts in order to know which version is used. Currently, I am doing this by hand, which is rather cumbersome.

我正在使用Visual Studio 2010.

I am using Visual Studio 2010.

推荐答案

您可以使用 __ DATE __ __ TIME __ .

作为示例,如下所示:

#include <iostream>
int main()
{
    using namespace std;
    cout << "Compiled on: " << __DATE__ << endl;
    cout << "Compiled at: " << __TIME__ << endl;
}

您将修改消息并根据需要使用.

You would modify the messages and use according to your needs.

您甚至可以将其构建为更大的宏或命名变量.

You could even look to building it up into a larger macro or named variable.

#include <iostream>
const char* const COMPILED = __DATE__ " @ " __TIME__;
int main()
{
    using namespace std;
    cout << "Compiled: " << COMPILED << endl;
}

如果版本"信息与源代码控制位置或数据(例如提交编号)相关联,则可能是通过某种构建步骤或某种命令行定义将数据包含在构建中的想法.您还应该考虑增量构建对日期和时间的影响.我假设发布"版本不是基于增量的,如果是这样,则包含日期和时间数据的文件上存在接触".

If the "version" information is tied to a source control location or data (such as a commit number), it may also be an idea to include that data in the build via a build step or command line define of some sort. You should also consider the effect of incremental builds on the date and time. I'm assuming the "release" builds are not incremental based, or if so, there is "touch" on the file containing the date and time data.

这篇关于我怎样才能知道我的源代码被编译的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆