__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][/color] 先看下簡單的初始代碼:注意其編譯運行後的結果。
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]
__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][/color] 先看下簡單的初始代碼:注意其編譯運行後的結果。
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]