1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" This manpage is Copyright (C) 2006, Michael Kerrisk
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH FEATURE_TEST_MACROS 7 2010-09-10 "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 <sys/stat.h>
62 .BI "int chmod(const char *" path ", mode_t " mode );
64 .BI "int fchmod(int " fd ", mode_t " mode );
68 Feature Test Macro Requirements for glibc (see
69 .BR feature_test_macros (7)):
74 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
77 The \fB||\fP means that in order to obtain the declaration of
81 \fIeither\fP of the following macro
82 definitions must be made before including any header files:
87 #define _XOPEN_SOURCE 500 /* or any value > 500 */
91 Alternatively, equivalent definitions can be included in the
97 cc \-D_XOPEN_SOURCE=500 # Or any value > 500
101 Note that, as described below,
102 .BR "some feature test macros are defined by default" ,
103 so that it may not always be necessary to
104 explicitly specify the feature test macro(s) shown in the
107 In a few cases, manual pages use a shorthand for expressing the
108 feature test macro requirements (this example from
113 .B #define _GNU_SOURCE
114 .B #include <fcntl.h>
116 .BI "ssize_t readahead(int " fd ", off64_t *" offset ", size_t " count );
120 This format is employed in cases where only a single
121 feature test macro can be used to expose the function
122 declaration, and that macro is not defined by default.
123 .SS Feature test macros understood by glibc
124 The following paragraphs explain how feature test macros are handled
125 in Linux glibc 2.\fIx\fP, \fIx\fP > 0.
126 .\" The details in glibc 2.0 are simpler, but combining a
127 .\" a description of them with the details in later glibc versions
128 .\" would make for a complicated description.
130 Linux glibc understands the following feature test macros:
134 This macro is implicitly defined by
136 when invoked with, for example, the
143 Defining this macro causes header files to expose definitions as follows:
146 The value 1 exposes definitions conforming to POSIX.1-1990 and
149 The value 2 or greater additionally exposes
150 definitions for POSIX.2-1992.
152 The value 199309L or greater additionally exposes
153 definitions for POSIX.1b (real-time extensions).
154 .\" 199506L functionality is only available since glibc 2.1
156 The value 199506L or greater additionally exposes
157 definitions for POSIX.1c (threads).
160 The value 200112L or greater exposes definitions corresponding
161 to the POSIX.1-2001 base specification (excluding the XSI extension).
164 The value 200809L or greater exposes definitions corresponding
165 to the POSIX.1-2008 base specification (excluding the XSI extension).
169 Defining this obsolete macro with any value is equivalent to defining
174 Defining this macro causes header files to expose definitions as follows:
177 Defining with any value exposes
178 definitions conforming to POSIX.1, POSIX.2, and XPG4.
180 The value 500 or greater additionally exposes
181 definitions for SUSv2 (UNIX 98).
183 (Since glibc 2.2) The value 600 or greater additionally exposes
184 definitions for SUSv3 (UNIX 03; i.e., the POSIX.1-2001 base specification
185 plus the XSI extension) and C99 definitions.
187 (Since glibc 2.10) The value 700 or greater additionally exposes
188 definitions for SUSv4 (i.e., the POSIX.1-2008 base specification
189 plus the XSI extension).
192 .B _XOPEN_SOURCE_EXTENDED
193 If this macro is defined, and
195 is defined, then expose definitions corresponding to the XPG4v2
196 (SUSv1) UNIX extensions (UNIX 95).
197 This macro is also implicitly defined if
199 is defined with a value of 500 or more.
202 Exposes C99 extensions to ISO C (1990).
203 This macro is recognized since glibc 2.1.3;
204 earlier glibc 2.1.x versions recognized an equivalent macro named
206 (because the C99 standard had not then been finalized).
207 Although the use of the latter macro is obsolete, glibc continues
208 to recognize it for backward compatibility.
210 .B _LARGEFILE64_SOURCE
211 Expose definitions for the alternative API specified by the
212 LFS (Large File Summit) as a "transitional extension" to the
213 Single UNIX Specification.
214 (See http://opengroup.org/platform/lfs.html.)
215 The alternative API consists of a set of new objects
216 (i.e., functions and types) whose names are suffixed with "64"
225 New programs should not employ this interface; instead
226 .I _FILE_OFFSET_BITS=64
230 Defining this macro with the value 64
231 automatically converts references to 32-bit functions and data types
232 related to file I/O and file system operations into references to
233 their 64-bit counterparts.
234 This is useful for performing I/O on large files (> 2 Gigabytes)
236 (Defining this macro permits correctly written programs to use
237 large files with only a recompilation being required.)
238 64-bit systems naturally permit file sizes greater than 2 Gigabytes,
239 and on those systems this macro has no effect.
242 Defining this macro with any value causes header files to expose
243 BSD-derived definitions.
244 Defining this macro also causes BSD definitions to be preferred in
245 some situations where standards conflict, unless one or more of
248 .BR _POSIX_C_SOURCE ,
250 .BR _XOPEN_SOURCE_EXTENDED ,
253 is defined, in which case BSD definitions are disfavored.
256 Defining this macro with any value causes header files to expose
257 System V-derived definitions.
258 (SVID == System V Interface Definition; see
261 .BR _ATFILE_SOURCE " (since glibc 2.4)"
262 Defining this macro with any value causes header files to expose
263 declarations of a range of functions with the suffix "at";
266 Since glibc 2.10, this macro is also implicitly defined if
268 is defined with a value greater than or equal to 200809L.
271 Defining this macro (with any value) is equivalent to defining
275 .BR _LARGEFILE64_SOURCE ,
277 .BR _XOPEN_SOURCE_EXTENDED ,
280 with the value 200809L
281 (200112L in glibc versions before 2.10;
282 199506L in glibc versions before 2.5;
283 199309L in glibc versions before 2.1)
287 (600 in glibc versions before 2.10;
288 500 in glibc versions before 2.2).
289 In addition, various GNU-specific extensions are also exposed.
290 Where standards conflict, BSD definitions are disfavored.
293 Defining this macro exposes definitions of certain reentrant functions.
294 For multithreaded programs, use
301 provided for compatibility with some other implementations.
303 .BR _FORTIFY_SOURCE " (since glibc 2.3.4)"
304 .\" For more detail, see:
305 .\" http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
306 .\" [PATCH] Object size checking to prevent (some) buffer overflows
307 .\" * From: Jakub Jelinek <jakub at redhat dot com>
308 .\" * To: gcc-patches at gcc dot gnu dot org
309 .\" * Date: Tue, 21 Sep 2004 04:16:40 -0400
310 Defining this macro causes some lightweight checks to be performed
311 to detect some buffer overflow errors when employing
312 various string and memory manipulation functions.
313 Not all buffer overflows are detected, just some common cases.
314 In the current implementation checks are added for
333 is set to 1, with compiler optimization level 1
335 and above, checks that shouldn't change the behavior of
336 conforming programs are performed.
339 set to 2 some more checking is added, but
340 some conforming programs might fail.
341 Some of the checks can be performed at compile time,
342 and result in compiler warnings;
343 other checks take place at run time,
344 and result in a run-time error if the check fails.
345 Use of this macro requires compiler support, available with
348 .SS Default definitions, implicit definitions, and combining definitions
350 If no feature test macros are explicitly defined,
351 then the following feature test macros are defined by default:
356 .BR _POSIX_C_SOURCE =200809L
357 (200112L in glibc versions before 2.10;
358 199506L in glibc versions before 2.4;
359 199309L in glibc versions before 2.1).
362 .BR __STRICT_ANSI__ ,
365 .BR _POSIX_C_SOURCE ,
367 .BR _XOPEN_SOURCE_EXTENDED ,
371 is explicitly defined, then
375 are not defined by default.
381 are not explicitly defined,
386 is defined with a value of 500 or more, then
390 is defined with the value 1; and
393 is defined with one of the following values:
399 is defined with a value less than 500;
404 is defined with a value greater than or equal to 500 and less than 600;
407 (since glibc 2.4) 200112L,
410 is defined with a value greater than or equal to 600 and less than 700.
416 is defined with a value greater than or equal to 700.
418 Older versions of glibc do not know about the values
419 200112L and 200809L for
420 .BR _POSIX_C_SOURCE ,
421 and the setting of this macro will depend on the glibc version.
425 is undefined, then the setting of
427 depends on the glibc version:
428 199506L, in glibc versions before 2.4;
429 200112L, in glibc 2.4 to 2.9; and
430 200809L, since glibc 2.10.
434 Multiple macros can be defined; the results are additive.
437 .BR _POSIX_C_SOURCE ,
441 .B _XOPEN_SOURCE_EXTENDED
442 was specified by XPG4v2 (aka SUSv1).
445 is not specified by any standard,
446 but is employed on some other implementations.
452 .BR _FORTIFY_SOURCE ,
456 are specific to Linux (glibc).
459 is a Linux/glibc-specific header file.
460 Other systems have an analogous file, but typically with a different name.
461 This header file is automatically included by other header files as
462 required: it is not necessary to explicitly include it in order to
463 employ feature test macros.
465 According to which of the above feature test macros are defined,
467 internally defines various other macros that are checked by
468 other glibc header files.
469 These macros have names prefixed by two underscores (e.g.,
471 Programs should \fInever\fP define these macros directly:
472 instead, the appropriate feature test macro(s) from the
473 list above should be employed.
475 The program below can be used to explore how the various
476 feature test macros are set depending on the glibc version
477 and what feature test macros are explicitly set.
478 The following shell session, on a system with glibc 2.10,
479 shows some examples of what we would see:
485 _POSIX_SOURCE defined
486 _POSIX_C_SOURCE defined: 200809L
489 _ATFILE_SOURCE defined
490 $ \fBcc -D_XOPEN_SOURCE=500 ftm.c\fP
492 _POSIX_SOURCE defined
493 _POSIX_C_SOURCE defined: 199506L
494 _XOPEN_SOURCE defined: 500
495 $ \fBcc -D_GNU_SOURCE ftm.c\fP
497 _POSIX_SOURCE defined
498 _POSIX_C_SOURCE defined: 200809L
499 _ISOC99_SOURCE defined
500 _XOPEN_SOURCE defined: 700
501 _XOPEN_SOURCE_EXTENDED defined
502 _LARGEFILE64_SOURCE defined
505 _ATFILE_SOURCE defined
519 main(int argc, char *argv[])
522 printf("_POSIX_SOURCE defined\\n");
525 #ifdef _POSIX_C_SOURCE
526 printf("_POSIX_C_SOURCE defined: %ldL\\n", (long) _POSIX_C_SOURCE);
529 #ifdef _ISOC99_SOURCE
530 printf("_ISOC99_SOURCE defined\\n");
534 printf("_XOPEN_SOURCE defined: %d\\n", _XOPEN_SOURCE);
537 #ifdef _XOPEN_SOURCE_EXTENDED
538 printf("_XOPEN_SOURCE_EXTENDED defined\\n");
541 #ifdef _LARGEFILE64_SOURCE
542 printf("_LARGEFILE64_SOURCE defined\\n");
545 #ifdef _FILE_OFFSET_BITS
546 printf("_FILE_OFFSET_BITS defined: %d\\n", _FILE_OFFSET_BITS);
550 printf("_BSD_SOURCE defined\\n");
554 printf("_SVID_SOURCE defined\\n");
557 #ifdef _ATFILE_SOURCE
558 printf("_ATFILE_SOURCE defined\\n");
562 printf("_GNU_SOURCE defined\\n");
566 printf("_REENTRANT defined\\n");
570 printf("_THREAD_SAFE defined\\n");
573 #ifdef _FORTIFY_SOURCE
574 printf("_FORTIFY_SOURCE defined\\n");
584 The section "Feature Test Macros" under
586 .\" But beware: the info libc document is out of date (Jul 07, mtk)
588 .I /usr/include/features.h