C語言除錯手段 鎖定錯誤的實現方法

2022-09-29 22:36:20 字數 2123 閱讀 5239

在專案開發工程中,如果能確定哪個檔案下的哪個函式下的哪行出錯--即鎖定錯誤,那該多好啊,該文章就www.cppcns.com是為此而作的。

首先來了解一下檔案預設的輸出資訊的函式吧:

檔案資訊函式:

複製** **如下:

printf("line : %d\n", __line__);                   //當前行數

printf("filename : %s\n", __file__);             //當前檔名

printf("function : %s\n", __function__);  //當前函式

printf("time : %s\n", __time__);                  //當前時間

printf ("date : %s\n",&n程式設計客棧bsp; __date__);              //當前日期

輸出:line : 10

filename : test.c

function : main.c

time : 14:13:51

date : oct 13 2012

理論已足,那就來看看如何鎖定錯誤吧:

一、原始檔:

複製** **如下:

[root@localhost for_test]# cat erroutput.c

#include

#include

#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 \

&nwww.cppcns.combsp; www.cppcns.com;          } while (0)

int main(void)

二、輸出:

複製** **如下:

[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處理:

複製** **如下:

#ifdef debug

#define dbg(fmt, args...)  printf("debug " fmt, ##args)// ##運算子用於把引數連線到一起。預處理程式把出現在##兩側的引數合併成乙個符號。

#else

#define dbg(fmt, args...)

#endif

#define err(fmt, args...)  printf("error " fmt, ##args)

[root@localhost for_test]# cat debug_err.c

#include

//#define debug

int main(void)

[root@localhost for_test]# ./a.out

error ***x

#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)

本文標題: c語言除錯手段:鎖定錯誤的實現方法

本文位址:

C語言錯誤 指標的型別錯誤

指標的型別錯誤 include include include 用const來限制形參的指向不可以修改,優化 的安全性 int getnum int const pin in 指標的型別 指標都是有自己的型別的 例如 int int 指標的型別本質上是對指標所指向的記憶體空間一種描述 一級指標,二級...

C語言遇到的錯誤

第乙個 expected declaration or statement at end of input 翻譯 期望在輸入結束時宣告或語句 可能錯誤 1某乙個函式或者變數沒有在使用之前宣告。2某個地方少了個括號。這個最坑 第二個 error lvalue required as left oper...

遇到的c語言錯誤

include include time t time time t t intmain int argc,char ar 錯誤 發生了段錯誤分析 我們只傳入了乙個空指標,它並沒有指向任何變數。所以他是無法 裝下任何東西的 所以我們在解引用的時候並沒有任何意義。如果我們將變數比作乙個容器,把容器內部...