Pet*_*aro 11 c errno clang tr24731 c11
这是我死的简单虚拟代码:
#include <errno.h>
int main(void)
{
errno_t e;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这令人惊讶地引发了这个错误:
main.c:5:5: error: use of undeclared identifier 'errno_t'
errno_t x;
^
Run Code Online (Sandbox Code Playgroud)
我开始遵循这些跟踪:当编译器看到<...>包含时,它将首先查看/usr/include我找到errno.h文件的位置.实际上它除了许可评论之外还有一行,它是:
#include <sys/errno.h>
Run Code Online (Sandbox Code Playgroud)
现在,/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
Run Code Online (Sandbox Code Playgroud)
而在/usr/include/_types中_errno_t.h,我发现这一点:
typedef int errno_t;
Run Code Online (Sandbox Code Playgroud)
所以看起来,它就在那里,它是整数类型的别名,也是它的一部分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
Run Code Online (Sandbox Code Playgroud)
宏变量__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
Run Code Online (Sandbox Code Playgroud)
更新:
正如@PaulR在评论部分所说:如果我删除了-std=c11标志,它就会编译.如果包含该标志,则与引发的错误一样令人惊讶.所以我用一个子问题来扩展这个问题:
是不是errno_t对C11标准的一部分,或者为什么没有包括在内,当编译器规定的标准?
R..*_*R.. 24
errno_t不是标准类型; 它是ISO C11中包含的可选(并且广泛不受支持和不支持)附件K的一部分,仅因为某个特定供应商有忽视和破坏标准的历史.
由于附件K定义errno_t为int,errno对象的类型是int,并且所有错误代码都在您的程序中int使用int.它比依赖不太可能支持的可选功能便携得多.