According to this, the POSIX library does not include getopt.h
. However, I found this in unistd.h
:
#ifdef __USE_POSIX2
/* Get definitions and prototypes for functions to process the
arguments in ARGV (ARGC of them, minus the program name) for
options given in OPTS. */
# define __need_getopt
# include <getopt.h>
#endif
Does this mean that getopt.h
is implicitly included when you include unistd.h
? I mean, is the code above something I should expect from all implementations of the unistd header file, or is it just something that is in my particular version? Also, is the __USE_POSIX2
macro defined in POSIX.2 and onwards, or is it just for POSIX.2?
__USE_POSIX2
is an implementation detail of glibc; it corresponds to _POSIX_C_SOURCE >= 2
or _XOPEN_SOURCE
being defined. These are also implied by _GNU_SOURCE
, and are used implicitly unless strict ANSI mode is on. You are not supposed to define the __USE_ macros directly.
Since it corresponds to a value >= 2
, it does apply to later versions. See the feature_test_macros manpage for further details.
Or, from the comments in features.h (the internal header - don't include directly - that takes care of all this):
/* These are defined by the user (or the compiler)
to specify the desired environment:
...
_POSIX_C_SOURCE If ==1, like _POSIX_SOURCE; if >=2 add IEEE Std 1003.2;
if >=199309L, add IEEE Std 1003.1b-1993;
if >=199506L, add IEEE Std 1003.1c-1995;
if >=200112L, all of IEEE 1003.1-2004
if >=200809L, all of IEEE 1003.1-2008
_XOPEN_SOURCE Includes POSIX and XPG things. Set to 500 if
Single Unix conformance is wanted, to 600 for the
sixth revision, to 700 for the seventh revision.