如何查找UNIX的编译时间
How to find UNIX time of compilation
我希望能够获得编译c++程序的UNIX时间(从epoch开始的秒数)。
我应该如何使用预处理器声明来做到这一点?
我认为一旦我计算出了UNIX编译时间,其他的一切都可以以此为基础。
用sscanf
、__DATE__
和/或__TIME__
填充tm
结构体的编译时间。
使用mktime
将对象转换为unix时间,得到一个time_t
。
tm compile_time;
...
sscanf(__DATE__, "%s %d %d", month, &compile_time.tm_mday, &compile_time.tm_year);
...
time_t timestamp = mktime(&compile_time);
请注意,您必须将月份名称转换为整数(0-11)。
c++提供了许多预定义的宏,包括__TIME__
__LINE__ This contain the current line number of the program when it is being compiled.
__FILE__ This contain the current file name of the program when it is being compiled.
__DATE__ This contains a string of the form month/day/year that is the date of the translation of the source file into object code.
__TIME__ This contains a string of the form hour:minute:second that is the time at which the program was compiled.
让我们看看上面所有宏的例子:
#include <iostream>
using namespace std;
int main ()
{
cout << "Value of __LINE__ : " << __LINE__ << endl;
cout << "Value of __FILE__ : " << __FILE__ << endl;
cout << "Value of __DATE__ : " << __DATE__ << endl;
cout << "Value of __TIME__ : " << __TIME__ << endl;
return 0;
}
如果我们编译并运行上面的代码,将产生以下结果:
Value of __LINE__ : 6
Value of __FILE__ : test.cpp
Value of __DATE__ : Mai 17 2014
Value of __TIME__ : 18:52:48
相关文章:
- 二叉排序树无法编译
- 编译时未启用intel oneApi CUDA支持
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- Unix 宏在使用 -std=c++0x 标志编译时变得未定义
- 双击编译C++ Unix 可执行文件不会打开现有文件以从中读取信息
- 在 Unix 中编译 c++ 时嵌套模板参数出错
- 如何在Windows中为Unix/Linux编译C++应用程序
- Unix可执行文件上的Mac分段错误,文件编译正常
- Unix 命令行在编译后无法运行程序,没有错误消息
- 为什么这会在Visual Studio 2012中编译,而不是UNIX
- 为什么这不能在我的学校 unix 系统上编译?
- 如何让我的C++程序在 Unix 上编译
- 在unix中编译dhrystone时出错
- 在Unix/Linux(Ubuntu)中编译头文件和两个.cpp文件
- 在Unix上编译所有的boost库
- 我得到编译器错误,而在unix中编译我的代码
- 如何查找UNIX的编译时间
- 从Visual Studio 2012为Windows和UNIX计算机编译项目
- 代码在Visual Studio中可以很好地编译,但在使用g++的Unix终端中就不行了.我在多维向量处得到一个错误.为
- AIX unix服务器上的c++编译错误