1 .\" This manpage is Copyright (C) 2006, Michael Kerrisk
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH feature_test_macros 7 (date) "Linux man-pages (unreleased)"
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 #define _FILE_OFFSET_BITS 64
117 .B #include <fcntl.h>
119 .BI "ssize_t readahead(int " fd ", off_t *" offset ", size_t " count );
123 This format is employed when the feature test macros ensure
124 that the proper function declarations are visible,
125 and the macros are not defined by default.
126 .SS Feature test macros understood by glibc
127 The paragraphs below explain how feature test macros are handled
132 First, though, a summary of a few details for the impatient:
134 The macros that you most likely need to use in modern source code are
136 (for definitions from various versions of POSIX.1),
138 (for definitions from various versions of SUS),
140 (for GNU and/or Linux specific stuff), and
142 (to get definitions that would normally be provided by default).
144 Certain macros are defined with default values.
145 Thus, although one or more macros may be indicated as being
146 required in the SYNOPSIS of a man page,
147 it may not be necessary to define them explicitly.
148 Full details of the defaults are given later in this man page.
152 with a value of 600 or greater produces the same effects as defining
154 with a value of 200112L or greater.
159 _POSIX_C_SOURCE >= 200112L
163 in the feature test macro requirements in the SYNOPSIS of a man page,
164 it is implicit that the following has the same effect:
174 with a value of 700 or greater produces the same effects as defining
176 with a value of 200809L or greater.
181 _POSIX_C_SOURCE >= 200809L
185 in the feature test macro requirements in the SYNOPSIS of a man page,
186 it is implicit that the following has the same effect:
193 .\" The details in glibc 2.0 are simpler, but combining a
194 .\" a description of them with the details in later glibc versions
195 .\" would make for a complicated description.
197 glibc understands the following feature test macros:
201 This macro is implicitly defined by
203 when invoked with, for example, the
210 Defining this macro causes header files to expose definitions as follows:
213 The value 1 exposes definitions conforming to POSIX.1-1990 and
216 The value 2 or greater additionally exposes
217 definitions for POSIX.2-1992.
219 The value 199309L or greater additionally exposes
220 definitions for POSIX.1b (real-time extensions).
221 .\" 199506L functionality is available only since glibc 2.1
223 The value 199506L or greater additionally exposes
224 definitions for POSIX.1c (threads).
227 The value 200112L or greater additionally exposes definitions corresponding
228 to the POSIX.1-2001 base specification (excluding the XSI extension).
229 This value also causes C95 (since glibc 2.12) and
230 C99 (since glibc 2.10) features to be exposed
231 (in other words, the equivalent of defining
232 .BR _ISOC99_SOURCE ).
235 The value 200809L or greater additionally exposes definitions corresponding
236 to the POSIX.1-2008 base specification (excluding the XSI extension).
240 Defining this obsolete macro with any value is equivalent to defining
244 Since this macro is obsolete,
245 its usage is generally not documented when discussing
246 feature test macro requirements in the man pages.
249 Defining this macro causes header files to expose definitions as follows:
252 Defining with any value exposes
253 definitions conforming to POSIX.1, POSIX.2, and XPG4.
255 The value 500 or greater additionally exposes
256 definitions for SUSv2 (UNIX 98).
258 (Since glibc 2.2) The value 600 or greater additionally exposes
259 definitions for SUSv3 (UNIX 03; i.e., the POSIX.1-2001 base specification
260 plus the XSI extension) and C99 definitions.
262 (Since glibc 2.10) The value 700 or greater additionally exposes
263 definitions for SUSv4 (i.e., the POSIX.1-2008 base specification
264 plus the XSI extension).
271 is defined with a value greater than or equal to 500
277 is explicitly defined, then
278 the following macros are implicitly defined:
282 is defined with the value 1.
285 is defined, according to the value of
289 .BR _XOPEN_SOURCE " < 500"
291 is defined with the value 2.
293 .RB "500 <= " _XOPEN_SOURCE " < 600"
295 is defined with the value 199506L.
297 .RB "600 <= " _XOPEN_SOURCE " < 700"
299 is defined with the value 200112L.
301 .RB "700 <= " _XOPEN_SOURCE " (since glibc 2.10)"
303 is defined with the value 200809L.
307 In addition, defining
309 with a value of 500 or greater produces the same effects as defining
310 .BR _XOPEN_SOURCE_EXTENDED .
312 .B _XOPEN_SOURCE_EXTENDED
313 If this macro is defined,
316 is defined, then expose definitions corresponding to the XPG4v2
317 (SUSv1) UNIX extensions (UNIX 95).
320 with a value of 500 or more also produces the same effect as defining
321 .BR _XOPEN_SOURCE_EXTENDED .
323 .B _XOPEN_SOURCE_EXTENDED
324 in new source code should be avoided.
328 with a value of 500 or more has the same effect as defining
329 .BR _XOPEN_SOURCE_EXTENDED ,
330 the latter (obsolete) feature test macro is generally not described in the
331 SYNOPSIS in man pages.
333 .BR _ISOC99_SOURCE " (since glibc 2.1.3)"
334 Exposes declarations consistent with the ISO C99 standard.
336 Earlier glibc 2.1.x versions recognized an equivalent macro named
338 (because the C99 standard had not then been finalized).
339 Although the use of this macro is obsolete, glibc continues
340 to recognize it for backward compatibility.
344 also exposes ISO C (1990) Amendment 1 ("C95") definitions.
345 (The primary change in C95 was support for international character sets.)
347 Invoking the C compiler with the option
349 produces the same effects as defining this macro.
351 .BR _ISOC11_SOURCE " (since glibc 2.16)"
352 Exposes declarations consistent with the ISO C11 standard.
353 Defining this macro also enables C99 and C95 features (like
354 .BR _ISOC99_SOURCE ).
356 Invoking the C compiler with the option
358 produces the same effects as defining this macro.
360 .B _LARGEFILE64_SOURCE
361 Expose definitions for the alternative API specified by the
362 LFS (Large File Summit) as a "transitional extension" to the
363 Single UNIX Specification.
365 .UR http:\:/\:/opengroup.org\:/platform\:/lfs.html
367 The alternative API consists of a set of new objects
368 (i.e., functions and types) whose names are suffixed with "64"
377 New programs should not employ this macro; instead
378 .I _FILE_OFFSET_BITS=64
382 This macro was historically used to expose certain functions (specifically
386 that address limitations of earlier APIs
393 This macro is implicitly defined if
395 is defined with a value greater than or equal to 500.
396 New programs should not employ this macro;
399 as just described or defining
401 with the value 64 is the preferred mechanism to achieve the same result.
404 Defining this macro with the value 64
405 automatically converts references to 32-bit functions and data types
406 related to file I/O and filesystem operations into references to
407 their 64-bit counterparts.
408 This is useful for performing I/O on large files (> 2 Gigabytes)
410 It is also useful when calling functions like
411 .BR copy_file_range (2)
412 that were added more recently and that come only in 64-bit flavors.
413 (Defining this macro permits correctly written programs to use
414 large files with only a recompilation being required.)
416 64-bit systems naturally permit file sizes greater than 2 Gigabytes,
417 and on those systems this macro has no effect.
420 Defining this macro with the value 64
423 to 64-bit which allows handling of timestamps beyond
425 It is closely related to
427 and depending on implementation, may require it set.
428 This macro is available as of glibc 2.34.
430 .BR _BSD_SOURCE " (deprecated since glibc 2.20)"
431 Defining this macro with any value causes header files to expose
432 BSD-derived definitions.
434 In glibc versions up to and including 2.18,
435 defining this macro also causes BSD definitions to be preferred in
436 some situations where standards conflict, unless one or more of
439 .BR _POSIX_C_SOURCE ,
441 .BR _XOPEN_SOURCE_EXTENDED ,
444 is defined, in which case BSD definitions are disfavored.
447 no longer causes BSD definitions to be preferred in case of conflicts.
449 Since glibc 2.20, this macro is deprecated.
450 .\" commit c941736c92fa3a319221f65f6755659b2a5e0a20
451 .\" commit 498afc54dfee41d33ba519f496e96480badace8e
452 .\" commit acd7f096d79c181866d56d4aaf3b043e741f1e2c
453 It now has the same effect as defining
454 .BR _DEFAULT_SOURCE ,
455 but generates a compile-time warning (unless
457 .\" commit ade40b10ff5fa59a318cf55b9d8414b758e8df78
462 To allow code that requires
464 in glibc 2.19 and earlier and
466 in glibc 2.20 and later to compile without warnings, define
470 .BR _DEFAULT_SOURCE .
472 .BR _SVID_SOURCE " (deprecated since glibc 2.20)"
473 Defining this macro with any value causes header files to expose
474 System V-derived definitions.
475 (SVID == System V Interface Definition; see
478 Since glibc 2.20, this macro is deprecated in the same fashion as
481 .BR _DEFAULT_SOURCE " (since glibc 2.19)"
482 This macro can be defined to ensure that the "default"
483 definitions are provided even when the defaults would otherwise
485 as happens when individual macros are explicitly defined,
486 or the compiler is invoked in one of its "standard" modes (e.g.,
490 without defining other individual macros
491 or invoking the compiler in one of its "standard" modes has no effect.
493 The "default" definitions comprise those required by POSIX.1-2008 and ISO C99,
494 as well as various definitions originally derived from BSD and System V.
495 On glibc 2.19 and earlier, these defaults were approximately equivalent
496 to explicitly defining the following:
500 cc \-D_BSD_SOURCE \-D_SVID_SOURCE \-D_POSIX_C_SOURCE=200809
504 .BR _ATFILE_SOURCE " (since glibc 2.4)"
505 Defining this macro with any value causes header files to expose
506 declarations of a range of functions with the suffix "at";
509 Since glibc 2.10, this macro is also implicitly defined if
511 is defined with a value greater than or equal to 200809L.
514 Defining this macro (with any value) implicitly defines
516 .BR _LARGEFILE64_SOURCE ,
518 .BR _XOPEN_SOURCE_EXTENDED ,
521 with the value 200809L
522 (200112L before glibc 2.10;
523 199506L before glibc 2.5;
524 199309L before glibc 2.1)
528 (600 before glibc 2.10;
529 500 before glibc 2.2).
530 In addition, various GNU-specific extensions are also exposed.
532 Since glibc 2.19, defining
534 also has the effect of implicitly defining
535 .BR _DEFAULT_SOURCE .
536 Before glibc 2.20, defining
538 also had the effect of implicitly defining
544 Historically, on various C libraries
545 it was necessary to define this macro in all
548 .\" There did once exist C libraries where it was necessary. The ones
549 .\" I remember were proprietary Unix vendor libcs from the mid-1990s
550 .\" You would get completely unlocked stdio without _REENTRANT.
551 (Some C libraries may still require this.)
553 this macro also exposed definitions of certain reentrant functions.
555 However, glibc has been thread-safe by default for many years;
556 since glibc 2.3, the only effect of defining
558 has been to enable one or two of the same declarations that
559 are also enabled by defining
561 with a value of 199606L or greater.
565 In glibc 2.25 and later, defining
567 is equivalent to defining
569 with the value 199606L.
570 If a higher POSIX conformance level is
571 selected by any other means (such as
575 .BR _DEFAULT_SOURCE ,
582 This macro is automatically defined if one compiles with
586 Synonym for the (deprecated)
588 provided for compatibility with some other implementations.
590 .BR _FORTIFY_SOURCE " (since glibc 2.3.4)"
591 .\" For more detail, see:
592 .\" http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
593 .\" [PATCH] Object size checking to prevent (some) buffer overflows
594 .\" * From: Jakub Jelinek <jakub at redhat dot com>
595 .\" * To: gcc-patches at gcc dot gnu dot org
596 .\" * Date: Tue, 21 Sep 2004 04:16:40 -0400
597 Defining this macro causes some lightweight checks to be performed
598 to detect some buffer overflow errors when employing
599 various string and memory manipulation functions (for example,
612 and wide character variants thereof).
613 For some functions, argument consistency is checked;
614 for example, a check is made that
616 has been supplied with a
618 argument when the specified flags include
620 Not all problems are detected, just some common cases.
621 .\" Look for __USE_FORTIFY_LEVEL in the header files
625 is set to 1, with compiler optimization level 1
627 and above, checks that shouldn't change the behavior of
628 conforming programs are performed.
631 set to 2, some more checking is added, but
632 some conforming programs might fail.
633 .\" For example, given the following code
635 .\" char buf[1000], buf[1000];
636 .\" strcpy(fmt, "Hello world\n%n");
637 .\" snprintf(buf, sizeof(buf), fmt, &d);
639 .\" Compiling with "gcc -D_FORTIFY_SOURCE=2 -O1" and then running will
640 .\" cause the following diagnostic at run time at the snprintf() call
642 .\" *** %n in writable segment detected ***
643 .\" Aborted (core dumped)
646 Some of the checks can be performed at compile time
647 (via macros logic implemented in header files),
648 and result in compiler warnings;
649 other checks take place at run time,
650 and result in a run-time error if the check fails.
654 set to 3, additional checking is added to intercept
655 some function calls used with an argument of variable size
656 where the compiler can deduce an upper bound for its value.
657 For example, a program where
659 size argument is variable
660 can now be fortified.
662 Use of this macro requires compiler support, available since
663 gcc 4.0 and clang 2.6.
666 set to 3 requires gcc 12.0 or later, or clang 9.0 or later,
667 in conjunction with glibc 2.33 or later.
668 .\" glibc is not an absolute requirement (gcc has libssp; NetBSD/newlib
669 .\" and Darwin each have their own implementation), but let's keep it
671 .SS Default definitions, implicit definitions, and combining definitions
672 If no feature test macros are explicitly defined,
673 then the following feature test macros are defined by default:
675 (in glibc 2.19 and earlier),
677 (in glibc 2.19 and earlier),
682 .BR _POSIX_C_SOURCE =200809L
683 (200112L before glibc 2.10;
684 199506L before glibc 2.4;
685 199309L before glibc 2.1).
688 .BR __STRICT_ANSI__ ,
693 .BR _POSIX_C_SOURCE ,
695 .B _XOPEN_SOURCE_EXTENDED
696 (in glibc 2.11 and earlier),
698 (in glibc 2.19 and earlier),
701 (in glibc 2.19 and earlier)
702 is explicitly defined, then
707 are not defined by default.
713 are not explicitly defined,
718 is defined with a value of 500 or more, then
721 is defined with the value 1; and
724 is defined with one of the following values:
730 is defined with a value less than 500;
735 is defined with a value greater than or equal to 500 and less than 600;
738 (since glibc 2.4) 200112L,
741 is defined with a value greater than or equal to 600 and less than 700.
747 is defined with a value greater than or equal to 700.
749 Older versions of glibc do not know about the values
750 200112L and 200809L for
751 .BR _POSIX_C_SOURCE ,
752 and the setting of this macro will depend on the glibc version.
756 is undefined, then the setting of
758 depends on the glibc version:
759 199506L, before glibc 2.4;
760 200112L, since glibc 2.4 to glibc 2.9; and
761 200809L, since glibc 2.10.
764 Multiple macros can be defined; the results are additive.
767 .BR _POSIX_C_SOURCE ,
773 is not specified by any standard,
774 but is employed on some other implementations.
778 .BR _DEFAULT_SOURCE ,
781 .BR _FORTIFY_SOURCE ,
785 are specific to glibc.
787 .B _XOPEN_SOURCE_EXTENDED
788 was specified by XPG4v2 (aka SUSv1), but is not present in SUSv2 and later.
791 is a Linux/glibc-specific header file.
792 Other systems have an analogous file, but typically with a different name.
793 This header file is automatically included by other header files as
794 required: it is not necessary to explicitly include it in order to
795 employ feature test macros.
797 According to which of the above feature test macros are defined,
799 internally defines various other macros that are checked by
800 other glibc header files.
801 These macros have names prefixed by two underscores (e.g.,
805 define these macros directly:
806 instead, the appropriate feature test macro(s) from the
807 list above should be employed.
809 The program below can be used to explore how the various
810 feature test macros are set depending on the glibc version
811 and what feature test macros are explicitly set.
812 The following shell session, on a system with glibc 2.10,
813 shows some examples of what we would see:
819 _POSIX_SOURCE defined
820 _POSIX_C_SOURCE defined: 200809L
823 _ATFILE_SOURCE defined
824 $ \fBcc \-D_XOPEN_SOURCE=500 ftm.c\fP
826 _POSIX_SOURCE defined
827 _POSIX_C_SOURCE defined: 199506L
828 _XOPEN_SOURCE defined: 500
829 $ \fBcc \-D_GNU_SOURCE ftm.c\fP
831 _POSIX_SOURCE defined
832 _POSIX_C_SOURCE defined: 200809L
833 _ISOC99_SOURCE defined
834 _XOPEN_SOURCE defined: 700
835 _XOPEN_SOURCE_EXTENDED defined
836 _LARGEFILE64_SOURCE defined
839 _ATFILE_SOURCE defined
854 main(int argc, char *argv[])
857 printf("_POSIX_SOURCE defined\en");
860 #ifdef _POSIX_C_SOURCE
861 printf("_POSIX_C_SOURCE defined: %jdL\en",
862 (intmax_t) _POSIX_C_SOURCE);
865 #ifdef _ISOC99_SOURCE
866 printf("_ISOC99_SOURCE defined\en");
869 #ifdef _ISOC11_SOURCE
870 printf("_ISOC11_SOURCE defined\en");
874 printf("_XOPEN_SOURCE defined: %d\en", _XOPEN_SOURCE);
877 #ifdef _XOPEN_SOURCE_EXTENDED
878 printf("_XOPEN_SOURCE_EXTENDED defined\en");
881 #ifdef _LARGEFILE64_SOURCE
882 printf("_LARGEFILE64_SOURCE defined\en");
885 #ifdef _FILE_OFFSET_BITS
886 printf("_FILE_OFFSET_BITS defined: %d\en", _FILE_OFFSET_BITS);
890 printf("_TIME_BITS defined: %d\en", _TIME_BITS);
894 printf("_BSD_SOURCE defined\en");
898 printf("_SVID_SOURCE defined\en");
901 #ifdef _DEFAULT_SOURCE
902 printf("_DEFAULT_SOURCE defined\en");
905 #ifdef _ATFILE_SOURCE
906 printf("_ATFILE_SOURCE defined\en");
910 printf("_GNU_SOURCE defined\en");
914 printf("_REENTRANT defined\en");
918 printf("_THREAD_SAFE defined\en");
921 #ifdef _FORTIFY_SOURCE
922 printf("_FORTIFY_SOURCE defined\en");
931 .BR system_data_types (7)
933 The section "Feature Test Macros" under
935 .\" But beware: the info libc document is out of date (Jul 07, mtk)
937 .I /usr/include/features.h