__FILE__,__LINE__,FUNCTION__實現代碼跟蹤調試(linux下c語言編程

來源:互聯網
上載者:User


__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]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.