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 2016-07-17 "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.
40 Some feature test macros are useful for creating portable applications,
41 by preventing nonstandard definitions from being exposed.
42 Other macros can be used to expose nonstandard definitions that
43 are not exposed by default.
45 The precise effects of each of the feature test macros described below
46 can be ascertained by inspecting the
52 need to directly include
54 indeed, doing so is actively discouraged.
56 .SS Specification of feature test macro requirements in manual pages
57 When a function requires that a feature test macro is defined,
58 the manual page SYNOPSIS typically includes a note of the following form
59 (this example from the
64 .B #include <unistd.h>
66 .BI "int acct(const char *" filename );
70 Feature Test Macro Requirements for glibc (see
71 .BR feature_test_macros (7)):
76 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500)
81 means that in order to obtain the declaration of
86 of the following macro
87 definitions must be made before including any header files:
92 #define _XOPEN_SOURCE /* or any value < 500 */
96 Alternatively, equivalent definitions can be included in the
102 cc \-D_XOPEN_SOURCE # Or any value < 500
106 Note that, as described below,
107 .BR "some feature test macros are defined by default" ,
108 so that it may not always be necessary to
109 explicitly specify the feature test macro(s) shown in the
112 In a few cases, manual pages use a shorthand for expressing the
113 feature test macro requirements (this example from
118 .B #define _GNU_SOURCE
119 .B #include <fcntl.h>
121 .BI "ssize_t readahead(int " fd ", off64_t *" offset ", size_t " count );
125 This format is employed in cases where only a single
126 feature test macro can be used to expose the function
127 declaration, and that macro is not defined by default.
128 .SS Feature test macros understood by glibc
129 The paragraphs below explain how feature test macros are handled
130 in Linux glibc 2.\fIx\fP,
134 First, though a summary of a few details for the impatient:
136 The macros that you most likely need to use in modern source code are
138 (for definitions from various versions of POSIX.1),
140 (for definitions from various versions of SUS),
142 (for GNU and/or Linux specific stuff), and
144 (to get definitions that would normally be provided by default).
146 Certain macros are defined with default values.
147 Thus, although one or more macros may be indicated as being
148 required in the SYNOPSIS of a man page,
149 it may not be necessary to define them explicitly.
150 Full details of the defaults are given later in this man page.
154 with a value of 600 or greater produces the same effects as defining
156 with a value of 200112L or greater.
161 _POSIX_C_SOURCE >= 200112L
166 in the feature test macro requirements in the SYNOPSIS of a man page,
167 it is implicit that the following has the same effect:
178 with a value of 700 or greater produces the same effects as defining
180 with a value of 200809L or greater.
185 _POSIX_C_SOURCE >= 200809L
190 in the feature test macro requirements in the SYNOPSIS of a man page,
191 it is implicit that the following has the same effect:
199 .\" The details in glibc 2.0 are simpler, but combining a
200 .\" a description of them with the details in later glibc versions
201 .\" would make for a complicated description.
203 Linux glibc understands the following feature test macros:
207 This macro is implicitly defined by
209 when invoked with, for example, the
216 Defining this macro causes header files to expose definitions as follows:
219 The value 1 exposes definitions conforming to POSIX.1-1990 and
222 The value 2 or greater additionally exposes
223 definitions for POSIX.2-1992.
225 The value 199309L or greater additionally exposes
226 definitions for POSIX.1b (real-time extensions).
227 .\" 199506L functionality is available only since glibc 2.1
229 The value 199506L or greater additionally exposes
230 definitions for POSIX.1c (threads).
233 The value 200112L or greater additionally exposes definitions corresponding
234 to the POSIX.1-2001 base specification (excluding the XSI extension).
235 This value also causes C95 (since glibc 2.12) and
236 C99 (since glibc 2.10) features to be exposed
237 (in other words, the equivalent of defining
238 .BR _ISOC99_SOURCE ).
241 The value 200809L or greater additionally exposes definitions corresponding
242 to the POSIX.1-2008 base specification (excluding the XSI extension).
246 Defining this obsolete macro with any value is equivalent to defining
250 Since this macro is obsolete,
251 its usage is generally not documented when discussing
252 feature test macro requirements in the man pages.
255 Defining this macro causes header files to expose definitions as follows:
258 Defining with any value exposes
259 definitions conforming to POSIX.1, POSIX.2, and XPG4.
261 The value 500 or greater additionally exposes
262 definitions for SUSv2 (UNIX 98).
264 (Since glibc 2.2) The value 600 or greater additionally exposes
265 definitions for SUSv3 (UNIX 03; i.e., the POSIX.1-2001 base specification
266 plus the XSI extension) and C99 definitions.
268 (Since glibc 2.10) The value 700 or greater additionally exposes
269 definitions for SUSv4 (i.e., the POSIX.1-2008 base specification
270 plus the XSI extension).
277 is defined with a value greater than or equal to 500
283 is explicitly defined, then
284 the following macros are implicitly defined:
288 is defined with the value 1.
291 is defined, according to the value of
295 .BR _XOPEN_SOURCE " < 500"
297 is defined with the value 2.
299 .RB "500 <= " _XOPEN_SOURCE " < 600"
301 is defined with the value 199506L.
303 .RB "600 <= " _XOPEN_SOURCE " < 700"
305 is defined with the value 200112L.
307 .RB "700 <= " _XOPEN_SOURCE " (since glibc 2.10)"
309 is defined with the value 200809L.
313 In addition, defining
315 with a value of 500 or greater produces the same effects as defining
316 .BR _XOPEN_SOURCE_EXTENDED .
318 .B _XOPEN_SOURCE_EXTENDED
319 If this macro is defined,
322 is defined, then expose definitions corresponding to the XPG4v2
323 (SUSv1) UNIX extensions (UNIX 95).
326 with a value of 500 or more also produces the same effect as defining
327 .BR _XOPEN_SOURCE_EXTENDED .
329 .BR _XOPEN_SOURCE_EXTENDED
330 in new source code should be avoided.
334 with a value of 500 or more has the same effect as defining
335 .BR _XOPEN_SOURCE_EXTENDED ,
336 the latter (obsolete) feature test macro is generally not described in the
337 SYNOPSIS in man pages.
339 .BR _ISOC99_SOURCE " (since glibc 2.1.3)"
340 Exposes declarations consistent with the ISO C99 standard.
342 Earlier glibc 2.1.x versions recognized an equivalent macro named
344 (because the C99 standard had not then been finalized).
345 Although the use of this macro is obsolete, glibc continues
346 to recognize it for backward compatibility.
350 also exposes ISO C (1990) Amendment 1 ("C95") definitions.
351 (The primary change in C95 was support for international character sets.)
353 Invoking the C compiler with the option
355 produces the same effects as defining this macro.
357 .BR _ISOC11_SOURCE " (since glibc 2.16)"
358 Exposes declarations consistent with the ISO C11 standard.
359 Defining this macro also enables C99 and C95 features (like
360 .BR _ISOC99_SOURCE ).
362 Invoking the C compiler with the option
364 produces the same effects as defining this macro.
366 .B _LARGEFILE64_SOURCE
367 Expose definitions for the alternative API specified by the
368 LFS (Large File Summit) as a "transitional extension" to the
369 Single UNIX Specification.
371 .UR http:\:/\:/opengroup.org\:/platform\:/lfs.html
373 The alternative API consists of a set of new objects
374 (i.e., functions and types) whose names are suffixed with "64"
383 New programs should not employ this macro; instead
384 .I _FILE_OFFSET_BITS=64
387 .BR _LARGEFILE_SOURCE
388 This macro was historically used to expose certain functions (specifically
392 that address limitations of earlier APIs
399 This macro is implicitly defined if
401 is defined with a value greater than or equal to 500.
402 New programs should not employ this macro;
405 as just described or defining
407 with the value 64 is the preferred mechanism to achieve the same result.
410 Defining this macro with the value 64
411 automatically converts references to 32-bit functions and data types
412 related to file I/O and filesystem operations into references to
413 their 64-bit counterparts.
414 This is useful for performing I/O on large files (> 2 Gigabytes)
416 (Defining this macro permits correctly written programs to use
417 large files with only a recompilation being required.)
419 64-bit systems naturally permit file sizes greater than 2 Gigabytes,
420 and on those systems this macro has no effect.
422 .BR _BSD_SOURCE " (deprecated since glibc 2.20)"
423 Defining this macro with any value causes header files to expose
424 BSD-derived definitions.
426 In glibc versions up to and including 2.18,
427 defining this macro also causes BSD definitions to be preferred in
428 some situations where standards conflict, unless one or more of
431 .BR _POSIX_C_SOURCE ,
433 .BR _XOPEN_SOURCE_EXTENDED ,
436 is defined, in which case BSD definitions are disfavored.
439 no longer causes BSD definitions to be preferred in case of conflicts.
441 Since glibc 2.20, this macro is deprecated.
442 .\" commit c941736c92fa3a319221f65f6755659b2a5e0a20
443 .\" commit 498afc54dfee41d33ba519f496e96480badace8e
444 .\" commit acd7f096d79c181866d56d4aaf3b043e741f1e2c
445 It now has the same effect as defining
446 .BR _DEFAULT_SOURCE ,
447 but generates a compile-time warning (unless
449 .\" commit ade40b10ff5fa59a318cf55b9d8414b758e8df78
454 To allow code that requires
456 in glibc 2.19 and earlier and
458 in glibc 2.20 and later to compile without warnings, define
462 .BR _DEFAULT_SOURCE .
464 .BR _SVID_SOURCE " (deprecated since glibc 2.20)"
465 Defining this macro with any value causes header files to expose
466 System V-derived definitions.
467 (SVID == System V Interface Definition; see
470 Since glibc 2.20, this macro is deprecated in the same fashion as
473 .BR _DEFAULT_SOURCE " (since glibc 2.19)"
474 This macro can be defined to ensure that the "default"
475 definitions are provided even when the defaults would otherwise
477 as happens when individual macros are explicitly defined,
478 or the compiler is invoked in one of its "standard" modes (e.g.,
479 .IR "cc\ \-std=c99" ).
482 without defining other individual macros
483 or invoking the compiler in one of its "standard" modes has no effect.
485 The "default" definitions comprise those required by POSIX.1-2008 and ISO C99,
486 as well as various definitions originally derived from BSD and System V.
487 On glibc 2.19 and earlier, these defaults were approximately equivalent
488 to explicitly defining the following:
490 cc \-D_BSD_SOURCE \-D_SVID_SOURCE \-D_POSIX_C_SOURCE=200809
492 .BR _ATFILE_SOURCE " (since glibc 2.4)"
493 Defining this macro with any value causes header files to expose
494 declarations of a range of functions with the suffix "at";
497 Since glibc 2.10, this macro is also implicitly defined if
499 is defined with a value greater than or equal to 200809L.
502 Defining this macro (with any value) implicitly defines
504 .BR _LARGEFILE64_SOURCE ,
506 .BR _XOPEN_SOURCE_EXTENDED ,
509 with the value 200809L
510 (200112L in glibc versions before 2.10;
511 199506L in glibc versions before 2.5;
512 199309L in glibc versions before 2.1)
516 (600 in glibc versions before 2.10;
517 500 in glibc versions before 2.2).
518 In addition, various GNU-specific extensions are also exposed.
520 Since glibc 2.19, defining
522 also has the effect of implicitly defining
523 .BR _DEFAULT_SOURCE .
524 In glibc versions before 2.20, defining
526 also had the effect of implicitly defining
532 Defining this macro exposes definitions of certain reentrant functions.
533 For multithreaded programs, use
540 provided for compatibility with some other implementations.
542 .BR _FORTIFY_SOURCE " (since glibc 2.3.4)"
543 .\" For more detail, see:
544 .\" http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
545 .\" [PATCH] Object size checking to prevent (some) buffer overflows
546 .\" * From: Jakub Jelinek <jakub at redhat dot com>
547 .\" * To: gcc-patches at gcc dot gnu dot org
548 .\" * Date: Tue, 21 Sep 2004 04:16:40 -0400
549 Defining this macro causes some lightweight checks to be performed
550 to detect some buffer overflow errors when employing
551 various string and memory manipulation functions (for example,
564 and wide character variants thereof).
565 For some functions, argument consistency is checked;
566 for example, a check is made that
568 has been supplied with a
570 argument when the specified flags include
572 Not all problems are detected, just some common cases.
573 .\" Look for __USE_FORTIFY_LEVEL in the header files
577 is set to 1, with compiler optimization level 1
579 and above, checks that shouldn't change the behavior of
580 conforming programs are performed.
583 set to 2, some more checking is added, but
584 some conforming programs might fail.
585 .\" For example, given the following code
587 .\" char buf[1000], buf[1000];
588 .\" strcpy(fmt, "Hello world\n%n");
589 .\" snprintf(buf, sizeof(buf), fmt, &d);
591 .\" Compiling with "gcc -D_FORTIFY_SOURCE=2 -O1" and then running will
592 .\" cause the following diagnostic at runtime at the snprintf() call
594 .\" *** %n in writable segment detected ***
595 .\" Aborted (core dumped)
598 Some of the checks can be performed at compile time
599 (via macros logic implemented in header files),
600 and result in compiler warnings;
601 other checks take place at run time,
602 and result in a run-time error if the check fails.
604 Use of this macro requires compiler support, available with
607 .SS Default definitions, implicit definitions, and combining definitions
609 If no feature test macros are explicitly defined,
610 then the following feature test macros are defined by default:
612 (in glibc 2.19 and earlier),
614 (in glibc 2.19 and earlier),
619 .BR _POSIX_C_SOURCE =200809L
620 (200112L in glibc versions before 2.10;
621 199506L in glibc versions before 2.4;
622 199309L in glibc versions before 2.1).
625 .BR __STRICT_ANSI__ ,
628 .BR _POSIX_C_SOURCE ,
630 .BR _XOPEN_SOURCE_EXTENDED ,
632 (in glibc 2.19 and earlier),
635 (in glibc 2.19 and earlier)
636 is explicitly defined, then
641 are not defined by default.
647 are not explicitly defined,
652 is defined with a value of 500 or more, then
655 is defined with the value 1; and
658 is defined with one of the following values:
664 is defined with a value less than 500;
669 is defined with a value greater than or equal to 500 and less than 600;
672 (since glibc 2.4) 200112L,
675 is defined with a value greater than or equal to 600 and less than 700.
681 is defined with a value greater than or equal to 700.
683 Older versions of glibc do not know about the values
684 200112L and 200809L for
685 .BR _POSIX_C_SOURCE ,
686 and the setting of this macro will depend on the glibc version.
690 is undefined, then the setting of
692 depends on the glibc version:
693 199506L, in glibc versions before 2.4;
694 200112L, in glibc 2.4 to 2.9; and
695 200809L, since glibc 2.10.
698 Multiple macros can be defined; the results are additive.
701 .BR _POSIX_C_SOURCE ,
706 .B _XOPEN_SOURCE_EXTENDED
707 was specified by XPG4v2 (aka SUSv1), but is not present in SUSv2 and later.
709 is not specified by any standard,
710 but is employed on some other implementations.
714 .BR _DEFAULT_SOURCE ,
717 .BR _FORTIFY_SOURCE ,
721 are specific to Linux (glibc).
724 is a Linux/glibc-specific header file.
725 Other systems have an analogous file, but typically with a different name.
726 This header file is automatically included by other header files as
727 required: it is not necessary to explicitly include it in order to
728 employ feature test macros.
730 According to which of the above feature test macros are defined,
732 internally defines various other macros that are checked by
733 other glibc header files.
734 These macros have names prefixed by two underscores (e.g.,
738 define these macros directly:
739 instead, the appropriate feature test macro(s) from the
740 list above should be employed.
742 The program below can be used to explore how the various
743 feature test macros are set depending on the glibc version
744 and what feature test macros are explicitly set.
745 The following shell session, on a system with glibc 2.10,
746 shows some examples of what we would see:
752 _POSIX_SOURCE defined
753 _POSIX_C_SOURCE defined: 200809L
756 _ATFILE_SOURCE defined
757 $ \fBcc \-D_XOPEN_SOURCE=500 ftm.c\fP
759 _POSIX_SOURCE defined
760 _POSIX_C_SOURCE defined: 199506L
761 _XOPEN_SOURCE defined: 500
762 $ \fBcc \-D_GNU_SOURCE ftm.c\fP
764 _POSIX_SOURCE defined
765 _POSIX_C_SOURCE defined: 200809L
766 _ISOC99_SOURCE defined
767 _XOPEN_SOURCE defined: 700
768 _XOPEN_SOURCE_EXTENDED defined
769 _LARGEFILE64_SOURCE defined
772 _ATFILE_SOURCE defined
786 main(int argc, char *argv[])
789 printf("_POSIX_SOURCE defined\\n");
792 #ifdef _POSIX_C_SOURCE
793 printf("_POSIX_C_SOURCE defined: %ldL\\n", (long) _POSIX_C_SOURCE);
796 #ifdef _ISOC99_SOURCE
797 printf("_ISOC99_SOURCE defined\\n");
800 #ifdef _ISOC11_SOURCE
801 printf("_ISOC11_SOURCE defined\\n");
805 printf("_XOPEN_SOURCE defined: %d\\n", _XOPEN_SOURCE);
808 #ifdef _XOPEN_SOURCE_EXTENDED
809 printf("_XOPEN_SOURCE_EXTENDED defined\\n");
812 #ifdef _LARGEFILE64_SOURCE
813 printf("_LARGEFILE64_SOURCE defined\\n");
816 #ifdef _FILE_OFFSET_BITS
817 printf("_FILE_OFFSET_BITS defined: %d\\n", _FILE_OFFSET_BITS);
821 printf("_BSD_SOURCE defined\\n");
825 printf("_SVID_SOURCE defined\\n");
828 #ifdef _DEFAULT_SOURCE
829 printf("_DEFAULT_SOURCE defined\\n");
832 #ifdef _ATFILE_SOURCE
833 printf("_ATFILE_SOURCE defined\\n");
837 printf("_GNU_SOURCE defined\\n");
841 printf("_REENTRANT defined\\n");
845 printf("_THREAD_SAFE defined\\n");
848 #ifdef _FORTIFY_SOURCE
849 printf("_FORTIFY_SOURCE defined\\n");
859 The section "Feature Test Macros" under
861 .\" But beware: the info libc document is out of date (Jul 07, mtk)
863 .I /usr/include/features.h