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 #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.
416 Defining this macro with the value 64
419 to 64-bit which allows handling of timestamps beyond
421 It is closely related to
423 and depending on implementation, may require it set.
424 This macro is available as of glibc 2.34.
426 .BR _BSD_SOURCE " (deprecated since glibc 2.20)"
427 Defining this macro with any value causes header files to expose
428 BSD-derived definitions.
430 In glibc versions up to and including 2.18,
431 defining this macro also causes BSD definitions to be preferred in
432 some situations where standards conflict, unless one or more of
435 .BR _POSIX_C_SOURCE ,
437 .BR _XOPEN_SOURCE_EXTENDED ,
440 is defined, in which case BSD definitions are disfavored.
443 no longer causes BSD definitions to be preferred in case of conflicts.
445 Since glibc 2.20, this macro is deprecated.
446 .\" commit c941736c92fa3a319221f65f6755659b2a5e0a20
447 .\" commit 498afc54dfee41d33ba519f496e96480badace8e
448 .\" commit acd7f096d79c181866d56d4aaf3b043e741f1e2c
449 It now has the same effect as defining
450 .BR _DEFAULT_SOURCE ,
451 but generates a compile-time warning (unless
453 .\" commit ade40b10ff5fa59a318cf55b9d8414b758e8df78
458 To allow code that requires
460 in glibc 2.19 and earlier and
462 in glibc 2.20 and later to compile without warnings, define
466 .BR _DEFAULT_SOURCE .
468 .BR _SVID_SOURCE " (deprecated since glibc 2.20)"
469 Defining this macro with any value causes header files to expose
470 System V-derived definitions.
471 (SVID == System V Interface Definition; see
474 Since glibc 2.20, this macro is deprecated in the same fashion as
477 .BR _DEFAULT_SOURCE " (since glibc 2.19)"
478 This macro can be defined to ensure that the "default"
479 definitions are provided even when the defaults would otherwise
481 as happens when individual macros are explicitly defined,
482 or the compiler is invoked in one of its "standard" modes (e.g.,
486 without defining other individual macros
487 or invoking the compiler in one of its "standard" modes has no effect.
489 The "default" definitions comprise those required by POSIX.1-2008 and ISO C99,
490 as well as various definitions originally derived from BSD and System V.
491 On glibc 2.19 and earlier, these defaults were approximately equivalent
492 to explicitly defining the following:
496 cc \-D_BSD_SOURCE \-D_SVID_SOURCE \-D_POSIX_C_SOURCE=200809
500 .BR _ATFILE_SOURCE " (since glibc 2.4)"
501 Defining this macro with any value causes header files to expose
502 declarations of a range of functions with the suffix "at";
505 Since glibc 2.10, this macro is also implicitly defined if
507 is defined with a value greater than or equal to 200809L.
510 Defining this macro (with any value) implicitly defines
512 .BR _LARGEFILE64_SOURCE ,
514 .BR _XOPEN_SOURCE_EXTENDED ,
517 with the value 200809L
518 (200112L in glibc versions before 2.10;
519 199506L in glibc versions before 2.5;
520 199309L in glibc versions before 2.1)
524 (600 in glibc versions before 2.10;
525 500 in glibc versions before 2.2).
526 In addition, various GNU-specific extensions are also exposed.
528 Since glibc 2.19, defining
530 also has the effect of implicitly defining
531 .BR _DEFAULT_SOURCE .
532 In glibc versions before 2.20, defining
534 also had the effect of implicitly defining
540 Historically, on various C libraries
541 it was necessary to define this macro in all
544 .\" There did once exist C libraries where it was necessary. The ones
545 .\" I remember were proprietary Unix vendor libcs from the mid-1990s
546 .\" You would get completely unlocked stdio without _REENTRANT.
547 (Some C libraries may still require this.)
549 this macro also exposed definitions of certain reentrant functions.
551 However, glibc has been thread-safe by default for many years;
552 since glibc 2.3, the only effect of defining
554 has been to enable one or two of the same declarations that
555 are also enabled by defining
557 with a value of 199606L or greater.
561 In glibc 2.25 and later, defining
563 is equivalent to defining
565 with the value 199606L.
566 If a higher POSIX conformance level is
567 selected by any other means (such as
571 .BR _DEFAULT_SOURCE ,
578 This macro is automatically defined if one compiles with
582 Synonym for the (deprecated)
584 provided for compatibility with some other implementations.
586 .BR _FORTIFY_SOURCE " (since glibc 2.3.4)"
587 .\" For more detail, see:
588 .\" http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
589 .\" [PATCH] Object size checking to prevent (some) buffer overflows
590 .\" * From: Jakub Jelinek <jakub at redhat dot com>
591 .\" * To: gcc-patches at gcc dot gnu dot org
592 .\" * Date: Tue, 21 Sep 2004 04:16:40 -0400
593 Defining this macro causes some lightweight checks to be performed
594 to detect some buffer overflow errors when employing
595 various string and memory manipulation functions (for example,
608 and wide character variants thereof).
609 For some functions, argument consistency is checked;
610 for example, a check is made that
612 has been supplied with a
614 argument when the specified flags include
616 Not all problems are detected, just some common cases.
617 .\" Look for __USE_FORTIFY_LEVEL in the header files
621 is set to 1, with compiler optimization level 1
623 and above, checks that shouldn't change the behavior of
624 conforming programs are performed.
627 set to 2, some more checking is added, but
628 some conforming programs might fail.
629 .\" For example, given the following code
631 .\" char buf[1000], buf[1000];
632 .\" strcpy(fmt, "Hello world\n%n");
633 .\" snprintf(buf, sizeof(buf), fmt, &d);
635 .\" Compiling with "gcc -D_FORTIFY_SOURCE=2 -O1" and then running will
636 .\" cause the following diagnostic at run time at the snprintf() call
638 .\" *** %n in writable segment detected ***
639 .\" Aborted (core dumped)
642 Some of the checks can be performed at compile time
643 (via macros logic implemented in header files),
644 and result in compiler warnings;
645 other checks take place at run time,
646 and result in a run-time error if the check fails.
650 set to 3, additional checking is added to intercept
651 some function calls used with an argument of variable size
652 where the compiler can deduce an upper bound for its value.
653 For example, a program where
655 size argument is variable
656 can now be fortified.
658 Use of this macro requires compiler support, available with
666 version 12.0 or later.
667 .SS Default definitions, implicit definitions, and combining definitions
668 If no feature test macros are explicitly defined,
669 then the following feature test macros are defined by default:
671 (in glibc 2.19 and earlier),
673 (in glibc 2.19 and earlier),
678 .BR _POSIX_C_SOURCE =200809L
679 (200112L in glibc versions before 2.10;
680 199506L in glibc versions before 2.4;
681 199309L in glibc versions before 2.1).
684 .BR __STRICT_ANSI__ ,
689 .BR _POSIX_C_SOURCE ,
691 .B _XOPEN_SOURCE_EXTENDED
692 (in glibc 2.11 and earlier),
694 (in glibc 2.19 and earlier),
697 (in glibc 2.19 and earlier)
698 is explicitly defined, then
703 are not defined by default.
709 are not explicitly defined,
714 is defined with a value of 500 or more, then
717 is defined with the value 1; and
720 is defined with one of the following values:
726 is defined with a value less than 500;
731 is defined with a value greater than or equal to 500 and less than 600;
734 (since glibc 2.4) 200112L,
737 is defined with a value greater than or equal to 600 and less than 700.
743 is defined with a value greater than or equal to 700.
745 Older versions of glibc do not know about the values
746 200112L and 200809L for
747 .BR _POSIX_C_SOURCE ,
748 and the setting of this macro will depend on the glibc version.
752 is undefined, then the setting of
754 depends on the glibc version:
755 199506L, in glibc versions before 2.4;
756 200112L, in glibc 2.4 to 2.9; and
757 200809L, since glibc 2.10.
760 Multiple macros can be defined; the results are additive.
763 .BR _POSIX_C_SOURCE ,
768 .B _XOPEN_SOURCE_EXTENDED
769 was specified by XPG4v2 (aka SUSv1), but is not present in SUSv2 and later.
771 is not specified by any standard,
772 but is employed on some other implementations.
776 .BR _DEFAULT_SOURCE ,
779 .BR _FORTIFY_SOURCE ,
783 are specific to Linux (glibc).
786 is a Linux/glibc-specific header file.
787 Other systems have an analogous file, but typically with a different name.
788 This header file is automatically included by other header files as
789 required: it is not necessary to explicitly include it in order to
790 employ feature test macros.
792 According to which of the above feature test macros are defined,
794 internally defines various other macros that are checked by
795 other glibc header files.
796 These macros have names prefixed by two underscores (e.g.,
800 define these macros directly:
801 instead, the appropriate feature test macro(s) from the
802 list above should be employed.
804 The program below can be used to explore how the various
805 feature test macros are set depending on the glibc version
806 and what feature test macros are explicitly set.
807 The following shell session, on a system with glibc 2.10,
808 shows some examples of what we would see:
814 _POSIX_SOURCE defined
815 _POSIX_C_SOURCE defined: 200809L
818 _ATFILE_SOURCE defined
819 $ \fBcc \-D_XOPEN_SOURCE=500 ftm.c\fP
821 _POSIX_SOURCE defined
822 _POSIX_C_SOURCE defined: 199506L
823 _XOPEN_SOURCE defined: 500
824 $ \fBcc \-D_GNU_SOURCE ftm.c\fP
826 _POSIX_SOURCE defined
827 _POSIX_C_SOURCE defined: 200809L
828 _ISOC99_SOURCE defined
829 _XOPEN_SOURCE defined: 700
830 _XOPEN_SOURCE_EXTENDED defined
831 _LARGEFILE64_SOURCE defined
834 _ATFILE_SOURCE defined
849 main(int argc, char *argv[])
852 printf("_POSIX_SOURCE defined\en");
855 #ifdef _POSIX_C_SOURCE
856 printf("_POSIX_C_SOURCE defined: %jdL\en",
857 (intmax_t) _POSIX_C_SOURCE);
860 #ifdef _ISOC99_SOURCE
861 printf("_ISOC99_SOURCE defined\en");
864 #ifdef _ISOC11_SOURCE
865 printf("_ISOC11_SOURCE defined\en");
869 printf("_XOPEN_SOURCE defined: %d\en", _XOPEN_SOURCE);
872 #ifdef _XOPEN_SOURCE_EXTENDED
873 printf("_XOPEN_SOURCE_EXTENDED defined\en");
876 #ifdef _LARGEFILE64_SOURCE
877 printf("_LARGEFILE64_SOURCE defined\en");
880 #ifdef _FILE_OFFSET_BITS
881 printf("_FILE_OFFSET_BITS defined: %d\en", _FILE_OFFSET_BITS);
885 printf("_TIME_BITS defined: %d\en", _TIME_BITS);
889 printf("_BSD_SOURCE defined\en");
893 printf("_SVID_SOURCE defined\en");
896 #ifdef _DEFAULT_SOURCE
897 printf("_DEFAULT_SOURCE defined\en");
900 #ifdef _ATFILE_SOURCE
901 printf("_ATFILE_SOURCE defined\en");
905 printf("_GNU_SOURCE defined\en");
909 printf("_REENTRANT defined\en");
913 printf("_THREAD_SAFE defined\en");
916 #ifdef _FORTIFY_SOURCE
917 printf("_FORTIFY_SOURCE defined\en");
926 .BR system_data_types (7)
928 The section "Feature Test Macros" under
930 .\" But beware: the info libc document is out of date (Jul 07, mtk)
932 .I /usr/include/features.h