用C语言为图书馆设计公共错误代码

用C语言为图书馆设计公共错误代码,c,api-design,library-design,C,Api Design,Library Design,我试图解决的一个设计问题是,如何灵活地使客户端能够处理调用库函数时可能出现的错误。我看到两种方法: I.将任何库函数可能引发的所有错误放在公共单独的头文件中,例如。 lib_errors.h: #define APP_ERR_OBJECT_TOO_SMALL 1 /* The object is too small */ #define APP_ERR_HOST_UNREACHABLE 2 /* Host is not reachable */ #define APP_ERR_UNKNOWN

我试图解决的一个设计问题是,如何灵活地使客户端能够处理调用库函数时可能出现的错误。我看到两种方法:

I.将任何库函数可能引发的所有错误放在公共单独的头文件中,例如。
lib_errors.h

#define APP_ERR_OBJECT_TOO_SMALL 1 /* The object is too small */

#define APP_ERR_HOST_UNREACHABLE 2 /* Host is not reachable */

#define APP_ERR_UNKNOWN          3 /* Unknown error */
#include "lib_errors.h"

/**
 * -1 - on error 
 *      error_code is set to APP_ERR_OBJECT_TOO_SMALL the buf is too small
 *      error_code is set to APP_ERR_UNKNOWN in case of unknown error
 */ 
int hdr1_drain(void *buf, size_t sz, int *error_code);
#include "lib_errors.h"

/**
 * -1 - on error 
 *      error_code is set to APP_ERR_HOST_UNREACHABLE if the host is unreachable
 *      error_code is set to APP_ERR_UNKNOWN in case of unknown error
 */ 
int hdr2_connect(char *add, int *error_code);
#define HDR1_OBJECT_TOO_SMALL 1
#define HDR1_UNKNOWN          2
并将其包含在库的所有公共头文件中

标题1.h

#define APP_ERR_OBJECT_TOO_SMALL 1 /* The object is too small */

#define APP_ERR_HOST_UNREACHABLE 2 /* Host is not reachable */

#define APP_ERR_UNKNOWN          3 /* Unknown error */
#include "lib_errors.h"

/**
 * -1 - on error 
 *      error_code is set to APP_ERR_OBJECT_TOO_SMALL the buf is too small
 *      error_code is set to APP_ERR_UNKNOWN in case of unknown error
 */ 
int hdr1_drain(void *buf, size_t sz, int *error_code);
#include "lib_errors.h"

/**
 * -1 - on error 
 *      error_code is set to APP_ERR_HOST_UNREACHABLE if the host is unreachable
 *      error_code is set to APP_ERR_UNKNOWN in case of unknown error
 */ 
int hdr2_connect(char *add, int *error_code);
#define HDR1_OBJECT_TOO_SMALL 1
#define HDR1_UNKNOWN          2
标题2.h

#define APP_ERR_OBJECT_TOO_SMALL 1 /* The object is too small */

#define APP_ERR_HOST_UNREACHABLE 2 /* Host is not reachable */

#define APP_ERR_UNKNOWN          3 /* Unknown error */
#include "lib_errors.h"

/**
 * -1 - on error 
 *      error_code is set to APP_ERR_OBJECT_TOO_SMALL the buf is too small
 *      error_code is set to APP_ERR_UNKNOWN in case of unknown error
 */ 
int hdr1_drain(void *buf, size_t sz, int *error_code);
#include "lib_errors.h"

/**
 * -1 - on error 
 *      error_code is set to APP_ERR_HOST_UNREACHABLE if the host is unreachable
 *      error_code is set to APP_ERR_UNKNOWN in case of unknown error
 */ 
int hdr2_connect(char *add, int *error_code);
#define HDR1_OBJECT_TOO_SMALL 1
#define HDR1_UNKNOWN          2
我看到的问题是:库可能引发的所有错误都将包含在每个头文件中,即使是头文件本身没有引发的错误

II.要在每个头文件中定义特定于头的错误:

标题1.h

#define APP_ERR_OBJECT_TOO_SMALL 1 /* The object is too small */

#define APP_ERR_HOST_UNREACHABLE 2 /* Host is not reachable */

#define APP_ERR_UNKNOWN          3 /* Unknown error */
#include "lib_errors.h"

/**
 * -1 - on error 
 *      error_code is set to APP_ERR_OBJECT_TOO_SMALL the buf is too small
 *      error_code is set to APP_ERR_UNKNOWN in case of unknown error
 */ 
int hdr1_drain(void *buf, size_t sz, int *error_code);
#include "lib_errors.h"

/**
 * -1 - on error 
 *      error_code is set to APP_ERR_HOST_UNREACHABLE if the host is unreachable
 *      error_code is set to APP_ERR_UNKNOWN in case of unknown error
 */ 
int hdr2_connect(char *add, int *error_code);
#define HDR1_OBJECT_TOO_SMALL 1
#define HDR1_UNKNOWN          2
header2.h

#define HDR2_HOST_UNREACHABLE 1
#define HDR2_UNKNOWN          2
我看到的问题是:代码重复的数量随着常见错误的数量而增加。所有常见错误都必须在每个公共头文件中复制


所以我现在不知道该选哪一个。可能还有另一种方法来解决设计问题。第一种方法似乎更接近POSIX
errno

您可以将这两种方法结合起来。将所有常见错误放在一个标题中,然后将特定于应用程序的错误放在应用程序标题中

lib_errors.h:

#define ERR_UNKNOWN 1
...
#define LIBERR_LAST 100
标题1.h:

#include "lib_errors.h"

#define HDR1_OBJECT_TOO_SMALL (LIBERR_LAST + 1)
...
标题2.h:

#include "lib_errors.h"

#define HDR2_HOST_UNREACHABLE (LIBERR_LAST + 1)
...

errno
不是位掩码,它只是顺序值:
1、2、3、4、
@Barmar都不是我的错误代码<代码>1、2、4等。。我选择了2的幂。这被认为是错误的吗?2的幂是位掩码。它允许您组合它们,如
APP_ERR_OBJECT_TOO_SMALL | APP_ERR_UNKNOWN
,以指示发生了多个错误。您的方法意味着只能有32个不同的错误代码。我认为第一种方法没有错。您可以将库的所有接口放在一个标题中。