I'm compiling code using gcc that comes from Visual C++ 2008. The code is using errno_t, but in some versions of gcc headers including <errno.h>
doesn't define the type. How do I detect if the type is defined? Is there a define that signals that the type was defined? In the case it isn't defined I'd like to provide the typedef to let the code compile correctly on all platforms.

- 6,468
- 9
- 43
- 53
4 Answers
Microsoft's errno_t
is redundant. errno
is defined by the ISO C standard to be a modifiable lvalue of type int
. If your code needs to store errno
values, then you should put them into an int
.
Do a global search and replace s/errno_t/int/
and you're done.
Edit: Also, you shouldn't be providing a typedef int errno_t
in your code, because all names that end with _t
are reserved.

- 6,920
- 3
- 25
- 29
-
1"all names that end with `_t` are reserved." Is that so? Where is this declared? – sbi Sep 23 '09 at 08:52
-
3sbi: Hmm, I didn't have a ready answer, so I've done a bit of research. The _t namespace is widely described as being "reserved by POSIX", but I've not found anything to that effect in the POSIX spec. The best source is from glibc: http://www.gnu.org/s/libc/manual/html_node/Reserved-Names.html. I think it boils down to the fact that POSIX, or ISO C may define new _t names, so in many people's opinion they are best avoided in application code. – alex tingle Sep 23 '09 at 10:27
-
4C99 enumerates the types defined in the standard headers and warns that "typedef names beginning with `int` or `uint` and ending with `_t` may be added" to `stdint.h` but it doesn't embargo `*_t` entirely. – hobbs Sep 24 '09 at 02:51
-
2Here's the citation for POSIX reserving `_t`: http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_02_02 – R.. GitHub STOP HELPING ICE May 18 '11 at 15:52
You can't check for a typedef the way you can for a macro, so this is a bit on the tricky side. If you're using autoconf
, this patch shows the minimum changes that you need to have autoconf check for the presence of errno_t
and define it if it's missing (the typedef would be placed in a file that includes your generated config.h
and is included by all files that need errno_t
). If you're not using autoconf
you need to come up with some way to do the same thing within your build system, or a very clever set of tests against compiler version macros.

- 223,387
- 19
- 210
- 288
-
thanks. I don't have autoconf available, so I guess I have to go with compiler version macro checks... – vividos Sep 24 '09 at 07:30
-
3Alex's answer, not this one, is the correct answer. `errno_t` should never be used for anything because the type of `errno` is specified as `int`. – R.. GitHub STOP HELPING ICE Aug 11 '10 at 17:00
-
1People may be interested in using `errno_t` because it was recommended by CERT. https://www.securecoding.cert.org/confluence/display/seccode/DCL09-C.+Declare+functions+that+return+errno+with+a+return+type+of+errno_t – glguy Nov 08 '10 at 21:57
-
2
This is typically the case where GNU autoconf comes to the rescue. Basically autoconf will generate a configure script that can detect various system-dependent features such as whether this type exists and how it is defined. You then include the generated C header file within your application.

- 2,325
- 14
- 23
If you know which versions of GCC are giving you trouble, you can test for them. You can check for versions of GCC using something like:
#if __GNUC__ == 3
...
#else
...
#endif

- 4,967
- 20
- 15