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_POSIX2is an implementation detail of glibc; it corresponds to_POSIX_C_SOURCE >= 2or_XOPEN_SOURCEbeing 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):