visual-studio 在 Visual Studio C++ 构建中打印日期和时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/602936/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:13:35  来源:igfitidea点击:

Print Date and Time In Visual Studio C++ build?

c++visual-studiodatetimecompiler-construction

提问by Brock Woolf

How would I print the date and time for the purposes of the build. Ie: When the console for my application starts up I want to do this:

为了构建的目的,我将如何打印日期和时间。即:当我的应用程序的控制台启动时,我想这样做:

Binary Build date: 03/03/2009 @ 10:00AM
Binary Build date: 03/03/2009 @ 10:00AM

I think this would be a super useful function for all applications to have behind the scenes for programmers, especially in a team environment.

我认为这对于所有应用程序在幕后为程序员提供的超级有用的功能,尤其是在团队环境中。

Is there a simple way to do this using Visual Studio 2008 in C++. Thanks.

有没有一种简单的方法可以在 C++ 中使用 Visual Studio 2008 来做到这一点。谢谢。

回答by Virne

Use preprocessor's __DATE__and __TIME__.

使用预处理器的__DATE____TIME__.

printf("Binary build date: %s @ %s\n", __DATE__, __TIME__);

For making sure that cpp file that contains this code is really compiled, I use touch-utility for file as a pre-build step: touch file.cpp

为了确保真正编译包含此代码的 cpp 文件,我使用 touch-utility for file 作为预构建步骤: touch file.cpp

Touch.bat:

触摸.bat:

@copy nul: /b +%1 tmp.$$$
@move tmp.$$$ %1

回答by dirkgently

You can use the macros __TIME__and __DATE__. Note the double underscores. These are unrolled at compile time and hence you will get the last compile time saved in your file(s).

您可以使用宏__TIME____DATE__. 注意双下划线。这些在编译时展开,因此您将获得保存在文件中的最后编译时间。

回答by dirkgently

Note that the time and date macros only work as desired if the particular file containing them is guaranteed to be compiled during every build.

请注意,如果确保在每次构建期间编译包含时间和日期宏的特定文件,则时间和日期宏仅按需要工作。

回答by saw-lau

One way of doing this would be using the built-in __DATE__and __TIME__macros. From MSDN (for VS 2005):

一种方法是使用内置宏__DATE____TIME__宏。来自 MSDN(对于 VS 2005):

__DATE__: The compilation date of the current source file. The date is a string literal of the form Mmm dd yyyy. The month name Mmm is the same as for dates generated by the library function asctime declared in TIME.H.

__TIME__: The most recent compilation time of the current source file. The time is a string literal of the form hh:mm:ss.

__DATE__: 当前源文件的编译日期。日期是格式为 Mmm dd yyyy 的字符串文字。月份名称 Mmm 与 TIME.H 中声明的库函数 asctime 生成的日期相同。

__TIME__: 当前源文件的最近编译时间。时间是 hh:mm:ss 形式的字符串文字。

回答by User

Similar to Virne's answer I created a simple header file called "BuildDate.h" with the following contents:

与 Virne 的回答类似,我创建了一个名为“BuildDate.h”的简单头文件,其中包含以下内容:

#define BUILD_DATE __DATE__ " " __TIME__

I touch the file using GnuWin32touch command in my pre-build event:

我在预构建事件中使用GnuWin32touch 命令触摸文件:

touch.exe BuildDate.h

Then I include the header file in any code where I want access to the BUILD_DATEstring. E.g.:

然后我将头文件包含在我想要访问BUILD_DATE字符串的任何代码中。例如:

#include "BuildDate.h"
...
logger->Log("Build Date: " BUILD_DATE);