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 2014-03-20 "Linux" "Linux Programmer's Manual"
27 feature_test_macros \- feature test macros
30 .B #include <features.h>
33 Feature test macros allow the programmer to control the definitions that
34 are exposed by system header files when a program is compiled.
37 In order to be effective, a feature test macro
38 .IR "must be defined before including any header files" .
39 This can be done either in the compilation command
40 .RI ( "cc \-DMACRO=value" )
41 or by defining the macro within the source code before
42 including any headers.
44 Some feature test macros are useful for creating portable applications,
45 by preventing nonstandard definitions from being exposed.
46 Other macros can be used to expose nonstandard definitions that
47 are not exposed by default.
48 The precise effects of each of the feature test macros described below
49 can be ascertained by inspecting the
52 .SS Specification of feature test macro requirements in manual pages
53 When a function requires that a feature test macro is defined,
54 the manual page SYNOPSIS typically includes a note of the following form
55 (this example from the
60 .B #include <unistd.h>
62 .BI "int acct(const char *" filename );
66 Feature Test Macro Requirements for glibc (see
67 .BR feature_test_macros (7)):
72 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500)
77 means that in order to obtain the declaration of
82 of the following macro
83 definitions must be made before including any header files:
88 #define _XOPEN_SOURCE /* or any value < 500 */
92 Alternatively, equivalent definitions can be included in the
98 cc \-D_XOPEN_SOURCE # Or any value < 500
102 Note that, as described below,
103 .BR "some feature test macros are defined by default" ,
104 so that it may not always be necessary to
105 explicitly specify the feature test macro(s) shown in the
108 In a few cases, manual pages use a shorthand for expressing the
109 feature test macro requirements (this example from
114 .B #define _GNU_SOURCE
115 .B #include <fcntl.h>
117 .BI "ssize_t readahead(int " fd ", off64_t *" offset ", size_t " count );
121 This format is employed in cases where only a single
122 feature test macro can be used to expose the function
123 declaration, and that macro is not defined by default.
124 .SS Feature test macros understood by glibc
125 The following paragraphs explain how feature test macros are handled
126 in Linux glibc 2.\fIx\fP,
129 .\" The details in glibc 2.0 are simpler, but combining a
130 .\" a description of them with the details in later glibc versions
131 .\" would make for a complicated description.
133 Linux glibc understands the following feature test macros:
137 This macro is implicitly defined by
139 when invoked with, for example, the
146 Defining this macro causes header files to expose definitions as follows:
149 The value 1 exposes definitions conforming to POSIX.1-1990 and
152 The value 2 or greater additionally exposes
153 definitions for POSIX.2-1992.
155 The value 199309L or greater additionally exposes
156 definitions for POSIX.1b (real-time extensions).
157 .\" 199506L functionality is available only since glibc 2.1
159 The value 199506L or greater additionally exposes
160 definitions for POSIX.1c (threads).
163 The value 200112L or greater additionally exposes definitions corresponding
164 to the POSIX.1-2001 base specification (excluding the XSI extension)
165 and also causes C95 (since glibc 2.12) and
166 C99 (since glibc 2.10) features to be exposed.
169 The value 200809L or greater additionally exposes definitions corresponding
170 to the POSIX.1-2008 base specification (excluding the XSI extension).
174 Defining this obsolete macro with any value is equivalent to defining
179 Defining this macro causes header files to expose definitions as follows:
182 Defining with any value exposes
183 definitions conforming to POSIX.1, POSIX.2, and XPG4.
185 The value 500 or greater additionally exposes
186 definitions for SUSv2 (UNIX 98).
188 (Since glibc 2.2) The value 600 or greater additionally exposes
189 definitions for SUSv3 (UNIX 03; i.e., the POSIX.1-2001 base specification
190 plus the XSI extension) and C99 definitions.
192 (Since glibc 2.10) The value 700 or greater additionally exposes
193 definitions for SUSv4 (i.e., the POSIX.1-2008 base specification
194 plus the XSI extension).
201 is defined with a value greater than or equal to 500
207 is explicitly defined, then
208 the following macros are implicitly defined:
212 is defined with the value 1.
215 is defined, according to the value of
219 .BR _XOPEN_SOURCE " < 500"
221 is defined with the value 2.
223 .RB "500 <= " _XOPEN_SOURCE " < 600"
225 is defined with the value 199506L.
227 .RB "600 <= " _XOPEN_SOURCE " < 700"
229 is defined with the value 200112L.
231 .RB "700 <= " _XOPEN_SOURCE " (since glibc 2.10)"
233 is defined with the value 200809L.
237 .B _XOPEN_SOURCE_EXTENDED
238 If this macro is defined, and
240 is defined, then expose definitions corresponding to the XPG4v2
241 (SUSv1) UNIX extensions (UNIX 95).
242 This macro is also implicitly defined if
244 is defined with a value of 500 or more.
246 .BR _ISOC99_SOURCE " (since glibc 2.1.3)"
247 Exposes declarations consistent with the ISO C99 standard.
249 Earlier glibc 2.1.x versions recognized an equivalent macro named
251 (because the C99 standard had not then been finalized).
252 Although the use of this macro is obsolete, glibc continues
253 to recognize it for backward compatibility.
257 also exposes ISO C (1990) Amendment 1 ("C95") definitions.
258 (The primary change in C95 was support for international character sets.)
260 .BR _ISOC11_SOURCE " (since glibc 2.16)"
261 Exposes declarations consistent with the ISO C11 standard.
262 Defining this macro also enables C99 and C95 features (like
263 .BR _ISOC99_SOURCE ).
265 .B _LARGEFILE64_SOURCE
266 Expose definitions for the alternative API specified by the
267 LFS (Large File Summit) as a "transitional extension" to the
268 Single UNIX Specification.
270 .UR http:\:/\:/opengroup.org\:/platform\:/lfs.html
272 The alternative API consists of a set of new objects
273 (i.e., functions and types) whose names are suffixed with "64"
282 New programs should not employ this macro; instead
283 .I _FILE_OFFSET_BITS=64
286 .BR _LARGEFILE_SOURCE
287 This macro was historically used to expose certain functions (specifically
291 that address limitations of earlier APIs
298 This macro is implicitly defined if
300 is defined with a value greater than or equal to 500.
301 New programs should not employ this macro;
304 as just described or defining
306 with the value 64 is the preferred mechanism to achieve the same result.
309 Defining this macro with the value 64
310 automatically converts references to 32-bit functions and data types
311 related to file I/O and filesystem operations into references to
312 their 64-bit counterparts.
313 This is useful for performing I/O on large files (> 2 Gigabytes)
315 (Defining this macro permits correctly written programs to use
316 large files with only a recompilation being required.)
318 64-bit systems naturally permit file sizes greater than 2 Gigabytes,
319 and on those systems this macro has no effect.
321 .BR _BSD_SOURCE " (deprecated since glibc 2.20)"
322 Defining this macro with any value causes header files to expose
323 BSD-derived definitions.
325 In glibc versions up to and including 2.18,
326 defining this macro also causes BSD definitions to be preferred in
327 some situations where standards conflict, unless one or more of
330 .BR _POSIX_C_SOURCE ,
332 .BR _XOPEN_SOURCE_EXTENDED ,
335 is defined, in which case BSD definitions are disfavored.
338 no longer causes BSD definitions to be preferred in case of conflicts.
340 Since glibc 2.20, this macro is deprecated.
341 .\" commit c941736c92fa3a319221f65f6755659b2a5e0a20
342 .\" commit 498afc54dfee41d33ba519f496e96480badace8e
343 .\" commit acd7f096d79c181866d56d4aaf3b043e741f1e2c
344 It now has the same effect as defining
345 .BR _DEFAULT_SOURCE ,
346 but generates a compile-time warning (unless
348 .\" commit ade40b10ff5fa59a318cf55b9d8414b758e8df78
353 To allow code that requires
355 in glibc 2.19 and earlier and
357 in glibc 2.20 and later to compile without warnings, define
361 .BR _DEFAULT_SOURCE .
363 .BR _SVID_SOURCE " (deprecated since glibc 2.20)"
364 Defining this macro with any value causes header files to expose
365 System V-derived definitions.
366 (SVID == System V Interface Definition; see
369 Since glibc 2.20, this macro is deprecated in the same fashion as
372 .BR _DEFAULT_SOURCE " (since glibc 2.19)"
373 This macro can be defined to ensure that the "default"
374 definitions are provided even when the defaults would otherwise
376 as happens when individual macros are explicitly defined,
377 or the compiler is invoked in one of its "standard" modes (e.g.,
378 .IR "cc\ \-std=c99" ).
381 without defining other individual macros
382 or invoking the compiler in one of its "standard" modes has no effect.
384 The "default" definitions comprise those required by POSIX.1-2008
385 as well as various definitions derived from BSD and System V.
386 On glibc 2.19 and earlier, these defaults were approximately equivalent
387 to explicitly defining the following:
389 cc \-D_BSD_SOURCE \-D_SVID_SOURCE \-D_POSIX_C_SOURCE=200809
392 .BR _ATFILE_SOURCE " (since glibc 2.4)"
393 Defining this macro with any value causes header files to expose
394 declarations of a range of functions with the suffix "at";
397 Since glibc 2.10, this macro is also implicitly defined if
399 is defined with a value greater than or equal to 200809L.
402 Defining this macro (with any value) implicitly defines
404 .BR _LARGEFILE64_SOURCE ,
406 .BR _XOPEN_SOURCE_EXTENDED ,
409 with the value 200809L
410 (200112L in glibc versions before 2.10;
411 199506L in glibc versions before 2.5;
412 199309L in glibc versions before 2.1)
416 (600 in glibc versions before 2.10;
417 500 in glibc versions before 2.2).
418 In addition, various GNU-specific extensions are also exposed.
420 Since glibc 2.19, defining
422 also has the effect of implicitly defining
423 .BR _DEFAULT_SOURCE .
424 In glibc versions before 2.20, defining
426 also had the effect of implicitly defining
432 Defining this macro exposes definitions of certain reentrant functions.
433 For multithreaded programs, use
440 provided for compatibility with some other implementations.
442 .BR _FORTIFY_SOURCE " (since glibc 2.3.4)"
443 .\" For more detail, see:
444 .\" http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
445 .\" [PATCH] Object size checking to prevent (some) buffer overflows
446 .\" * From: Jakub Jelinek <jakub at redhat dot com>
447 .\" * To: gcc-patches at gcc dot gnu dot org
448 .\" * Date: Tue, 21 Sep 2004 04:16:40 -0400
449 Defining this macro causes some lightweight checks to be performed
450 to detect some buffer overflow errors when employing
451 various string and memory manipulation functions.
452 Not all buffer overflows are detected, just some common cases.
454 In the current implementation, checks are added for
474 is set to 1, with compiler optimization level 1
476 and above, checks that shouldn't change the behavior of
477 conforming programs are performed.
480 set to 2 some more checking is added, but
481 some conforming programs might fail.
482 Some of the checks can be performed at compile time,
483 and result in compiler warnings;
484 other checks take place at run time,
485 and result in a run-time error if the check fails.
487 Use of this macro requires compiler support, available with
490 .SS Default definitions, implicit definitions, and combining definitions
492 If no feature test macros are explicitly defined,
493 then the following feature test macros are defined by default:
495 (in glibc 2.19 and earlier),
497 (in glibc 2.19 and earlier),
502 .BR _POSIX_C_SOURCE =200809L
503 (200112L in glibc versions before 2.10;
504 199506L in glibc versions before 2.4;
505 199309L in glibc versions before 2.1).
508 .BR __STRICT_ANSI__ ,
511 .BR _POSIX_C_SOURCE ,
513 .BR _XOPEN_SOURCE_EXTENDED ,
515 (in glibc 2.19 and earlier),
518 (in glibc 2.19 and earlier)
519 is explicitly defined, then
524 are not defined by default.
530 are not explicitly defined,
535 is defined with a value of 500 or more, then
538 is defined with the value 1; and
541 is defined with one of the following values:
547 is defined with a value less than 500;
552 is defined with a value greater than or equal to 500 and less than 600;
555 (since glibc 2.4) 200112L,
558 is defined with a value greater than or equal to 600 and less than 700.
564 is defined with a value greater than or equal to 700.
566 Older versions of glibc do not know about the values
567 200112L and 200809L for
568 .BR _POSIX_C_SOURCE ,
569 and the setting of this macro will depend on the glibc version.
573 is undefined, then the setting of
575 depends on the glibc version:
576 199506L, in glibc versions before 2.4;
577 200112L, in glibc 2.4 to 2.9; and
578 200809L, since glibc 2.10.
581 Multiple macros can be defined; the results are additive.
584 .BR _POSIX_C_SOURCE ,
588 .B _XOPEN_SOURCE_EXTENDED
589 was specified by XPG4v2 (aka SUSv1).
592 is not specified by any standard,
593 but is employed on some other implementations.
597 .BR _DEFAULT_SOURCE ,
600 .BR _FORTIFY_SOURCE ,
604 are specific to Linux (glibc).
607 is a Linux/glibc-specific header file.
608 Other systems have an analogous file, but typically with a different name.
609 This header file is automatically included by other header files as
610 required: it is not necessary to explicitly include it in order to
611 employ feature test macros.
613 According to which of the above feature test macros are defined,
615 internally defines various other macros that are checked by
616 other glibc header files.
617 These macros have names prefixed by two underscores (e.g.,
621 define these macros directly:
622 instead, the appropriate feature test macro(s) from the
623 list above should be employed.
625 The program below can be used to explore how the various
626 feature test macros are set depending on the glibc version
627 and what feature test macros are explicitly set.
628 The following shell session, on a system with glibc 2.10,
629 shows some examples of what we would see:
635 _POSIX_SOURCE defined
636 _POSIX_C_SOURCE defined: 200809L
639 _ATFILE_SOURCE defined
640 $ \fBcc \-D_XOPEN_SOURCE=500 ftm.c\fP
642 _POSIX_SOURCE defined
643 _POSIX_C_SOURCE defined: 199506L
644 _XOPEN_SOURCE defined: 500
645 $ \fBcc \-D_GNU_SOURCE ftm.c\fP
647 _POSIX_SOURCE defined
648 _POSIX_C_SOURCE defined: 200809L
649 _ISOC99_SOURCE defined
650 _XOPEN_SOURCE defined: 700
651 _XOPEN_SOURCE_EXTENDED defined
652 _LARGEFILE64_SOURCE defined
655 _ATFILE_SOURCE defined
669 main(int argc, char *argv[])
672 printf("_POSIX_SOURCE defined\\n");
675 #ifdef _POSIX_C_SOURCE
676 printf("_POSIX_C_SOURCE defined: %ldL\\n", (long) _POSIX_C_SOURCE);
679 #ifdef _ISOC99_SOURCE
680 printf("_ISOC99_SOURCE defined\\n");
683 #ifdef _ISOC11_SOURCE
684 printf("_ISOC11_SOURCE defined\\n");
688 printf("_XOPEN_SOURCE defined: %d\\n", _XOPEN_SOURCE);
691 #ifdef _XOPEN_SOURCE_EXTENDED
692 printf("_XOPEN_SOURCE_EXTENDED defined\\n");
695 #ifdef _LARGEFILE64_SOURCE
696 printf("_LARGEFILE64_SOURCE defined\\n");
699 #ifdef _FILE_OFFSET_BITS
700 printf("_FILE_OFFSET_BITS defined: %d\\n", _FILE_OFFSET_BITS);
704 printf("_BSD_SOURCE defined\\n");
708 printf("_SVID_SOURCE defined\\n");
711 #ifdef _DEFAULT_SOURCE
712 printf("_DEFAULT_SOURCE defined\\n");
715 #ifdef _ATFILE_SOURCE
716 printf("_ATFILE_SOURCE defined\\n");
720 printf("_GNU_SOURCE defined\\n");
724 printf("_REENTRANT defined\\n");
728 printf("_THREAD_SAFE defined\\n");
731 #ifdef _FORTIFY_SOURCE
732 printf("_FORTIFY_SOURCE defined\\n");
742 The section "Feature Test Macros" under
744 .\" But beware: the info libc document is out of date (Jul 07, mtk)
746 .I /usr/include/features.h
748 This page is part of release 3.79 of the Linux
751 A description of the project,
752 information about reporting bugs,
753 and the latest version of this page,
755 \%http://www.kernel.org/doc/man\-pages/.