1 .\" This manpage is Copyright (C) 2006, Michael Kerrisk
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH FEATURE_TEST_MACROS 7 2021-03-22 "Linux" "Linux Programmer's Manual"
27 feature_test_macros \- feature test macros
29 Feature test macros allow the programmer to control the definitions that
30 are exposed by system header files when a program is compiled.
33 In order to be effective, a feature test macro
34 .IR "must be defined before including any header files" .
35 This can be done either in the compilation command
36 .RI ( "cc \-DMACRO=value" )
37 or by defining the macro within the source code before
38 including any headers.
39 The requirement that the macro must be defined before including any
40 header file exists because header files may freely include one another.
41 Thus, for example, in the following lines, defining the
43 macro may have no effect because the header
47 (POSIX explicitly allows this):
57 Some feature test macros are useful for creating portable applications,
58 by preventing nonstandard definitions from being exposed.
59 Other macros can be used to expose nonstandard definitions that
60 are not exposed by default.
62 The precise effects of each of the feature test macros described below
63 can be ascertained by inspecting the
69 need to directly include
71 indeed, doing so is actively discouraged.
73 .SS Specification of feature test macro requirements in manual pages
74 When a function requires that a feature test macro is defined,
75 the manual page SYNOPSIS typically includes a note of the following form
76 (this example from the
81 .B #include <unistd.h>
83 .BI "int acct(const char *" filename );
87 Feature Test Macro Requirements for glibc (see
88 .BR feature_test_macros (7)):
93 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
98 means that in order to obtain the declaration of
103 of the following macro
104 definitions must be made before including any header files:
109 #define _XOPEN_SOURCE /* or any value < 500 */
113 Alternatively, equivalent definitions can be included in the
119 cc \-D_XOPEN_SOURCE # Or any value < 500
123 Note that, as described below,
124 .BR "some feature test macros are defined by default" ,
125 so that it may not always be necessary to
126 explicitly specify the feature test macro(s) shown in the
129 In a few cases, manual pages use a shorthand for expressing the
130 feature test macro requirements (this example from
135 .B #define _GNU_SOURCE
136 .B #include <fcntl.h>
138 .BI "ssize_t readahead(int " fd ", off64_t *" offset ", size_t " count );
142 This format is employed in cases where only a single
143 feature test macro can be used to expose the function
144 declaration, and that macro is not defined by default.
145 .SS Feature test macros understood by glibc
146 The paragraphs below explain how feature test macros are handled
151 First, though, a summary of a few details for the impatient:
153 The macros that you most likely need to use in modern source code are
155 (for definitions from various versions of POSIX.1),
157 (for definitions from various versions of SUS),
159 (for GNU and/or Linux specific stuff), and
161 (to get definitions that would normally be provided by default).
163 Certain macros are defined with default values.
164 Thus, although one or more macros may be indicated as being
165 required in the SYNOPSIS of a man page,
166 it may not be necessary to define them explicitly.
167 Full details of the defaults are given later in this man page.
171 with a value of 600 or greater produces the same effects as defining
173 with a value of 200112L or greater.
178 _POSIX_C_SOURCE >= 200112L
182 in the feature test macro requirements in the SYNOPSIS of a man page,
183 it is implicit that the following has the same effect:
193 with a value of 700 or greater produces the same effects as defining
195 with a value of 200809L or greater.
200 _POSIX_C_SOURCE >= 200809L
204 in the feature test macro requirements in the SYNOPSIS of a man page,
205 it is implicit that the following has the same effect:
212 .\" The details in glibc 2.0 are simpler, but combining a
213 .\" a description of them with the details in later glibc versions
214 .\" would make for a complicated description.
216 Glibc understands the following feature test macros:
220 This macro is implicitly defined by
222 when invoked with, for example, the
229 Defining this macro causes header files to expose definitions as follows:
232 The value 1 exposes definitions conforming to POSIX.1-1990 and
235 The value 2 or greater additionally exposes
236 definitions for POSIX.2-1992.
238 The value 199309L or greater additionally exposes
239 definitions for POSIX.1b (real-time extensions).
240 .\" 199506L functionality is available only since glibc 2.1
242 The value 199506L or greater additionally exposes
243 definitions for POSIX.1c (threads).
246 The value 200112L or greater additionally exposes definitions corresponding
247 to the POSIX.1-2001 base specification (excluding the XSI extension).
248 This value also causes C95 (since glibc 2.12) and
249 C99 (since glibc 2.10) features to be exposed
250 (in other words, the equivalent of defining
251 .BR _ISOC99_SOURCE ).
254 The value 200809L or greater additionally exposes definitions corresponding
255 to the POSIX.1-2008 base specification (excluding the XSI extension).
259 Defining this obsolete macro with any value is equivalent to defining
263 Since this macro is obsolete,
264 its usage is generally not documented when discussing
265 feature test macro requirements in the man pages.
268 Defining this macro causes header files to expose definitions as follows:
271 Defining with any value exposes
272 definitions conforming to POSIX.1, POSIX.2, and XPG4.
274 The value 500 or greater additionally exposes
275 definitions for SUSv2 (UNIX 98).
277 (Since glibc 2.2) The value 600 or greater additionally exposes
278 definitions for SUSv3 (UNIX 03; i.e., the POSIX.1-2001 base specification
279 plus the XSI extension) and C99 definitions.
281 (Since glibc 2.10) The value 700 or greater additionally exposes
282 definitions for SUSv4 (i.e., the POSIX.1-2008 base specification
283 plus the XSI extension).
290 is defined with a value greater than or equal to 500
296 is explicitly defined, then
297 the following macros are implicitly defined:
301 is defined with the value 1.
304 is defined, according to the value of
308 .BR _XOPEN_SOURCE " < 500"
310 is defined with the value 2.
312 .RB "500 <= " _XOPEN_SOURCE " < 600"
314 is defined with the value 199506L.
316 .RB "600 <= " _XOPEN_SOURCE " < 700"
318 is defined with the value 200112L.
320 .RB "700 <= " _XOPEN_SOURCE " (since glibc 2.10)"
322 is defined with the value 200809L.
326 In addition, defining
328 with a value of 500 or greater produces the same effects as defining
329 .BR _XOPEN_SOURCE_EXTENDED .
331 .B _XOPEN_SOURCE_EXTENDED
332 If this macro is defined,
335 is defined, then expose definitions corresponding to the XPG4v2
336 (SUSv1) UNIX extensions (UNIX 95).
339 with a value of 500 or more also produces the same effect as defining
340 .BR _XOPEN_SOURCE_EXTENDED .
342 .BR _XOPEN_SOURCE_EXTENDED
343 in new source code should be avoided.
347 with a value of 500 or more has the same effect as defining
348 .BR _XOPEN_SOURCE_EXTENDED ,
349 the latter (obsolete) feature test macro is generally not described in the
350 SYNOPSIS in man pages.
352 .BR _ISOC99_SOURCE " (since glibc 2.1.3)"
353 Exposes declarations consistent with the ISO C99 standard.
355 Earlier glibc 2.1.x versions recognized an equivalent macro named
357 (because the C99 standard had not then been finalized).
358 Although the use of this macro is obsolete, glibc continues
359 to recognize it for backward compatibility.
363 also exposes ISO C (1990) Amendment 1 ("C95") definitions.
364 (The primary change in C95 was support for international character sets.)
366 Invoking the C compiler with the option
368 produces the same effects as defining this macro.
370 .BR _ISOC11_SOURCE " (since glibc 2.16)"
371 Exposes declarations consistent with the ISO C11 standard.
372 Defining this macro also enables C99 and C95 features (like
373 .BR _ISOC99_SOURCE ).
375 Invoking the C compiler with the option
377 produces the same effects as defining this macro.
379 .B _LARGEFILE64_SOURCE
380 Expose definitions for the alternative API specified by the
381 LFS (Large File Summit) as a "transitional extension" to the
382 Single UNIX Specification.
384 .UR http:\:/\:/opengroup.org\:/platform\:/lfs.html
386 The alternative API consists of a set of new objects
387 (i.e., functions and types) whose names are suffixed with "64"
396 New programs should not employ this macro; instead
397 .I _FILE_OFFSET_BITS=64
400 .BR _LARGEFILE_SOURCE
401 This macro was historically used to expose certain functions (specifically
405 that address limitations of earlier APIs
412 This macro is implicitly defined if
414 is defined with a value greater than or equal to 500.
415 New programs should not employ this macro;
418 as just described or defining
420 with the value 64 is the preferred mechanism to achieve the same result.
423 Defining this macro with the value 64
424 automatically converts references to 32-bit functions and data types
425 related to file I/O and filesystem operations into references to
426 their 64-bit counterparts.
427 This is useful for performing I/O on large files (> 2 Gigabytes)
429 (Defining this macro permits correctly written programs to use
430 large files with only a recompilation being required.)
432 64-bit systems naturally permit file sizes greater than 2 Gigabytes,
433 and on those systems this macro has no effect.
435 .BR _BSD_SOURCE " (deprecated since glibc 2.20)"
436 Defining this macro with any value causes header files to expose
437 BSD-derived definitions.
439 In glibc versions up to and including 2.18,
440 defining this macro also causes BSD definitions to be preferred in
441 some situations where standards conflict, unless one or more of
444 .BR _POSIX_C_SOURCE ,
446 .BR _XOPEN_SOURCE_EXTENDED ,
449 is defined, in which case BSD definitions are disfavored.
452 no longer causes BSD definitions to be preferred in case of conflicts.
454 Since glibc 2.20, this macro is deprecated.
455 .\" commit c941736c92fa3a319221f65f6755659b2a5e0a20
456 .\" commit 498afc54dfee41d33ba519f496e96480badace8e
457 .\" commit acd7f096d79c181866d56d4aaf3b043e741f1e2c
458 It now has the same effect as defining
459 .BR _DEFAULT_SOURCE ,
460 but generates a compile-time warning (unless
462 .\" commit ade40b10ff5fa59a318cf55b9d8414b758e8df78
467 To allow code that requires
469 in glibc 2.19 and earlier and
471 in glibc 2.20 and later to compile without warnings, define
475 .BR _DEFAULT_SOURCE .
477 .BR _SVID_SOURCE " (deprecated since glibc 2.20)"
478 Defining this macro with any value causes header files to expose
479 System V-derived definitions.
480 (SVID == System V Interface Definition; see
483 Since glibc 2.20, this macro is deprecated in the same fashion as
486 .BR _DEFAULT_SOURCE " (since glibc 2.19)"
487 This macro can be defined to ensure that the "default"
488 definitions are provided even when the defaults would otherwise
490 as happens when individual macros are explicitly defined,
491 or the compiler is invoked in one of its "standard" modes (e.g.,
492 .IR "cc\ \-std=c99" ).
495 without defining other individual macros
496 or invoking the compiler in one of its "standard" modes has no effect.
498 The "default" definitions comprise those required by POSIX.1-2008 and ISO C99,
499 as well as various definitions originally derived from BSD and System V.
500 On glibc 2.19 and earlier, these defaults were approximately equivalent
501 to explicitly defining the following:
503 cc \-D_BSD_SOURCE \-D_SVID_SOURCE \-D_POSIX_C_SOURCE=200809
505 .BR _ATFILE_SOURCE " (since glibc 2.4)"
506 Defining this macro with any value causes header files to expose
507 declarations of a range of functions with the suffix "at";
510 Since glibc 2.10, this macro is also implicitly defined if
512 is defined with a value greater than or equal to 200809L.
515 Defining this macro (with any value) implicitly defines
517 .BR _LARGEFILE64_SOURCE ,
519 .BR _XOPEN_SOURCE_EXTENDED ,
522 with the value 200809L
523 (200112L in glibc versions before 2.10;
524 199506L in glibc versions before 2.5;
525 199309L in glibc versions before 2.1)
529 (600 in glibc versions before 2.10;
530 500 in glibc versions before 2.2).
531 In addition, various GNU-specific extensions are also exposed.
533 Since glibc 2.19, defining
535 also has the effect of implicitly defining
536 .BR _DEFAULT_SOURCE .
537 In glibc versions before 2.20, defining
539 also had the effect of implicitly defining
545 Historically, on various C libraries
546 it was necessary to define this macro in all
549 .\" There did once exist C libraries where it was necessary. The ones
550 .\" I remember were proprietary Unix vendor libcs from the mid-1990s
551 .\" You would get completely unlocked stdio without _REENTRANT.
552 (Some C libraries may still require this.)
554 this macro also exposed definitions of certain reentrant functions.
556 However, glibc has been thread-safe by default for many years;
557 since glibc 2.3, the only effect of defining
559 has been to enable one or two of the same declarations that
560 are also enabled by defining
562 with a value of 199606L or greater.
566 In glibc 2.25 and later, defining
568 is equivalent to defining
570 with the value 199606L.
571 If a higher POSIX conformance level is
572 selected by any other means (such as
576 .BR _DEFAULT_SOURCE ,
583 This macro is automatically defined if one compiles with
584 .IR "cc\ \-pthread" .
587 Synonym for the (deprecated)
589 provided for compatibility with some other implementations.
591 .BR _FORTIFY_SOURCE " (since glibc 2.3.4)"
592 .\" For more detail, see:
593 .\" http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
594 .\" [PATCH] Object size checking to prevent (some) buffer overflows
595 .\" * From: Jakub Jelinek <jakub at redhat dot com>
596 .\" * To: gcc-patches at gcc dot gnu dot org
597 .\" * Date: Tue, 21 Sep 2004 04:16:40 -0400
598 Defining this macro causes some lightweight checks to be performed
599 to detect some buffer overflow errors when employing
600 various string and memory manipulation functions (for example,
613 and wide character variants thereof).
614 For some functions, argument consistency is checked;
615 for example, a check is made that
617 has been supplied with a
619 argument when the specified flags include
621 Not all problems are detected, just some common cases.
622 .\" Look for __USE_FORTIFY_LEVEL in the header files
626 is set to 1, with compiler optimization level 1
628 and above, checks that shouldn't change the behavior of
629 conforming programs are performed.
632 set to 2, some more checking is added, but
633 some conforming programs might fail.
634 .\" For example, given the following code
636 .\" char buf[1000], buf[1000];
637 .\" strcpy(fmt, "Hello world\n%n");
638 .\" snprintf(buf, sizeof(buf), fmt, &d);
640 .\" Compiling with "gcc -D_FORTIFY_SOURCE=2 -O1" and then running will
641 .\" cause the following diagnostic at run time at the snprintf() call
643 .\" *** %n in writable segment detected ***
644 .\" Aborted (core dumped)
647 Some of the checks can be performed at compile time
648 (via macros logic implemented in header files),
649 and result in compiler warnings;
650 other checks take place at run time,
651 and result in a run-time error if the check fails.
653 Use of this macro requires compiler support, available with
656 .SS Default definitions, implicit definitions, and combining definitions
657 If no feature test macros are explicitly defined,
658 then the following feature test macros are defined by default:
660 (in glibc 2.19 and earlier),
662 (in glibc 2.19 and earlier),
667 .BR _POSIX_C_SOURCE =200809L
668 (200112L in glibc versions before 2.10;
669 199506L in glibc versions before 2.4;
670 199309L in glibc versions before 2.1).
673 .BR __STRICT_ANSI__ ,
678 .BR _POSIX_C_SOURCE ,
680 .BR _XOPEN_SOURCE_EXTENDED
681 (in glibc 2.11 and earlier),
683 (in glibc 2.19 and earlier),
686 (in glibc 2.19 and earlier)
687 is explicitly defined, then
692 are not defined by default.
698 are not explicitly defined,
703 is defined with a value of 500 or more, then
706 is defined with the value 1; and
709 is defined with one of the following values:
715 is defined with a value less than 500;
720 is defined with a value greater than or equal to 500 and less than 600;
723 (since glibc 2.4) 200112L,
726 is defined with a value greater than or equal to 600 and less than 700.
732 is defined with a value greater than or equal to 700.
734 Older versions of glibc do not know about the values
735 200112L and 200809L for
736 .BR _POSIX_C_SOURCE ,
737 and the setting of this macro will depend on the glibc version.
741 is undefined, then the setting of
743 depends on the glibc version:
744 199506L, in glibc versions before 2.4;
745 200112L, in glibc 2.4 to 2.9; and
746 200809L, since glibc 2.10.
749 Multiple macros can be defined; the results are additive.
752 .BR _POSIX_C_SOURCE ,
757 .B _XOPEN_SOURCE_EXTENDED
758 was specified by XPG4v2 (aka SUSv1), but is not present in SUSv2 and later.
760 is not specified by any standard,
761 but is employed on some other implementations.
765 .BR _DEFAULT_SOURCE ,
768 .BR _FORTIFY_SOURCE ,
772 are specific to Linux (glibc).
775 is a Linux/glibc-specific header file.
776 Other systems have an analogous file, but typically with a different name.
777 This header file is automatically included by other header files as
778 required: it is not necessary to explicitly include it in order to
779 employ feature test macros.
781 According to which of the above feature test macros are defined,
783 internally defines various other macros that are checked by
784 other glibc header files.
785 These macros have names prefixed by two underscores (e.g.,
789 define these macros directly:
790 instead, the appropriate feature test macro(s) from the
791 list above should be employed.
793 The program below can be used to explore how the various
794 feature test macros are set depending on the glibc version
795 and what feature test macros are explicitly set.
796 The following shell session, on a system with glibc 2.10,
797 shows some examples of what we would see:
803 _POSIX_SOURCE defined
804 _POSIX_C_SOURCE defined: 200809L
807 _ATFILE_SOURCE defined
808 $ \fBcc \-D_XOPEN_SOURCE=500 ftm.c\fP
810 _POSIX_SOURCE defined
811 _POSIX_C_SOURCE defined: 199506L
812 _XOPEN_SOURCE defined: 500
813 $ \fBcc \-D_GNU_SOURCE ftm.c\fP
815 _POSIX_SOURCE defined
816 _POSIX_C_SOURCE defined: 200809L
817 _ISOC99_SOURCE defined
818 _XOPEN_SOURCE defined: 700
819 _XOPEN_SOURCE_EXTENDED defined
820 _LARGEFILE64_SOURCE defined
823 _ATFILE_SOURCE defined
838 main(int argc, char *argv[])
841 printf("_POSIX_SOURCE defined\en");
844 #ifdef _POSIX_C_SOURCE
845 printf("_POSIX_C_SOURCE defined: %jdL\en",
846 (intmax_t) _POSIX_C_SOURCE);
849 #ifdef _ISOC99_SOURCE
850 printf("_ISOC99_SOURCE defined\en");
853 #ifdef _ISOC11_SOURCE
854 printf("_ISOC11_SOURCE defined\en");
858 printf("_XOPEN_SOURCE defined: %d\en", _XOPEN_SOURCE);
861 #ifdef _XOPEN_SOURCE_EXTENDED
862 printf("_XOPEN_SOURCE_EXTENDED defined\en");
865 #ifdef _LARGEFILE64_SOURCE
866 printf("_LARGEFILE64_SOURCE defined\en");
869 #ifdef _FILE_OFFSET_BITS
870 printf("_FILE_OFFSET_BITS defined: %d\en", _FILE_OFFSET_BITS);
874 printf("_BSD_SOURCE defined\en");
878 printf("_SVID_SOURCE defined\en");
881 #ifdef _DEFAULT_SOURCE
882 printf("_DEFAULT_SOURCE defined\en");
885 #ifdef _ATFILE_SOURCE
886 printf("_ATFILE_SOURCE defined\en");
890 printf("_GNU_SOURCE defined\en");
894 printf("_REENTRANT defined\en");
898 printf("_THREAD_SAFE defined\en");
901 #ifdef _FORTIFY_SOURCE
902 printf("_FORTIFY_SOURCE defined\en");
911 .BR system_data_types (7)
913 The section "Feature Test Macros" under
915 .\" But beware: the info libc document is out of date (Jul 07, mtk)
917 .I /usr/include/features.h