strerror描述字符串

时间:2012-11-23 00:22:52

标签: c linux

C函数strerror返回错误描述字符串,详见here。示例字符串

No such file or directory

问题是这些字符串定义在哪里?我查看了我的头文件,但没有看到任何内容。

4 个答案:

答案 0 :(得分:6)

它们被定义在C库的某个地方,传统上是char*的全局数组sys_errlist,长度为sys_nerr,至少在Unix系统上。

由于在strerror之前编写的遗留程序已经标准化,可以直接访问此数组,即使在现代GNU / Linux和Mac OS X上也可以向后兼容(尽管你真的不应该访问它除非通过perrorstrerror)。

例如,这是Mac OS X 10.8.2 definition of sys_errlist

答案 1 :(得分:4)

包含错误消息的头文件名为errmsg.h

00012 const char *const sys_errlist[] = {
00013         "Operation succeeded",        /* 0 */
00014         "Invalid argument",           /* EINVAL */
00015         "Bad memory address",         /* EFAULT */
00016         "String too long",            /* ENAMETOOLONG */
00017         "Out of memory",              /* ENOMEM */
00018         "Input/output error",         /* EIO */
00019         "No such file or directory",  /* ENOENT */
00020         "Not a directory",            /* ENOTDIR */
00021         "Is a directory",             /* EISDIR */
00022         "File or object exists",      /* EEXIST */
00023         "Cross-device link",          /* EXDEV */
00024         "Try again later",            /* EAGAIN */
00025         "Illegal seek",               /* ESPIPE */
00026         "Unimplemented feature",      /* EUNIMP */
00027         "Device not available",       /* ENXIO */
00028         "No such device",             /* ENODEV */
00029         "Device or resource busy",    /* EBUSY */
00030         "Invalid/inappropriate ioctl",/* EIOCTL (ENOTTY in Unix) */
00031         "Directory not empty",        /* ENOTEMPTY */
00032         "Result too large",           /* ERANGE */
00033         "No space left on device",    /* ENOSPC */
00034         "Too many open files",        /* EMFILE */
00035         "Too many open files in system",/* ENFILE */
00036         "No such system call",        /* ENOSYS */
00037         "File is not executable",     /* ENOEXEC */
00038         "Argument list too long",     /* E2BIG */
00039         "Bad file number",            /* EBADF */
00040 };

正如您所看到的,它取决于libc实现。但总体思路是一样的:某些数组包含从错误号到字符串最大长度为1024字节的映射。

其他实施:

答案 2 :(得分:2)

它们通常可能被定义并嵌入到您的C运行时库中,例如libc在大多数unix类似的系统上。

答案 3 :(得分:2)

至少在一个典型的库中,它们将位于一个被链接的目标文件中 - 通常是strerror.o(或.obj等)。如果您足够关心,可以通过源代码轻松地将它们调到库中。

相关问题