设为首页 加入收藏

TOP

Linux下把错误捕获函数放入error.h
2014-11-24 03:03:23 来源: 作者: 【 】 浏览:1
Tags:Linux 错误 捕获 函数 放入 error.h

头文件放在/usr/include目录下,进入这个目录,然后用vi编辑器打开error.h文件。要添加的两个函数如下:


void my_err1(int error)
{
printf("error: %s with errno: %d\n",strerror(error),error);
exit(1);
}
void my_err2(const char* err_string,int line,int error)
{
printf("error: line:%d %s():%s with errno:%d\n",line,err_string,strerror(error),error);
exit(1);
}


将上述两函数添加至文件末尾。由于strerror()函数涉及到头文件string.h,所以在已打开的error.h文件中还得添加string.h头文件。具体添加位置可参考下面:


20 #ifndef _ERROR_H
21 #define _ERROR_H 1
22
23 #include "features.h"
24
25 //personal addition
26 #include "string.h"
27 //personal addition end
28
29 __BEGIN_DECLS
30


59 //personal addition
60 //brief function
61 void my_err1(int error)
62 {
63 printf("error: %s with errno: %d\n",strerror(error),error);
64 exit(1);
65 }
66
67 //detailed function
68 void my_err2(const char* err_string,int line,int error)
69 {
70 printf("error: line:%d %s():%s with errno:%d\n",line,err_string,strerror(error),error);
71 exit(1);
72 }
73 //persoanl addition end
74 __END_DECLS
75
76 #endif /* error.h */


edsionte@edsionte-laptop:/usr/include$ ls -l error.h
-rw-r--r-- 1 root root 2557 2010-06-23 12:11 error.h


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux下C编程错误捕获函数 下一篇Android button事件处理方式

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)