这是我死的简单虚拟代码:
#include <errno.h>
int main(void)
{
errno_t e;
return 0;
}
这令人惊讶地引发了这个错误:
main.c:5:5: error: use of undeclared identifier 'errno_t'
errno_t x;
^
我开始追踪这些痕迹:当编译器看到<...>
包含内容时,它会首先查看我在/usr/include
哪里找到了errno.h
文件。实际上,除了许可证注释之外,它只有一行:
#include <sys/errno.h>
现在,/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
在我发现这个/usr/include/_types
:_errno_t.h
typedef int errno_t;
所以看起来,它就在那里,它是整数类型的别名,也是errno.h
-- 的一部分。
那为什么不包括在内?为什么编译器会引发未声明的标识符错误?
提前致谢!
相关信息:
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
以下行中定义:
/* 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
更新:
正如@PaulR 在评论部分所说:如果我删除-std=c11
标志,它会编译。如果包含标志,这与引发的错误一样令人惊讶。所以我用一个子问题扩展了这个问题:
当为编译器指定标准时,它不是errno_t
C11 标准的一部分,或者为什么不包含它?