__FILE__,__LINE__,FUNCTION__实现代码跟踪调试(linux下c语言编程
__FILE__,__LINE__,FUNCTION__实现代码跟踪调试(linux下c语言编程http://bbs.linux-cn.com/archiver/tid-13793.html (come from)[color=#295200][size=14pt][b]__FILE__,__LINE__,FUNCTION__实现代码跟踪调试(linux下c语言编程 )[/b][/size][/c
·
__FILE__,__LINE__,FUNCTION__实现代码跟踪调试(linux下c语言编程
http://bbs.linux-cn.com/archiver/tid-13793.html (come from)
root@xuanfei-desktop:~/cpropram/2# cat global.h //头文件
#ifndef CLOBAL_H
#define GLOBAL_H
#include <stdio.h>
int funca(void);
int funcb(void);
#endif
root@xuanfei-desktop:~/cpropram/2# cat funca.c //函数a
#include "global.h"
int funca(void)
{
printf ("this is function/n");
return 0;
}
root@xuanfei-desktop:~/cpropram/2# cat funcb.c //函数b
#include "global.h"
int funcb(void)
{
printf ("this is function/n");
return 0;
}
root@xuanfei-desktop:~/cpropram/2# gcc -Wall funca.c funcb.c main.c //联合编译
root@xuanfei-desktop:~/cpropram/2# ./a.out //运行
this is main
this is function
this is main
this is function
this is main
相同结果很难让人看出那里出错,下面我们用用 __FILE__,__LINE__,__FUNCTION__加入代码,看看有什么区别吗.
把 __FILE__,__LINE__,__FUNCTION__加入到mail.c中
root@xuanfei-desktop:~/cpropram/2# cat main.c
#include "global.h"
int main(int argc, char **argv)
{
printf("%s(%d)-%s: this is main/n",__FILE__,__LINE__,__FUNCTION__);
funca();
printf("%s(%d)-%s: this is main/n",__FILE__,__LINE__,__FUNCTION__);
funcb();
printf("%s(%d)-%s: this is main/n",__FILE__,__LINE__,__FUNCTION__);
return 0;
}
root@xuanfei-desktop:~/cpropram/2# gcc -Wall funca.c funcb.c main.c
root@xuanfei-desktop:~/cpropram/2# ./a.out
main.c(4)-main: this is main
this is function
main.c(6)-main: this is main
this is function
main.c(8)-main: this is main
上面的结果main.c(4)-main:this is main 表示在mian.c源代码的第四行main函数里边打印出来的 this is main
那样的话就很方便的让程序员对自己的程序进行排错!
为了更方便的使用它我们可以通过在global.h代码中进行宏定义
root@xuanfei-desktop:~/cpropram/2# cat global.h
#ifndef CLOBAL_H
#define GLOBAL_H
#include <stdio.h>
int funca(void);
int funcb(void);
#define DEBUGFMT "%s(%d)-%s"
#define DEBUGARGS __FILE__,__LINE__,__FUNCTION__
#endif
root@xuanfei-desktop:~/cpropram/2# cat funca.c
#include "global.h"
int funca(void)
{
printf (DEBUGFMT " this is function/n",DEBUGARGS);
return 0;
}
root@xuanfei-desktop:~/cpropram/2# cat funcb.c
#include "global.h"
int funcb(void)
{
printf (DEBUGFMT " this is function/n",DEBUGARGS);
return 0;
}
root@xuanfei-desktop:~/cpropram/2# cat main.c
#include "global.h"
int main(int argc, char **argv)
{
printf(DEBUGFMT "this is main/n", DEBUGARGS);
funca();
printf(DEBUGFMT "this is main/n", DEBUGARGS);
funcb();
printf(DEBUGFMT "this is main/n", DEBUGARGS);
return 0;
}
root@xuanfei-desktop:~/cpropram/2# gcc -Wall funca.c funcb.c main.c
root@xuanfei-desktop:~/cpropram/2# ./a.out
main.c(4)-mainthis is main
funca.c(4)-funca this is function
main.c(6)-mainthis is main
funcb.c(4)-funcb this is function
main.c(8)-mainthis is main
root@xuanfei-desktop:~/cpropram/2#
这就是通过定义__FILE__,__LINE__,FUNCTION__的宏来简单实现代码的跟踪调试:)
如上内容根据周立发linux视频教程所做的笔录,为了方便大家理解,建议大家可以到下面的连接下载观看。
[size=2][color=#0001ff][url=http://blog.chinaunix.net/u/29321/showart_343791.html][size=10pt]周立发 linux 视频教程下载(不定期持续更新)[/size][/url][/color][/size][color=#ff0102][font=monospace]
[/font][/color][color=#ff0102]如以上内容有误或有不足之处,望朋友能给予意见或者建议!谢谢:)web log: xuanfei.cublog.cn
[/color][color=#ff0102]
[/color]
[[i] 本帖最后由 悬非 于 2007-7-22 11:15 编辑 [/i]]
更多推荐
已为社区贡献1条内容
所有评论(0)