国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C > 正文

C語(yǔ)言調(diào)試手段:鎖定錯(cuò)誤的實(shí)現(xiàn)方法

2020-01-26 16:07:36
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
在項(xiàng)目開發(fā)工程中,如果能確定哪個(gè)文件下的哪個(gè)函數(shù)下的哪行出錯(cuò)--即鎖定錯(cuò)誤,那該多好啊,該文章就是為此而作的。
首先來(lái)了解一下文件默認(rèn)的輸出信息的函數(shù)吧:
文件信息函數(shù):
復(fù)制代碼 代碼如下:

printf("line : %d/n", __LINE__);                   //當(dāng)前行數(shù)
printf("filename : %s/n", __FILE__);             //當(dāng)前文件名
printf("function : %s/n", __FUNCTION__);  //當(dāng)前函數(shù)
printf("time : %s/n", __TIME__);                  //當(dāng)前時(shí)間
printf ("date : %s/n",  __DATE__);              //當(dāng)前日期
輸出:
line : 10
filename : test.c
function : main.c
time : 14:13:51
date : Oct 13 2012

理論已足,那就來(lái)看看如何鎖定錯(cuò)誤吧:
一、源文件:
復(fù)制代碼 代碼如下:

[root@localhost for_test]# cat erroutput.c
#include <stdio.h>
#include <assert.h>
#define _DEBUG(msg...)    printf("[ %s,%s, %d ]=>",__FILE__, __FUNCTION__, __LINE__);  printf(msg);printf("/r/n")
#define _ERROR(msg...)    printf("[ error: %s, %d]=>", __FILE__,  __LINE__);printf(msg); printf("/r/n")
#define _ASSERT(exp)      /
                        do {/
                                if (!(exp)) {/
                                printf( "[ %s ]  ",#exp);printf("/r/n");/
                                assert(exp);/
                                }/
                        } while (0)
int main(void)
{
        char *p = NULL;
        _DEBUG("DEBUG!");
        _ERROR("ERROR!");
        _ASSERT(NULL != p);
        return 0;
}

二、輸出:
復(fù)制代碼 代碼如下:

[root@localhost for_test]# gcc erroutput.c
[root@localhost for_test]# ./a.out
[ erroutput.c,main, 17 ]=>DEBUG!
[ error: erroutput.c, 18]=>ERROR!
[ NULL != p ]
a.out: erroutput.c:19: main: Assertion `((void *)0) != p' failed.
已放棄

TI處理:
復(fù)制代碼 代碼如下:

#ifdef DEBUG
    #define DBG(fmt, args...)  printf("Debug " fmt, ##args)// ##運(yùn)算符用于把參數(shù)連接到一起。預(yù)處理程序把出現(xiàn)在##兩側(cè)的參數(shù)合并成一個(gè)符號(hào)。
#else
    #define DBG(fmt, args...)
#endif
#define ERR(fmt, args...)  printf("Error " fmt, ##args)
[root@localhost for_test]# cat debug_err.c
#include <stdio.h>
//#define DEBUG
int main(void)
{
       DBG("xxxx/n");
       ERR("xxxx/n");
       return 0;
}
[root@localhost for_test]# ./a.out
Error xxxx

#ifdef __DEBUG
    #define DBG(fmt, args...) fprintf(stderr,"Encode Debug: " fmt, ## args)
#else
    #define DBG(fmt, args...)
#endif
#define ERR(fmt, args...) fprintf(stderr,"Encode Error: " fmt, ## args)

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 汝南县| 保亭| 阳山县| 新源县| 广安市| 夏河县| 百色市| 遂昌县| 彩票| 嘉黎县| 都昌县| 华蓥市| 杭锦旗| 诸城市| 罗源县| 南澳县| 额敏县| 密云县| 同江市| 庄浪县| 锡林郭勒盟| 古田县| 会同县| 洞口县| 鹤峰县| 太仓市| 军事| 个旧市| 灌南县| 东宁县| 沙坪坝区| 凌海市| 和平县| 剑阁县| 弋阳县| 宁强县| 根河市| 平乐县| 桦南县| 武定县| 东平县|