6.1. Feature Selection
In glibc, there are a set of feature selection macros that are used to select which standards you wish glibc to comply with. Standards sometimes conflict, and so glibc allows you to select exactly which set of standards (formal, de jure, and informal, de facto) with which to comply, fully or partially. These macros are technically called feature test macros.
You need to be aware of these macros because the default set of macros defined does not provide all the functionality of glibc. A few mechanisms discussed in this book are not available with the default feature set selected; we document the required feature macros to use each of these mechanisms.
The feature test macros are designed to specify with what standards (de jure or de facto), and in some cases precisely which versions of those standards, glibc should comply. This compliance often includes not defining functions and macros beyond what is specified by a standard for header files that are themselves defined by that standard. That means that an application written to conform with a standard can define its own functions and macros without conflicting with extensions not defined by that standard.
The feature test macros do not guarantee that your application is fully compatible with the set of standards specified by the set of macros you define. Setting the feature test macros may find some use of nonportable extensions, but it will not show, for example, use of header files that are entirely unspecified by the standard.
The macros are defined in the system header file feature.h, which you should not include directly. Instead, all other header files that might be affected by the contents of feature.h include it.
The default set of feature macros if none are defined is _SVID_SOURCE=1,_BSD_SOURCE=1,_POSIX_SOURCE=1, and _POSIX_C_SOURCE=199506L. Each option is described in more detail below, but this essentially translates into "support the capabilities of the 1995 POSIX standard (see page 8; this is from before POSIX and the Single Unix Standard were combined), all standard System V features, and all BSD features that do not conflict with System V features." This default set of feature macros suffices for most programs.
When you give gcc the -ansi option, as documented on page 46, it automatically defines the internal __STRICT_ANSI__ macro, which turns off all the default feature macros.
With the exception of the __STRICT_ANSI__ macro, which is special (and which should be set only by the compiler in the context of the -ansi command line option), these feature macros are cumulative; you can define any combination of them. The exact definition of _BSD_SOURCE changes depending on which other feature macros are set (as documented below); the rest are purely cumulative.
Some of the feature test macros are defined by various versions of POSIX or other standards, some are common in the industry, and others are strictly limited to glibc.
If this macro is defined, all the interfaces defined as part of the original POSIX.1 specification are made available.
This macro was defined by the original POSIX.1-1990 standard.
This macro supersedes _POSIX_SOURCE. If it is set to 1, it is equivalent to _POSIX_SOURCE. If it is >=2, then it also includes C interfaces defined by POSIX.2, including regular expressions. If it is >=199309L, then it also includes additional C interfaces defined in the 1993 revision of POSIX, particularly including the soft real-time functionality; if it is >=199506L (the default), it also includes additional C interfaces defined in the 1995 revision of POSIX, particularly including POSIX threads. This macro was defined by versions of POSIX released after 1990 in order to differentiate support for various versions of the POSIX (and now also Single Unix) standards. It is largely superseded by _XOPEN_SOURCE.
The _XOPEN_SOURCE macro is defined by the XSI portion of the Single Unix Standard, and defines a logical superset of the interfaces included by _POSIX_C_SOURCE. It was also defined by XPG. If it is defined at all, base-level conformance with XPG4 (Unix95) is included. If it is defined as 500, then base-level conformance with XPG5 (Unix98, SuS version 2) is included. If it is defined as 600, base-level conformance with IEEE Std 1003.1-2003 (the combined POSIX and SuS document) is included.
This feature test macro exports the interfaces defined by the new ISO/IEC C99 standard.
This feature test macro makes functionality specified by the System V Interface Definition (SVID) available. This does not imply that glibc provides a complete implementation of the SVID standard; it merely exposes the SVID-specified functionality that exists in glibc.
BSD features can conflict with other features, and the conflicts are always resolved in favor of System Vor standard-compliant behavior if any POSIX, X/Open, or System V feature macro is defined or implied so the only feature macro that allows BSD behavior to be asserted is _ISOC99_SOURCE. (The exact definition of this feature test macro has changed from time to time, and may change again, since it is not specified by any standard.)
_GNU_SOURCE turns on everything possible, favoring System V interfaces to BSD interfaces in cases of conflict. It also adds some GNU- and Linux-specific interfaces, such as file leases.
When the standard set of feature test macros will not suffice, the most commonly useful feature macros to define are _GNU_SOURCE (turn everything on the easiest solution), _XOPEN_SOURCE=600 (most things you are likely to care about, a subset of _GNU_SOURCE), or _ISOC99_SOURCE (use features from the most recent C standard, a subset of _XOPEN_SOURCE=600).