15

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;
    ^

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>

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

And at /usr/include/_types in _errno_t.h I found this:

typedef int errno_t;

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?

Thanks in advance!


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

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:

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:

Is not errno_t part of the C11 standard, or why isn't it included, when the standard is specified for the compiler?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Peter Varo
  • 11,726
  • 7
  • 55
  • 77
  • Are you including `cdefs.h` (directly or through other includes)? `__STDC_WANT_LIB_EXT1__` is defined on a condition. Is that condition met? – P.P Jun 13 '14 at 13:56
  • @BlueMoon updated the post -- the `/usr/include/sys/errno.h` has the inclusion before it checks if the var is defined or not – Peter Varo Jun 13 '14 at 13:59
  • It's because you're compiling with `-std=c11` - take that out, or use the less strict `-std=gnu11`, and it will compile just fine. – Paul R Jun 13 '14 at 14:01
  • @PaulR you are right.. can you explain why is this happening? afaik `errno_t` is part of the C11 standard to, isn't it? – Peter Varo Jun 13 '14 at 14:02
  • That's a different question, and one I don't know the answer to. ;-) – Paul R Jun 13 '14 at 14:03
  • 1
    @PeterVaro: No, it's part of Annex K, which is not normative except for implementations which claim to support it. – R.. GitHub STOP HELPING ICE Jun 13 '14 at 14:08

1 Answers1

33

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.

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.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 2
    Give them time - it's taken 15 years for them to get to grips with C99! – Paul R Jun 13 '14 at 14:51
  • 11
    The bigger issue is that they've made a point of trying to impose their useless additions to the language into a standard that they have no interest or intention in following. That's not the way standards are supposed to work. If you don't want to follow the standard you shouldn't participate in trying to shape it. – R.. GitHub STOP HELPING ICE Jun 13 '14 at 16:18
  • @R..GitHubSTOPHELPINGICE Why not? In that position I’d want the standard to follow *me*. – David Foerster Feb 06 '21 at 11:14
  • 1
    @DavidFoerster: What I mean by "you shouldn't participate" is not that I expect someone in that position to voluntarily exclude themselves. It's a statement that I think the standards processes should be designed to be robust against such bad actors and exclude them from participating when they have a history of trying to shape it then refusing to follow it. – R.. GitHub STOP HELPING ICE Sep 01 '22 at 22:14
  • 1
    @R..GitHubSTOPHELPINGICE Of course. I was being sarcastic but forgot that I’m on the internet where sarcasm is… difficult. – David Foerster Sep 03 '22 at 00:50