• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C语言调试手段:锁定错误的实现方法

c语言 搞代码 4年前 (2022-01-06) 26次浏览 已收录 0个评论

本篇文章是对在C语言调试中,锁定错误的方法进行了详细的分析介绍,需要的朋友参考下

在项目开发工程中,如果能确定哪个文件下的哪个函数下的哪行出错–即锁定错误,那该多好啊,该文章就是为此而作的。
首先来了解一下文件默认的输出信息的函数吧:
文件信息函数:

代码如下:
printf(“line : %d\n”, __LINE__);                   //当前行数
printf(“filename : %s\n”, __FILE__);             //当前文件名
printf(“function : %s\n”, __FUNCTION__);  //当前函数
printf(“time : %s\n”, __TIME__);                  //当前时间
printf (“date : %s\n”,  __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(m来源gao*daima.com搞@代#码网sg…)    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;
}

二、输出:

代码如下:
[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)
{
       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)

以上就是C语言调试手段:锁定错误的实现方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C语言调试手段:锁定错误的实现方法
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址