feature_test_macros(7)

๐ŸŽ€ feature_test_macros(7) ๐ŸŽ€

okay so bestie, this is the Miscellaneous Information Manual entry for feature_test_macros, and honestly? it's giving "the lore you need before literally any header file behaves the way you want". buckle up ๐Ÿฆ‹


โœจ NAME โœจ

feature_test_macros โ€” feature test macros ๐Ÿ’…


๐ŸŒธ DESCRIPTION ๐ŸŒธ

Feature test macros are the little switches that let you, the programmer, control which definitions get exposed by system header files when a program is compiled. you're basically telling the headers "hey, show me this, keep that hidden" ๐Ÿซถ

NOTE: bestie, in order to be effective, a feature test macro must be defined before including any header files. you can do it either in the compilation command (cc -DMACRO=value) or by defining the macro inside the source code before including any headers. and why does it have to come first? because header files may freely include one another ๐Ÿ’€ so for example, in the following lines, defining the _GNU_SOURCE macro may have no effect, because the header <abc.h> itself includes <xyz.h> (POSIX explicitly allows this):
#include <abc.h>
#define _GNU_SOURCE
#include <xyz.h>

Some feature test macros are useful for creating portable applications, by preventing nonstandard definitions from being exposed. Other macros can be used to expose nonstandard definitions that are not exposed by default. pick your fighter โœจ

The precise effects of each of the feature test macros described below can be ascertained by inspecting the <features.h> header file. Note: applications do not need to directly include <features.h>; indeed, doing so is actively discouraged. See NOTES. ๐Ÿ‘€


๐Ÿ’œ 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 acct(2) manual page):

       #include <unistd.h>

       int acct(const char *filename);

   Feature Test Macro Requirements for glibc (see
   feature_test_macros(7)):

       acct(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)

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

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

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

    cc -D_BSD_SOURCE
    cc -D_XOPEN_SOURCE           # Or any value < 500

Note that, as described below, some feature test macros are defined by default, so that it may not always be necessary to explicitly specify the feature test macro(s) shown in the SYNOPSIS. so like, sometimes it's already handled for you, no cap ๐Ÿ˜Œ

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

    #define _GNU_SOURCE
    #define _FILE_OFFSET_BITS 64
    #include <fcntl.h>

    ssize_t readahead(int fd, off_t *offset, size_t count);

This format is employed when the feature test macros ensure that the proper function declarations are visible, and the macros are not defined by default. ๐Ÿ”ฅ


๐Ÿฆ‹ Feature test macros understood by glibc ๐Ÿฆ‹

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

First, though, a summary of a few details for the impatient: ๐Ÿƒโ™€๏ธ

  • The macros that you most likely need to use in modern source code are _POSIX_C_SOURCE (for definitions from various versions of POSIX.1), _XOPEN_SOURCE (for definitions from various versions of SUS), _GNU_SOURCE (for GNU and/or Linux specific stuff), and _DEFAULT_SOURCE (to get definitions that would normally be provided by default).
  • Certain macros are defined with default values. Thus, although one or more macros may be indicated as being required in the SYNOPSIS of a man page, it may not be necessary to define them explicitly. Full details of the defaults are given later in this man page.

glibc understands the following feature test macros:

  • __STRICT_ANSI__ โ€” ISO Standard C. This macro is implicitly defined by gcc(1) when invoked with, for example, the -std=c99 or -ansi flag. it just shows up, you didn't even invite it ๐Ÿ’€
  • _POSIX_C_SOURCE โ€” Defining this macro causes header files to expose definitions conforming to the following standards, depending on the value.
    • >= 1 โ€” POSIX.1-1990. POSIX.1-1990 implies ISO C90.
    • >= 2 โ€” POSIX.2-1992.
    • >= 199309L โ€” POSIX.1b (real-time extensions).
    • 199506L (since glibc 2.1) โ€” POSIX.1c (threads).
    • 200112L (since glibc 2.3.3) โ€” POSIX.1-2001 (excluding the XSI extension). POSIX.1-2001 implies ISO C99.
    • 200809L (since glibc 2.10) โ€” POSIX.1-2008 (excluding the XSI extension). POSIX.1-2008 implies ISO C99.
  • _POSIX_SOURCE [[deprecated]] โ€” Equivalent to -D_POSIX_C_SOURCE=1. old news, she's retired bestie ๐Ÿ™ˆ
  • _XOPEN_SOURCE โ€” Defining this macro causes header files to expose definitions conforming to the following standards, depending on the value.
    • 1 โ€” POSIX.1, POSIX.2, XPG4.
    • 500 โ€” SUSv2 (Issue 5).
    • 600 (since glibc 2.2) โ€” SUSv3 (Issue 6). SUSv3 is POSIX.1-2001 plus the XSI extension.
    • 700 (since glibc 2.10) โ€” SUSv4 (Issue 7). SUSv4 is POSIX.1-2008 plus the XSI extension.
  • _XOPEN_SOURCE_EXTENDED [[deprecated]] โ€” Defining this macro causes header files to expose definitions conforming to SUSv1.
  • _ISOCxx_SOURCE โ€” Defining one of these macros causes header files to expose definitions conforming to corresponding version of the ISO C standard. They should not be defined by programmers. Instead, the language version should be specified with the corresponding compiler flag. (yes this is the "let the compiler do it, girl" moment ๐Ÿ’…)
    • _ISOC99_SOURCE (since glibc 2.1.3) โ€” C99. Use -std=c99 or -std=gnu99 instead.
    • _ISOC11_SOURCE (since glibc 2.16) โ€” C11. Use -std=c11 or -std=gnu11 instead.
    • _ISOC23_SOURCE (since glibc 2.40), _ISOC2X_SOURCE (since glibc 2.31) [[deprecated]] โ€” C23. Use -std=c23 or -std=gnu23 instead. Exposes declarations consistent with the ISO C23 standard.
  • _LARGEFILE64_SOURCE [[deprecated]] โ€” 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 macro; instead _FILE_OFFSET_BITS=64 should be employed. new code, new choices, periodt โญ
  • _LARGEFILE_SOURCE [[deprecated]] โ€” This macro was historically used to expose certain functions (specifically, fseeko(3) and ftello(3)) that address limitations of earlier APIs (fseek(3) and ftell(3)) that use long for file offsets. These functions are now part of SUSv2; use _XOPEN_SOURCE instead. Alternatively, defining _FILE_OFFSET_BITS with the value 64 also serves for this.
  • _FILE_OFFSET_BITS โ€” Defining this macro with the value 64 automatically converts references to 32-bit functions and data types related to file I/O and filesystem operations into references to their 64-bit counterparts. This is useful for performing I/O on large files (> 2 Gigabytes) on 32-bit systems. It is also useful when calling functions like copy_file_range(2) that were added more recently and that come only in 64-bit flavors. (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. so on modern hardware it's givingโ€ฆ nothing ๐Ÿ˜ญ
  • _TIME_BITS โ€” Defining this macro with the value 64 changes the width of time_t(3type) to 64-bit which allows handling of timestamps beyond 2038. It is closely related to _FILE_OFFSET_BITS and depending on implementation, may require it set. This macro is available as of glibc 2.34. yes we are defeating the 2038 problem, iconic behavior ๐Ÿ‘‘
  • _BSD_SOURCE [[deprecated]] โ€” Defining this macro with any value causes header files to expose BSD-derived definitions. In glibc versions up to and including 2.18, 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. Since glibc 2.19, _BSD_SOURCE no longer causes BSD definitions to be preferred in case of conflicts. Since glibc 2.20, this macro is deprecated. It now has the same effect as defining _DEFAULT_SOURCE, but generates a compile-time warning (unless _DEFAULT_SOURCE is also defined). Use _DEFAULT_SOURCE instead. To allow code that requires _BSD_SOURCE in glibc 2.19 and earlier and _DEFAULT_SOURCE in glibc 2.20 and later to compile without warnings, define both _BSD_SOURCE and _DEFAULT_SOURCE. ok dual-wielding, we love to see it ๐Ÿซถ
  • _SVID_SOURCE [[deprecated]] โ€” Defining this macro with any value causes header files to expose System V-derived definitions. (SVID == System V Interface Definition; see standards(7).) Since glibc 2.20, this macro is deprecated in the same fashion as _BSD_SOURCE.
  • _DEFAULT_SOURCE (since glibc 2.19) โ€” This macro can be defined to ensure that the "default" definitions are provided even when the defaults would otherwise be disabled, as happens when individual macros are explicitly defined, or the compiler is invoked in one of its "standard" modes (e.g., cc -std=c99). Defining _DEFAULT_SOURCE without defining other individual macros or invoking the compiler in one of its "standard" modes has no effect. The "default" definitions comprise those required by POSIX.1-2008 and ISO C99, as well as various definitions originally derived from BSD and System V. On glibc 2.19 and earlier, these defaults were approximately equivalent to explicitly defining the following:
    cc -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809
  • _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). Since glibc 2.10, this macro is also implicitly defined if _POSIX_C_SOURCE is defined with a value greater than or equal to 200809L. ๐ŸŒˆ
  • _GNU_SOURCE โ€” the "give me literally EVERYTHING" macro ๐Ÿ‘‘๐Ÿ’… no cap, this is the one that opens every door.

Defining this macro (with any value โ€” literally any, bestie) implicitly defines _ATFILE_SOURCE, _LARGEFILE64_SOURCE, _ISOC99_SOURCE, _XOPEN_SOURCE_EXTENDED, _POSIX_SOURCE, _POSIX_C_SOURCE with the value 200809L (200112L before glibc 2.10; 199506L before glibc 2.5; 199309L before glibc 2.1) and _XOPEN_SOURCE with the value 700 (600 before glibc 2.10; 500 before glibc 2.2). In addition, various GNU-specific extensions are also exposed. โœจ

Since glibc 2.19, defining _GNU_SOURCE also has the effect of implicitly defining _DEFAULT_SOURCE. Before glibc 2.20, defining _GNU_SOURCE also had the effect of implicitly defining _BSD_SOURCE and _SVID_SOURCE.

  • _REENTRANT [[deprecated]] โ€” grandma macro, still in the group chat but like, respectfully, she's retired ๐Ÿซถ

Historically, on various C libraries it was necessary to define this macro in all multithreaded code. (Some C libraries may still require this.) In glibc, this macro also exposed definitions of certain reentrant functions.

However, glibc has been thread-safe by default for many years; since glibc 2.3, the only effect of defining _REENTRANT has been to enable one or two of the same declarations that are also enabled by defining _POSIX_C_SOURCE with a value of 199506L or greater.

_REENTRANT is now obsolete. In glibc 2.25 and later, defining _REENTRANT is equivalent to defining _POSIX_C_SOURCE with the value 199506L. If a higher POSIX conformance level is selected by any other means (such as _POSIX_C_SOURCE itself, _XOPEN_SOURCE, _DEFAULT_SOURCE, or _GNU_SOURCE), then defining _REENTRANT has no effect.

This macro is automatically defined if one compiles with cc -pthread. ๐Ÿ”ฅ

  • _THREAD_SAFE [[deprecated]] โ€” yeah she's just _REENTRANT in a different outfit ๐Ÿ’€

Synonym for _REENTRANT, provided for compatibility with some other implementations.

  • _FORTIFY_SOURCE (since glibc 2.3.4) โ€” your personal bodyguard for buffer overflows ๐Ÿ›ก๏ธโœจ

Defining this macro causes some lightweight checks to be performed to detect some buffer overflow errors when employing various string and memory manipulation functions (for example, memcpy(3), memset(3), stpcpy(3), strcpy(3), strncpy(3), strcat(3), strncat(3), sprintf(3), snprintf(3), vsprintf(3), vsnprintf(3), gets(3), and wide character variants thereof). For some functions, argument consistency is checked; for example, a check is made that open(2) has been supplied with a mode argument when the specified flags include O_CREAT. Not all problems are detected, just some common cases.

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 (via macros logic implemented in header files), and result in compiler warnings; other checks take place at run time, and result in a run-time error if the check fails.

With _FORTIFY_SOURCE set to 3, additional checking is added to intercept some function calls used with an argument of variable size where the compiler can deduce an upper bound for its value. For example, a program where malloc(3)'s size argument is variable can now be fortified.

Use of this macro requires compiler support, available since gcc 4.0 and clang 2.6. Use of _FORTIFY_SOURCE set to 3 requires gcc 12.0 or later, or clang 9.0 or later, in conjunction with glibc 2.33 or later.


๐Ÿง  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 (in glibc 2.19 and earlier), _SVID_SOURCE (in glibc 2.19 and earlier), _DEFAULT_SOURCE (since glibc 2.19), _POSIX_SOURCE, and _POSIX_C_SOURCE=200809L (200112L before glibc 2.10; 199506L before glibc 2.4; 199309L before glibc 2.1). ๐Ÿ’–

If any of __STRICT_ANSI__, _ISOC99_SOURCE, _ISOC11_SOURCE (since glibc 2.18), _ISOC23_SOURCE (since glibc 2.40), _POSIX_SOURCE, _POSIX_C_SOURCE, _XOPEN_SOURCE, _XOPEN_SOURCE_EXTENDED (in glibc 2.11 and earlier), _BSD_SOURCE (in glibc 2.19 and earlier), or _SVID_SOURCE (in glibc 2.19 and earlier) is explicitly defined, then _BSD_SOURCE, _SVID_SOURCE, and _DEFAULT_SOURCE are not defined by default. ๐Ÿ˜ญ the vibe shift is real


๐ŸŒˆ STANDARDS ๐ŸŒˆ

POSIX.1 specifies _POSIX_C_SOURCE, _POSIX_SOURCE, and _XOPEN_SOURCE.

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

_BSD_SOURCE, _SVID_SOURCE, _DEFAULT_SOURCE, _ATFILE_SOURCE, _GNU_SOURCE, _FORTIFY_SOURCE, _REENTRANT, and _THREAD_SAFE are specific to glibc. ๐Ÿ 


๐Ÿ“œ HISTORY ๐Ÿ“œ

_XOPEN_SOURCE_EXTENDED was specified by XPG4v2 (aka SUSv1), but is not present in SUSv2 and later. gone but not forgotten ๐Ÿ•ฏ๏ธ


๐Ÿ“ NOTES ๐Ÿ“

<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).

bestie listen: programs should never define these macros directly ๐Ÿ™ˆ instead, the appropriate feature test macro(s) from the list above should be employed. periodt.

๐ŸŽ€ EXAMPLES ๐ŸŽ€

The program below can be used to explore how the various feature test macros are set depending on the glibc version and what feature test macros are explicitly set. The following shell session, on a system with glibc 2.10, shows some examples of what we would see:

$ cc ftm.c
$ ./a.out
_POSIX_SOURCE defined
_POSIX_C_SOURCE defined: 200809L
_BSD_SOURCE defined
_SVID_SOURCE defined
_ATFILE_SOURCE defined
$ cc -D_XOPEN_SOURCE=500 ftm.c
$ ./a.out
_POSIX_SOURCE defined
_POSIX_C_SOURCE defined: 199506L
_XOPEN_SOURCE defined: 500
$ cc -D_GNU_SOURCE ftm.c
$ ./a.out
_POSIX_SOURCE defined
_POSIX_C_SOURCE defined: 200809L
_ISOC99_SOURCE defined
_XOPEN_SOURCE defined: 700
_XOPEN_SOURCE_EXTENDED defined
_LARGEFILE64_SOURCE defined
_BSD_SOURCE defined
_SVID_SOURCE defined
_ATFILE_SOURCE defined
_GNU_SOURCE defined

look at all those defines popping up, it's giving โœจslayโœจ

๐Ÿ’พ Program source ๐Ÿ’พ

/* ftm.c */

#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
#ifdef _POSIX_SOURCE
    printf("_POSIX_SOURCE defined\n");
#endif

#ifdef _POSIX_C_SOURCE
    printf("_POSIX_C_SOURCE defined: %jdL\n",
            (intmax_t) _POSIX_C_SOURCE);
#endif

#ifdef _ISOC99_SOURCE
    printf("_ISOC99_SOURCE defined\n");
#endif

#ifdef _ISOC11_SOURCE
    printf("_ISOC11_SOURCE defined\n");
#endif

#ifdef _ISOC23_SOURCE
    printf("_ISOC23_SOURCE defined\n");
#endif

#ifdef _XOPEN_SOURCE
    printf("_XOPEN_SOURCE defined: %d\n", _XOPEN_SOURCE);
#endif

#ifdef _XOPEN_SOURCE_EXTENDED
    printf("_XOPEN_SOURCE_EXTENDED defined\n");
#endif

#ifdef _LARGEFILE64_SOURCE
    printf("_LARGEFILE64_SOURCE defined\n");
#endif

#ifdef _FILE_OFFSET_BITS
    printf("_FILE_OFFSET_BITS defined: %d\n", _FILE_OFFSET_BITS);
#endif

#ifdef _TIME_BITS
    printf("_TIME_BITS defined: %d\n", _TIME_BITS);
#endif

#ifdef _BSD_SOURCE
    printf("_BSD_SOURCE defined\n");
#endif

#ifdef _SVID_SOURCE
    printf("_SVID_SOURCE defined\n");
#endif

#ifdef _DEFAULT_SOURCE
    printf("_DEFAULT_SOURCE defined\n");
#endif

#ifdef _ATFILE_SOURCE
    printf("_ATFILE_SOURCE defined\n");
#endif

#ifdef _GNU_SOURCE
    printf("_GNU_SOURCE defined\n");
#endif

#ifdef _REENTRANT
    printf("_REENTRANT defined\n");
#endif

#ifdef _THREAD_SAFE
    printf("_THREAD_SAFE defined\n");
#endif

#ifdef _FORTIFY_SOURCE
    printf("_FORTIFY_SOURCE defined\n");
#endif

    exit(EXIT_SUCCESS);
}

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€


and that's the whole page, bestie, you ate it ๐Ÿ’…๐ŸŒธ ok but fr, this was Linux man-pages 6.19, dated 2026-06-24 โ€” go forth and define your macros responsibly, mwah ๐Ÿซถโœจ

๐Ÿ“ฆ where this page comes from