1 .\" This manpage is Copyright (C) 2006, Michael Kerrisk
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH FEATURE_TEST_MACROS 7 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
7 feature_test_macros \- feature test macros
9 Feature test macros allow the programmer to control the definitions that
10 are exposed by system header files when a program is compiled.
13 In order to be effective, a feature test macro
14 .IR "must be defined before including any header files" .
15 This can be done either in the compilation command
16 .RI ( "cc \-DMACRO=value" )
17 or by defining the macro within the source code before
18 including any headers.
19 The requirement that the macro must be defined before including any
20 header file exists because header files may freely include one another.
21 Thus, for example, in the following lines, defining the
23 macro may have no effect because the header
27 (POSIX explicitly allows this):
37 Some feature test macros are useful for creating portable applications,
38 by preventing nonstandard definitions from being exposed.
39 Other macros can be used to expose nonstandard definitions that
40 are not exposed by default.
42 The precise effects of each of the feature test macros described below
43 can be ascertained by inspecting the
49 need to directly include
51 indeed, doing so is actively discouraged.
53 .SS Specification of feature test macro requirements in manual pages
54 When a function requires that a feature test macro is defined,
55 the manual page SYNOPSIS typically includes a note of the following form
56 (this example from the
61 .B #include <unistd.h>
63 .BI "int acct(const char *" filename );
67 Feature Test Macro Requirements for glibc (see
68 .BR feature_test_macros (7)):
73 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
78 means that in order to obtain the declaration of
83 of the following macro
84 definitions must be made before including any header files:
89 #define _XOPEN_SOURCE /* or any value < 500 */
93 Alternatively, equivalent definitions can be included in the
99 cc \-D_XOPEN_SOURCE # Or any value < 500
103 Note that, as described below,
104 .BR "some feature test macros are defined by default" ,
105 so that it may not always be necessary to
106 explicitly specify the feature test macro(s) shown in the
109 In a few cases, manual pages use a shorthand for expressing the
110 feature test macro requirements (this example from
115 .B #define _GNU_SOURCE
116 .B #include <fcntl.h>
118 .BI "ssize_t readahead(int " fd ", off64_t *" offset ", size_t " count );
122 This format is employed in cases where only a single
123 feature test macro can be used to expose the function
124 declaration, and that macro is not defined by default.
125 .SS Feature test macros understood by glibc
126 The paragraphs below explain how feature test macros are handled
131 First, though, a summary of a few details for the impatient:
133 The macros that you most likely need to use in modern source code are
135 (for definitions from various versions of POSIX.1),
137 (for definitions from various versions of SUS),
139 (for GNU and/or Linux specific stuff), and
141 (to get definitions that would normally be provided by default).
143 Certain macros are defined with default values.
144 Thus, although one or more macros may be indicated as being
145 required in the SYNOPSIS of a man page,
146 it may not be necessary to define them explicitly.
147 Full details of the defaults are given later in this man page.
151 with a value of 600 or greater produces the same effects as defining
153 with a value of 200112L or greater.
158 _POSIX_C_SOURCE >= 200112L
162 in the feature test macro requirements in the SYNOPSIS of a man page,
163 it is implicit that the following has the same effect:
173 with a value of 700 or greater produces the same effects as defining
175 with a value of 200809L or greater.
180 _POSIX_C_SOURCE >= 200809L
184 in the feature test macro requirements in the SYNOPSIS of a man page,
185 it is implicit that the following has the same effect:
192 .\" The details in glibc 2.0 are simpler, but combining a
193 .\" a description of them with the details in later glibc versions
194 .\" would make for a complicated description.
196 Glibc understands the following feature test macros:
200 This macro is implicitly defined by
202 when invoked with, for example, the
209 Defining this macro causes header files to expose definitions as follows:
212 The value 1 exposes definitions conforming to POSIX.1-1990 and
215 The value 2 or greater additionally exposes
216 definitions for POSIX.2-1992.
218 The value 199309L or greater additionally exposes
219 definitions for POSIX.1b (real-time extensions).
220 .\" 199506L functionality is available only since glibc 2.1
222 The value 199506L or greater additionally exposes
223 definitions for POSIX.1c (threads).
226 The value 200112L or greater additionally exposes definitions corresponding
227 to the POSIX.1-2001 base specification (excluding the XSI extension).
228 This value also causes C95 (since glibc 2.12) and
229 C99 (since glibc 2.10) features to be exposed
230 (in other words, the equivalent of defining
231 .BR _ISOC99_SOURCE ).
234 The value 200809L or greater additionally exposes definitions corresponding
235 to the POSIX.1-2008 base specification (excluding the XSI extension).
239 Defining this obsolete macro with any value is equivalent to defining
243 Since this macro is obsolete,
244 its usage is generally not documented when discussing
245 feature test macro requirements in the man pages.
248 Defining this macro causes header files to expose definitions as follows:
251 Defining with any value exposes
252 definitions conforming to POSIX.1, POSIX.2, and XPG4.
254 The value 500 or greater additionally exposes
255 definitions for SUSv2 (UNIX 98).
257 (Since glibc 2.2) The value 600 or greater additionally exposes
258 definitions for SUSv3 (UNIX 03; i.e., the POSIX.1-2001 base specification
259 plus the XSI extension) and C99 definitions.
261 (Since glibc 2.10) The value 700 or greater additionally exposes
262 definitions for SUSv4 (i.e., the POSIX.1-2008 base specification
263 plus the XSI extension).
270 is defined with a value greater than or equal to 500
276 is explicitly defined, then
277 the following macros are implicitly defined:
281 is defined with the value 1.
284 is defined, according to the value of
288 .BR _XOPEN_SOURCE " < 500"
290 is defined with the value 2.
292 .RB "500 <= " _XOPEN_SOURCE " < 600"
294 is defined with the value 199506L.
296 .RB "600 <= " _XOPEN_SOURCE " < 700"
298 is defined with the value 200112L.
300 .RB "700 <= " _XOPEN_SOURCE " (since glibc 2.10)"
302 is defined with the value 200809L.
306 In addition, defining
308 with a value of 500 or greater produces the same effects as defining
309 .BR _XOPEN_SOURCE_EXTENDED .
311 .B _XOPEN_SOURCE_EXTENDED
312 If this macro is defined,
315 is defined, then expose definitions corresponding to the XPG4v2
316 (SUSv1) UNIX extensions (UNIX 95).
319 with a value of 500 or more also produces the same effect as defining
320 .BR _XOPEN_SOURCE_EXTENDED .
322 .B _XOPEN_SOURCE_EXTENDED
323 in new source code should be avoided.
327 with a value of 500 or more has the same effect as defining
328 .BR _XOPEN_SOURCE_EXTENDED ,
329 the latter (obsolete) feature test macro is generally not described in the
330 SYNOPSIS in man pages.
332 .BR _ISOC99_SOURCE " (since glibc 2.1.3)"
333 Exposes declarations consistent with the ISO C99 standard.
335 Earlier glibc 2.1.x versions recognized an equivalent macro named
337 (because the C99 standard had not then been finalized).
338 Although the use of this macro is obsolete, glibc continues
339 to recognize it for backward compatibility.
343 also exposes ISO C (1990) Amendment 1 ("C95") definitions.
344 (The primary change in C95 was support for international character sets.)
346 Invoking the C compiler with the option
348 produces the same effects as defining this macro.
350 .BR _ISOC11_SOURCE " (since glibc 2.16)"
351 Exposes declarations consistent with the ISO C11 standard.
352 Defining this macro also enables C99 and C95 features (like
353 .BR _ISOC99_SOURCE ).
355 Invoking the C compiler with the option
357 produces the same effects as defining this macro.
359 .B _LARGEFILE64_SOURCE
360 Expose definitions for the alternative API specified by the
361 LFS (Large File Summit) as a "transitional extension" to the
362 Single UNIX Specification.
364 .UR http:\:/\:/opengroup.org\:/platform\:/lfs.html
366 The alternative API consists of a set of new objects
367 (i.e., functions and types) whose names are suffixed with "64"
376 New programs should not employ this macro; instead
377 .I _FILE_OFFSET_BITS=64
381 This macro was historically used to expose certain functions (specifically
385 that address limitations of earlier APIs
392 This macro is implicitly defined if
394 is defined with a value greater than or equal to 500.
395 New programs should not employ this macro;
398 as just described or defining
400 with the value 64 is the preferred mechanism to achieve the same result.
403 Defining this macro with the value 64
404 automatically converts references to 32-bit functions and data types
405 related to file I/O and filesystem operations into references to
406 their 64-bit counterparts.
407 This is useful for performing I/O on large files (> 2 Gigabytes)
409 (Defining this macro permits correctly written programs to use
410 large files with only a recompilation being required.)
412 64-bit systems naturally permit file sizes greater than 2 Gigabytes,
413 and on those systems this macro has no effect.
415 .BR _BSD_SOURCE " (deprecated since glibc 2.20)"
416 Defining this macro with any value causes header files to expose
417 BSD-derived definitions.
419 In glibc versions up to and including 2.18,
420 defining this macro also causes BSD definitions to be preferred in
421 some situations where standards conflict, unless one or more of
424 .BR _POSIX_C_SOURCE ,
426 .BR _XOPEN_SOURCE_EXTENDED ,
429 is defined, in which case BSD definitions are disfavored.
432 no longer causes BSD definitions to be preferred in case of conflicts.
434 Since glibc 2.20, this macro is deprecated.
435 .\" commit c941736c92fa3a319221f65f6755659b2a5e0a20
436 .\" commit 498afc54dfee41d33ba519f496e96480badace8e
437 .\" commit acd7f096d79c181866d56d4aaf3b043e741f1e2c
438 It now has the same effect as defining
439 .BR _DEFAULT_SOURCE ,
440 but generates a compile-time warning (unless
442 .\" commit ade40b10ff5fa59a318cf55b9d8414b758e8df78
447 To allow code that requires
449 in glibc 2.19 and earlier and
451 in glibc 2.20 and later to compile without warnings, define
455 .BR _DEFAULT_SOURCE .
457 .BR _SVID_SOURCE " (deprecated since glibc 2.20)"
458 Defining this macro with any value causes header files to expose
459 System V-derived definitions.
460 (SVID == System V Interface Definition; see
463 Since glibc 2.20, this macro is deprecated in the same fashion as
466 .BR _DEFAULT_SOURCE " (since glibc 2.19)"
467 This macro can be defined to ensure that the "default"
468 definitions are provided even when the defaults would otherwise
470 as happens when individual macros are explicitly defined,
471 or the compiler is invoked in one of its "standard" modes (e.g.,
475 without defining other individual macros
476 or invoking the compiler in one of its "standard" modes has no effect.
478 The "default" definitions comprise those required by POSIX.1-2008 and ISO C99,
479 as well as various definitions originally derived from BSD and System V.
480 On glibc 2.19 and earlier, these defaults were approximately equivalent
481 to explicitly defining the following:
485 cc \-D_BSD_SOURCE \-D_SVID_SOURCE \-D_POSIX_C_SOURCE=200809
489 .BR _ATFILE_SOURCE " (since glibc 2.4)"
490 Defining this macro with any value causes header files to expose
491 declarations of a range of functions with the suffix "at";
494 Since glibc 2.10, this macro is also implicitly defined if
496 is defined with a value greater than or equal to 200809L.
499 Defining this macro (with any value) implicitly defines
501 .BR _LARGEFILE64_SOURCE ,
503 .BR _XOPEN_SOURCE_EXTENDED ,
506 with the value 200809L
507 (200112L in glibc versions before 2.10;
508 199506L in glibc versions before 2.5;
509 199309L in glibc versions before 2.1)
513 (600 in glibc versions before 2.10;
514 500 in glibc versions before 2.2).
515 In addition, various GNU-specific extensions are also exposed.
517 Since glibc 2.19, defining
519 also has the effect of implicitly defining
520 .BR _DEFAULT_SOURCE .
521 In glibc versions before 2.20, defining
523 also had the effect of implicitly defining
529 Historically, on various C libraries
530 it was necessary to define this macro in all
533 .\" There did once exist C libraries where it was necessary. The ones
534 .\" I remember were proprietary Unix vendor libcs from the mid-1990s
535 .\" You would get completely unlocked stdio without _REENTRANT.
536 (Some C libraries may still require this.)
538 this macro also exposed definitions of certain reentrant functions.
540 However, glibc has been thread-safe by default for many years;
541 since glibc 2.3, the only effect of defining
543 has been to enable one or two of the same declarations that
544 are also enabled by defining
546 with a value of 199606L or greater.
550 In glibc 2.25 and later, defining
552 is equivalent to defining
554 with the value 199606L.
555 If a higher POSIX conformance level is
556 selected by any other means (such as
560 .BR _DEFAULT_SOURCE ,
567 This macro is automatically defined if one compiles with
571 Synonym for the (deprecated)
573 provided for compatibility with some other implementations.
575 .BR _FORTIFY_SOURCE " (since glibc 2.3.4)"
576 .\" For more detail, see:
577 .\" http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
578 .\" [PATCH] Object size checking to prevent (some) buffer overflows
579 .\" * From: Jakub Jelinek <jakub at redhat dot com>
580 .\" * To: gcc-patches at gcc dot gnu dot org
581 .\" * Date: Tue, 21 Sep 2004 04:16:40 -0400
582 Defining this macro causes some lightweight checks to be performed
583 to detect some buffer overflow errors when employing
584 various string and memory manipulation functions (for example,
597 and wide character variants thereof).
598 For some functions, argument consistency is checked;
599 for example, a check is made that
601 has been supplied with a
603 argument when the specified flags include
605 Not all problems are detected, just some common cases.
606 .\" Look for __USE_FORTIFY_LEVEL in the header files
610 is set to 1, with compiler optimization level 1
612 and above, checks that shouldn't change the behavior of
613 conforming programs are performed.
616 set to 2, some more checking is added, but
617 some conforming programs might fail.
618 .\" For example, given the following code
620 .\" char buf[1000], buf[1000];
621 .\" strcpy(fmt, "Hello world\n%n");
622 .\" snprintf(buf, sizeof(buf), fmt, &d);
624 .\" Compiling with "gcc -D_FORTIFY_SOURCE=2 -O1" and then running will
625 .\" cause the following diagnostic at run time at the snprintf() call
627 .\" *** %n in writable segment detected ***
628 .\" Aborted (core dumped)
631 Some of the checks can be performed at compile time
632 (via macros logic implemented in header files),
633 and result in compiler warnings;
634 other checks take place at run time,
635 and result in a run-time error if the check fails.
637 Use of this macro requires compiler support, available with
640 .SS Default definitions, implicit definitions, and combining definitions
641 If no feature test macros are explicitly defined,
642 then the following feature test macros are defined by default:
644 (in glibc 2.19 and earlier),
646 (in glibc 2.19 and earlier),
651 .BR _POSIX_C_SOURCE =200809L
652 (200112L in glibc versions before 2.10;
653 199506L in glibc versions before 2.4;
654 199309L in glibc versions before 2.1).
657 .BR __STRICT_ANSI__ ,
662 .BR _POSIX_C_SOURCE ,
664 .B _XOPEN_SOURCE_EXTENDED
665 (in glibc 2.11 and earlier),
667 (in glibc 2.19 and earlier),
670 (in glibc 2.19 and earlier)
671 is explicitly defined, then
676 are not defined by default.
682 are not explicitly defined,
687 is defined with a value of 500 or more, then
690 is defined with the value 1; and
693 is defined with one of the following values:
699 is defined with a value less than 500;
704 is defined with a value greater than or equal to 500 and less than 600;
707 (since glibc 2.4) 200112L,
710 is defined with a value greater than or equal to 600 and less than 700.
716 is defined with a value greater than or equal to 700.
718 Older versions of glibc do not know about the values
719 200112L and 200809L for
720 .BR _POSIX_C_SOURCE ,
721 and the setting of this macro will depend on the glibc version.
725 is undefined, then the setting of
727 depends on the glibc version:
728 199506L, in glibc versions before 2.4;
729 200112L, in glibc 2.4 to 2.9; and
730 200809L, since glibc 2.10.
733 Multiple macros can be defined; the results are additive.
736 .BR _POSIX_C_SOURCE ,
741 .B _XOPEN_SOURCE_EXTENDED
742 was specified by XPG4v2 (aka SUSv1), but is not present in SUSv2 and later.
744 is not specified by any standard,
745 but is employed on some other implementations.
749 .BR _DEFAULT_SOURCE ,
752 .BR _FORTIFY_SOURCE ,
756 are specific to Linux (glibc).
759 is a Linux/glibc-specific header file.
760 Other systems have an analogous file, but typically with a different name.
761 This header file is automatically included by other header files as
762 required: it is not necessary to explicitly include it in order to
763 employ feature test macros.
765 According to which of the above feature test macros are defined,
767 internally defines various other macros that are checked by
768 other glibc header files.
769 These macros have names prefixed by two underscores (e.g.,
773 define these macros directly:
774 instead, the appropriate feature test macro(s) from the
775 list above should be employed.
777 The program below can be used to explore how the various
778 feature test macros are set depending on the glibc version
779 and what feature test macros are explicitly set.
780 The following shell session, on a system with glibc 2.10,
781 shows some examples of what we would see:
787 _POSIX_SOURCE defined
788 _POSIX_C_SOURCE defined: 200809L
791 _ATFILE_SOURCE defined
792 $ \fBcc \-D_XOPEN_SOURCE=500 ftm.c\fP
794 _POSIX_SOURCE defined
795 _POSIX_C_SOURCE defined: 199506L
796 _XOPEN_SOURCE defined: 500
797 $ \fBcc \-D_GNU_SOURCE ftm.c\fP
799 _POSIX_SOURCE defined
800 _POSIX_C_SOURCE defined: 200809L
801 _ISOC99_SOURCE defined
802 _XOPEN_SOURCE defined: 700
803 _XOPEN_SOURCE_EXTENDED defined
804 _LARGEFILE64_SOURCE defined
807 _ATFILE_SOURCE defined
822 main(int argc, char *argv[])
825 printf("_POSIX_SOURCE defined\en");
828 #ifdef _POSIX_C_SOURCE
829 printf("_POSIX_C_SOURCE defined: %jdL\en",
830 (intmax_t) _POSIX_C_SOURCE);
833 #ifdef _ISOC99_SOURCE
834 printf("_ISOC99_SOURCE defined\en");
837 #ifdef _ISOC11_SOURCE
838 printf("_ISOC11_SOURCE defined\en");
842 printf("_XOPEN_SOURCE defined: %d\en", _XOPEN_SOURCE);
845 #ifdef _XOPEN_SOURCE_EXTENDED
846 printf("_XOPEN_SOURCE_EXTENDED defined\en");
849 #ifdef _LARGEFILE64_SOURCE
850 printf("_LARGEFILE64_SOURCE defined\en");
853 #ifdef _FILE_OFFSET_BITS
854 printf("_FILE_OFFSET_BITS defined: %d\en", _FILE_OFFSET_BITS);
858 printf("_BSD_SOURCE defined\en");
862 printf("_SVID_SOURCE defined\en");
865 #ifdef _DEFAULT_SOURCE
866 printf("_DEFAULT_SOURCE defined\en");
869 #ifdef _ATFILE_SOURCE
870 printf("_ATFILE_SOURCE defined\en");
874 printf("_GNU_SOURCE defined\en");
878 printf("_REENTRANT defined\en");
882 printf("_THREAD_SAFE defined\en");
885 #ifdef _FORTIFY_SOURCE
886 printf("_FORTIFY_SOURCE defined\en");
895 .BR system_data_types (7)
897 The section "Feature Test Macros" under
899 .\" But beware: the info libc document is out of date (Jul 07, mtk)
901 .I /usr/include/features.h