C/C++:__LINE__;__FILE__;__DATE__;__TIME__;__FUNCTION__等
测试环境:CentOS
[mytmp@localhost ~]$ uname -a
Linux localhost.localdomain 2.6.18-371.el5 #1 SMP Thu Sep 5 21:21:44 EDT 2013 x86_64 x86_64 x86_64 GNU/Linux
[mytmp@localhost ~]$ gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
__LINE__:指明当前语句所在的行号
#include
#include
void foo()
{
printf("b:line position : %d\n", __LINE__);
}
int main()
{
printf("a:line position : %d\n", __LINE__);
foo();
printf("c:line position : %d\n", __LINE__);
printf("d:line position : %d\n", __LINE__);
#line 200
printf("e:line position : %d\n", __LINE__);
#line 300
printf("f:line position : %d\n", __LINE__);
return 0;
}
[mytmp@localhost ~]$ ./main
a:line position : 11
b:line position : 6
c:line position : 13
d:line position : 14
e:line position : 200
f:line position : 300
其中我使用了#line,这个的作用是指明下一行的行号(set下行号)。
注意__LINE__不是一个字符串,是一个整型(即,被替换后不是"3",而是单纯的就是一个数字3)。
__FILE__:指明当前语句所在的源文件
#include
#include
int main()
{
printf("__FILE__ is : %s\n", __FILE__);
return 0;
}
[mytmp@localhost ~]$ ./main
__FILE__ is : main.c
我们可以使用预处理来看:
[mytmp@localhost ~]$ gcc -E main.c >> main.i
上述命令是将main.c进行预编译处理(这一步一般是把宏展开),然后将输出重定向到main.i文件中。
# 1 "main.c"
# 1 ""
# 1 ""
# 1 "main.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 28 "/usr/include/stdio.h" 3 4
……
# 977 "/usr/include/stdlib.h" 3 4
# 3 "main.c" 2
int main()
{
printf("__FILE__ is : %s\n", "main.c");
return 0;
}
__DAET__:指明编译日期
__TIME__:指明编译时间
#include
#include
int main()
{
printf("__DATE__ is %s\n", __DATE__);
printf("__TIME__ is %s\n", __TIME__);
return 0;
}
[mytmp@localhost ~]$ ./main
__DATE__ is May 3 2017
__TIME__ is 19:18:42
#include
#include
void foo()
{
printf("in function-foo:__FUNCTION__ is :%s\n", __FUNCTION__);
}
int main()
{
printf("in function-main:__FUNCTION__ is :%s\n", __FUNCTION__);
foo();
return 0;
}
[mytmp@localhost ~]$ ./main
in function-main:__FUNCTION__ is :main
in function-foo:__FUNCTION__ is :foo
#include
#include
void foo()
{
printf("in function-foo:__func__ is :%s\n", __func__);
}
int main()
{
printf("in function-main:__func__ is :%s\n", __func__);
foo();
return 0;
}
[mytmp@localhost ~]$ ./main
in function-main:__func__ is :main
in function-foo:__func__ is :foo
__func__很类似宏表达式,其会报告未修饰过的,正在被访问的函数名。
__func__不是一个宏,预处理器对当此函数一无所知,__func__是作为一个隐式声明的常量字符数组来实现的:
static const char __func__[] = "function-name";
大多数编译器提供商使用__FUNCTION__,其一般是一个宏,定义为__func__;GNU不推荐使用__FUNCTION__。
(PS:网上是两种说法,未考证。)
额,看下下面的代码吧:
#include
#include
int main()
{
printf("__LINE__ is %d\n", __LINE__);
printf("__FILE__ is %s\n", __FILE__);
printf("__DATE__ is %s\n", __DATE__);
printf("__TIME__ is %s\n", __TIME__);
printf("__VERSION__ is %s\n", __VERSION__);
printf("__func__ is %s\n", __func__);
printf("__FUNCTION__ is %s\n", __FUNCTION__);
printf("__BASE_FILE__ is %s\n", __BASE_FILE__);
return 0;
}
int main()
{
printf("__LINE__ is %d\n", 6);
printf("__FILE__ is %s\n", "main.c");
printf("__DATE__ is %s\n", "May 4 2017");
printf("__TIME__ is %s\n", "01:42:56");
printf("__VERSION__ is %s\n", "4.1.2 20080704 (Red Hat 4.1.2-54)");
printf("__func__ is %s\n", __func__);
printf("__FUNCTION__ is %s\n", __FUNCTION__);
printf("__BASE_FILE__ is %s\n", "main.c");
return 0;
}
输出如下:
[mytmp@localhost ~]$ ./main
__LINE__ is 6
__FILE__ is main.c
__DATE__ is May 4 2017
__TIME__ is 01:42:37
__VERSION__ is 4.1.2 20080704 (Red Hat 4.1.2-54)
__func__ is main
__FUNCTION__ is main
__BASE_FILE__ is main.c
注:
__LINE__以及__FILE__在日志中的使用:
http://blog.csdn.net/test1280/article/details/71155800
资料来源:
1.https://msdn.microsoft.com/en-us/library/b0084kay.aspx
2.http://www.cnblogs.com/lixiaohui-ambition/archive/2012/08/21/2649052.html
3.http://blog.csdn.net/cjsycyl/article/details/8690201
4.http://blog.csdn.net/tomtc123/article/details/8875468
5.http://blog.csdn.net/hj74535099/article/details/40351743
6.http://blog.chinaunix.net/uid-27183448-id-3383073.html
7.http://www.cppblog.com/byc/archive/2010/11/09/133080.html
8.http://blog.csdn.net/fg313071405/article/details/8306180
9.http://blog.csdn.net/dafan/article/details/5781491
10.http://www.cndev.org/forum/msg?pid=635846