下面是我死去的简单的虚拟代码:
#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
in 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
in _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标准的一部分,或者为什么不包括它?
发布于 2014-06-13 14:07:40
errno_t
不是一种标准类型;它是可选附件K的一部分(而且广泛不受欢迎和不受支持),包含在ISO C11中只是因为有一个特定的供应商有忽略和破坏该标准的历史。
由于附件K将errno_t
定义为int
,所以errno
对象的类型是int
,所有错误代码都是int
,所以只需在程序中使用int
即可。它比依赖不太可能被支持的可选特性的可移植性要好得多。
https://stackoverflow.com/questions/24206989
复制相似问题