错误:使用未声明的标识符“errno_t”的
下面是我死了简单的虚拟code:
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;
^
我开始的遵循痕迹的:当编译器看到&LT; ...&GT;
夹杂它会先看看 / usr / include目录
其中,当然,我发现 errno.h中
文件。其实它在一个单一的线,除了许可证评论,这是:
I started to follow the traces: when the compiler sees the <...>
inclusions it will first look at /usr/include
where of course I found errno.h
file. Actually it has a single line in it, besides the license comment, which is:
#include <sys/errno.h>
现在,在 / usr / include目录中的
我发现下面几行: errno.h中
/ SYS
Now, at /usr/include/sys
in errno.h
I found the following lines:
#include <sys/cdefs.h>
#if defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ >= 1
#include <sys/_types/_errno_t.h>
#endif
而在 / usr / include目录/ _types
在 _errno_t.h
我发现这一点:
typedef int errno_t;
因此,看起来,它的存在,它是整数类型的别名,的 errno.h中
部分 - 就像它应该是。
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.
那么,为什么是不是包括在内?为什么编译器引发未声明的标识符错误?
Then why isn't it included? Why the compiler raises the undeclared identifier error?
在此先感谢!
相关信息:的
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
宏变量 __ STDC_WANT_LIB_EXT1 __
将在定义 / usr / include目录中的
: / SYS
cdefs.h在以下行
The macro variable __STDC_WANT_LIB_EXT1__
will be defined at /usr/include/sys
in cdefs.h
in the following lines:
/* 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:
由于@PaulR在评论区说:如果我删除 -std = C11
标志,它编译。这是一样令人惊讶,如果被列入该标志作为引发的错误。所以我用一个子问题,延长了这个问题:
As @PaulR said in the comment section: if I remove the -std=c11
flag, 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:
的不 errno_t
的C11标准的一部分,或者为什么没有收录,当编译器规定的标准?的
Is not errno_t
part of the C11 standard, or why isn't it included, when the standard is specified for the compiler?
errno_t
不是一个标准的类型;它是可选的(和广泛不喜欢和不支持的)附件K,包括ISO C11只与忽视,破坏标准的历史,因为一个特定的供应商的一部分。
errno_t
is 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.
由于附件K定义 errno_t
为 INT
的错误号类型code>对象
。它不是依靠一个可选功能,它是不太可能支持更便携。 INT
,和所有的错误codeS是 INT
,只需使用 INT
Since Annex K defines errno_t
as int
, the type of the errno
object is int
, and all error codes are int
, simply use int
in your programs. It's much more portable than relying on an optional feature which is unlikely to be supported.