C语言 错误:使用未声明的标识符“errno_t”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24206989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: use of undeclared identifier 'errno_t'
提问by Peter Varo
Here is my dead simple dummy code:
这是我死的简单虚拟代码:
#include <errno.h>
int main(void)
{
errno_t e;
return 0;
}
Which surprisingly raises this error:
这令人惊讶地引发了这个错误:
main.c:5:5: error: use of undeclared identifier 'errno_t'
errno_t x;
^
I started to follow the traces: when the compiler sees the <...>inclusions it will first look at /usr/includewhere of course I found errno.hfile. Actually it has a single line in it, besides the license comment, which is:
我开始跟踪跟踪:当编译器看到<...>包含的内容时,它会首先查看/usr/include我找到errno.h文件的位置。实际上,除了许可证注释之外,它只有一行:
#include <sys/errno.h>
Now, at /usr/include/sysin errno.hI found the following lines:
现在,/usr/include/sys在errno.h我发现以下几行:
#include <sys/cdefs.h>
#if defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ >= 1
#include <sys/_types/_errno_t.h>
#endif
And at /usr/include/_typesin _errno_t.hI found this:
而在/usr/include/_types中_errno_t.h,我发现这一点:
typedef int errno_t;
So it looks like, it is there, and it is an alias of the integer type, and part of the errno.h-- just as it should be.
所以看起来,它就在那里,它是整数类型的别名,errno.h它应该是整数类型的一部分。
Then why isn't it included? Why the compiler raises the undeclared identifier error?
那为什么不包括在内呢?为什么编译器会引发未声明的标识符错误?
Thanks in advance!
提前致谢!
RELEVANT INFO:
相关信息:
Compiler:
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)`
Compiler flags:
-std=c11 -I/usr/include/sys -I/usr/local/include
The macro variable __STDC_WANT_LIB_EXT1__will be defined at /usr/include/sysin cdefs.hin the following lines:
宏变量__STDC_WANT_LIB_EXT1__将在被限定/usr/include/sys在cdefs.h下面的行:
/* If the developer has neither requested a strict language mode nor a version
* of POSIX, turn on functionality provided by __STDC_WANT_LIB_EXT1__ as part
* of __DARWIN_C_FULL.
*/
#if !defined(__STDC_WANT_LIB_EXT1__) && !defined(__STRICT_ANSI__) && __DARWIN_C_LEVEL >= __DARWIN_C_FULL
#define __STDC_WANT_LIB_EXT1__ 1
#endif
UPDATE:
更新:
As @PaulR said in the comment section: if I remove the -std=c11flag, it compiles. Which is just as surprising as the error raised if the flag was included. So I extend this question with a sub-question:
正如@PaulR 在评论部分所说:如果我删除-std=c11标志,它会编译。如果包含标志,这与引发的错误一样令人惊讶。所以我用一个子问题扩展这个问题:
Is not errno_tpart of the C11 standard, or why isn't it included, when the standard is specified for the compiler?
是不是errno_t对C11标准的一部分,或者为什么没有包括在内,当编译器规定的标准?
回答by R.. GitHub STOP HELPING ICE
errno_tis not a standard type; it's part of the optional (and widely disliked and unsupported) Annex K, included with ISO C11 only because of one particular vendor with a history of ignoring and sabotaging the standard.
errno_t不是标准类型;它是可选的(广泛不喜欢和不受支持的)附件 K 的一部分,包含在 ISO C11 中只是因为一个特定的供应商有无视和破坏标准的历史。
Since Annex K defines errno_tas int, the type of the errnoobject is int, and all error codes are int, simply use intin your programs. It's much more portable than relying on an optional feature which is unlikely to be supported.
由于附件 K 定义errno_t为int,errno对象的类型为int,所有错误代码均为int,只需int在您的程序中使用即可。它比依赖不太可能得到支持的可选功能更便携。

