feature_test_macros - feature test macros


 #include <features.h>


Feature test macros allow the programmer to control the definitions that are exposed by system header files when a program is compiled. This can be useful for creating portable applications, by preventing non-standard definitions from being exposed. Other macros can be used to expose non-standard definitions that are not exposed by default. The precise effects of each of the feature test macros described below can be ascertained by inspecting the <features.h> header file.

In order to be effective, a feature test macro must be defined before including any header files. This can either be done in the compilation command (cc -DMACRO=value) or by defining the macro within the source code before including any headers.

Specification of feature test macro requirements in manual pages

When a function requires that a feature test macro is defined, the manual page SYNOPSIS typically includes a note of the following form (this example from the chmod(2) manual page):

#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int fchmod(int fildes, mode_t mode);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
fchmod(): _BSD_SOURCE || _XOPEN_SOURCE >= 500

The || means that in order to obtain the declaration of fchmod(2) from <sys/stat.h>, either of the following macro definitions must be made before including any header files:

 #define _BSD_SOURCE
 #define _XOPEN_SOURCE 500     /* or any value > 500 */

Alternatively, equivalent definitions can be included in the compilation command:

 cc -D_XOPEN_SOURCE=500        # Or any value > 500

Note that, as described below, some feature test macros are defined be default, so that it may not always be necessary to explicitly specify the feature test macro(s) shown in the SYNOPSIS.

In a few cases, manual pages use a shorthand for expressing the feature test macro requirements (this example from readahead(2)):

 #define _GNU_SOURCE
 #include <fcntl.h>
 ssize_t readahead(int fd, off64_t *offset, size_t count);

This format is employed in cases where only a single feature test macro can be used to expose the function declaration, and that macro is not defined by default.

Feature test macros understood by glibc

The following paragraphs explain how feature test macros are handled in Linux glibc 2.x, x > 0.

Linux glibc understands the following feature test macros:

ISO Standard C. This macro is implicitly defined by gcc(1) when invoked with, for example, the -std=c99 or -ansi flag.
Defining this macro causes header files to expose definitions as follows:
The value 1 exposes definitions conforming to POSIX.1-1990 and ISO C (1990).
The value 2 or greater additionally exposes definitions for POSIX.2-1992.
The value 199309L or greater additionally exposes definitions for POSIX.1b (real-time extensions).
The value 199506L or greater additionally exposes definitions for POSIX.1c (threads).
(Since glibc 2.3.3) The value 200112L or greater exposes definitions corresponding to the POSIX.1-2001 base specification (excluding the XSI extension).
Defining this obsolete macro with any value is equivalent to defining _POSIX_C_SOURCE with the value 1.
Defining this macro causes header files to expose definitions as follows:
Defining with any value exposes definitions conforming to POSIX.1, POSIX.2, and XPG4.
The value 500 or greater additionally exposes definitions for SUSv2 (UNIX 98).
(Since glibc 2.2) The value 600 or greater additionally exposes definitions for SUSv3 (UNIX 03; i.e., the POSIX.1-2001 base specification plus the XSI extension) and C 99 definitions.
If this macro is defined, and _XOPEN_SOURCE is defined, then expose definitions corresponding to the XPG4v2 (SUSv1) UNIX extensions (UNIX 95). This macro is also implicitly defined if _XOPEN_SOURCE is defined with a value of 500 or more.
Exposes C 99 extensions to ISO C (1990). This macro is recognized since glibc 2.1.3; earlier glibc 2.1.x versions recognized an equivalent macro named _ISOC9X_SOURCE (because the C99 standard had not then been finalized). Although the use of the latter macro is obsolete, glibc continues to recognize it for backwards compatibility.
Expose definitions for the alternative API specified by the LFS (Large File Summit) as a "transitional extension" to the Single UNIX Specification. (See http://opengroup.org/platform/lfs.html.) The alternative API consists of a set of new objects (i.e., functions and types) whose names are suffixed with "64" (e.g., off64_t versus off_t, lseek64() versus lseek(), etc.). New programs should not employ this interface; instead _FILE_OFFSET_BITS=64 should be employed.
Defining this macro with the value 64 automatically converts references to 32-bit functions and data types related to file I/O and file system operations into references to their 64-bit counterparts. This is useful for performing I/O on large files (> 2 Gigabytes) on 32-bit systems. (Defining this macro permits correctly written programs to use large files with only a recompilation being required.) 64-bit systems naturally permit file sizes greater than 2 Gigabytes, and on those systems this macro has no effect.
Defining this macro with any value causes header files to expose BSD-derived definitions. Defining this macro also causes BSD definitions to be preferred in some situations where standards conflict, unless one or more of _SVID_SOURCE, _POSIX_SOURCE, _POSIX_C_SOURCE, _XOPEN_SOURCE, _XOPEN_SOURCE_EXTENDED, or _GNU_SOURCE is defined, in which case BSD definitions are disfavored.
Defining this macro with any value causes header files to expose System V-derived definitions. (SVID == System V Interface Definition; see standards(7).)
_ATFILE_SOURCE (since glibc 2.4)
Defining this macro with any value causes header files to expose declarations of a range of functions with the suffix "at"; see openat(2).
Defining this macro (with any value) is equivalent to defining _BSD_SOURCE, _SVID_SOURCE, _ATFILE_SOURCE, _LARGEFILE64_SOURCE, _ISOC99_SOURCE, _XOPEN_SOURCE_EXTENDED, _POSIX_SOURCE, _POSIX_C_SOURCE with the value 200112L (199506L in glibc versions before 2.5), and _XOPEN_SOURCE with the value 600 (500 in glibc versions before 2.2). In addition, various GNU-specific extensions are also exposed. Where standards conflict, BSD definitions are disfavored.
Defining this macro exposes definitions of certain reentrant functions. For multithreaded programs, use cc -pthread instead.
Synonym for _REENTRANT, provided for compatibility with some other implementations.
_FORTIFY_SOURCE (since glibc 2.3.4)
Defining this macro causes some lightweight checks to be performed to detect some buffer overflow errors when employing various string and memory manipulation functions. Not all buffer overflows are detected, just some common cases. In the current implementation checks are added for calls to memcpy(3), mempcpy(3), memmove(3), memset(3), stpcpy(3), strcpy(3), strncpy(3), strcat(3), strncat(3), sprintf(3), snprintf(3), vsprintf(3), vsnprintf(3), and gets(3). If _FORTIFY_SOURCE is set to 1, with compiler optimization level 1 (gcc -O1) and above, checks that shouldn't change the behavior of conforming programs are performed. With _FORTIFY_SOURCE set to 2 some more checking is added, but some conforming programs might fail. Some of the checks can be performed at compile time, and result in compiler warnings; other checks take place at run time, and result in a run-time error if the check fails. Use of this macro requires compiler support, available with gcc(1) since version 4.0.

Default definitions, implicit definitions, and combining definitions

If no feature test macros are explicitly defined, then the following feature test macros are defined by default: _BSD_SOURCE, _SVID_SOURCE, _POSIX_SOURCE, and _POSIX_C_SOURCE=200112L (199506L in glibc versions before 2.4).

If any of __STRICT_ANSI__, _ISOC99_SOURCE, _POSIX_SOURCE, _POSIX_C_SOURCE, _XOPEN_SOURCE, _XOPEN_SOURCE_EXTENDED, _BSD_SOURCE, or _SVID_SOURCE is explicitly defined, then _BSD_SOURCE, and _SVID_SOURCE are not defined by default.

If _POSIX_SOURCE and _POSIX_C_SOURCE are not explicitly defined, and either __STRICT_ANSI__ is not defined or _XOPEN_SOURCE is defined with a value of 500 or more, then

_POSIX_SOURCE is defined with the value 1; and
_POSIX_C_SOURCE is defined with one of the following values:
2, if XOPEN_SOURCE is defined with a value less than 500;
199506L, if XOPEN_SOURCE is defined with a value greater than or equal to 500 and less than 600; or
200112L (199506L in glibc versions before 2.4), if XOPEN_SOURCE is undefined, or is defined with a value greater than or equal to 600.

Multiple macros can be defined; the results are additive.


POSIX.1 specifies _POSIX_C_SOURCE, _POSIX_SOURCE, and _XOPEN_SOURCE. _XOPEN_SOURCE_EXTENDED was specified by XPG4v2 (aka SUSv1).

_FILE_OFFSET_BITS is not specified by any standard, but is employed on some other implementations.



<features.h> is a Linux/glibc specific header file. Other systems have an analogous file, but typically with a different name. This header file is automatically included by other header files as required: it is not necessary to explicitly include it in order to employ feature test macros.

According to which of the above feature test macros are defined, <features.h> internally defines various other macros that are checked by other glibc header files. These macros have names prefixed by two underscores (e.g., __USE_MISC). Programs should never define these macros directly: instead, the appropriate feature test macro(s) from the list above should be employed.



The section "Feature Test Macros" under info libc.