繁体   English   中英

错误:使用未声明的标识符'errno_t'

[英]error: use of undeclared identifier 'errno_t'

这是我死的简单虚拟代码:

#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>

现在,在errno.h /usr/include/sys中,我找到了以下行:

#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中的/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__将在cdefs.h中的/usr/include/syscdefs.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标准的一部分,还是为什么不包括它?

errno_t不是标准类型; 它是ISO C11中包含的可选(并且广泛不受支持和不支持)附件K的一部分,仅因为某个特定供应商有忽视和破坏标准的历史。

由于附件K将errno_t定义为int ,因此errno对象的类型为int ,并且所有错误代码均为int ,只需在程序中使用int即可。 它比依赖不太可能支持的可选功能便携得多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM