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 2011-09-17 "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)
75 The \fB||\fP means that in order to obtain the declaration of
79 \fIeither\fP of the following macro
80 definitions must be made before including any header files:
85 #define _XOPEN_SOURCE /* or any value < 500 */
89 Alternatively, equivalent definitions can be included in the
95 cc \-D_XOPEN_SOURCE # Or any value < 500
99 Note that, as described below,
100 .BR "some feature test macros are defined by default" ,
101 so that it may not always be necessary to
102 explicitly specify the feature test macro(s) shown in the
105 In a few cases, manual pages use a shorthand for expressing the
106 feature test macro requirements (this example from
111 .B #define _GNU_SOURCE
112 .B #include <fcntl.h>
114 .BI "ssize_t readahead(int " fd ", off64_t *" offset ", size_t " count );
118 This format is employed in cases where only a single
119 feature test macro can be used to expose the function
120 declaration, and that macro is not defined by default.
121 .SS Feature test macros understood by glibc
122 The following paragraphs explain how feature test macros are handled
123 in Linux glibc 2.\fIx\fP, \fIx\fP > 0.
124 .\" The details in glibc 2.0 are simpler, but combining a
125 .\" a description of them with the details in later glibc versions
126 .\" would make for a complicated description.
128 Linux glibc understands the following feature test macros:
132 This macro is implicitly defined by
134 when invoked with, for example, the
141 Defining this macro causes header files to expose definitions as follows:
144 The value 1 exposes definitions conforming to POSIX.1-1990 and
147 The value 2 or greater additionally exposes
148 definitions for POSIX.2-1992.
150 The value 199309L or greater additionally exposes
151 definitions for POSIX.1b (real-time extensions).
152 .\" 199506L functionality is only available since glibc 2.1
154 The value 199506L or greater additionally exposes
155 definitions for POSIX.1c (threads).
158 The value 200112L or greater exposes definitions corresponding
159 to the POSIX.1-2001 base specification (excluding the XSI extension).
162 The value 200809L or greater exposes definitions corresponding
163 to the POSIX.1-2008 base specification (excluding the XSI extension).
167 Defining this obsolete macro with any value is equivalent to defining
172 Defining this macro causes header files to expose definitions as follows:
175 Defining with any value exposes
176 definitions conforming to POSIX.1, POSIX.2, and XPG4.
178 The value 500 or greater additionally exposes
179 definitions for SUSv2 (UNIX 98).
181 (Since glibc 2.2) The value 600 or greater additionally exposes
182 definitions for SUSv3 (UNIX 03; i.e., the POSIX.1-2001 base specification
183 plus the XSI extension) and C99 definitions.
185 (Since glibc 2.10) The value 700 or greater additionally exposes
186 definitions for SUSv4 (i.e., the POSIX.1-2008 base specification
187 plus the XSI extension).
190 .B _XOPEN_SOURCE_EXTENDED
191 If this macro is defined, and
193 is defined, then expose definitions corresponding to the XPG4v2
194 (SUSv1) UNIX extensions (UNIX 95).
195 This macro is also implicitly defined if
197 is defined with a value of 500 or more.
200 Exposes ISO C (1990) Amendment 1 definitions (also known as C95).
201 This macro is recognized since glibc 2.12.
202 The primary change in C95 was support for international character sets.
203 The C95 changes were included in the subsequent C99 standard
207 .BR _ISOC95_SOURCE ).
210 Exposes C99 extensions to ISO C (1990).
211 This macro is recognized since glibc 2.1.3;
212 earlier glibc 2.1.x versions recognized an equivalent macro named
214 (because the C99 standard had not then been finalized).
215 Although the use of the latter macro is obsolete, glibc continues
216 to recognize it for backward compatibility.
218 .B _LARGEFILE64_SOURCE
219 Expose definitions for the alternative API specified by the
220 LFS (Large File Summit) as a "transitional extension" to the
221 Single UNIX Specification.
222 (See http://opengroup.org/platform/lfs.html.)
223 The alternative API consists of a set of new objects
224 (i.e., functions and types) whose names are suffixed with "64"
233 New programs should not employ this interface; instead
234 .I _FILE_OFFSET_BITS=64
238 Defining this macro with the value 64
239 automatically converts references to 32-bit functions and data types
240 related to file I/O and file system operations into references to
241 their 64-bit counterparts.
242 This is useful for performing I/O on large files (> 2 Gigabytes)
244 (Defining this macro permits correctly written programs to use
245 large files with only a recompilation being required.)
246 64-bit systems naturally permit file sizes greater than 2 Gigabytes,
247 and on those systems this macro has no effect.
250 Defining this macro with any value causes header files to expose
251 BSD-derived definitions.
252 Defining this macro also causes BSD definitions to be preferred in
253 some situations where standards conflict, unless one or more of
256 .BR _POSIX_C_SOURCE ,
258 .BR _XOPEN_SOURCE_EXTENDED ,
261 is defined, in which case BSD definitions are disfavored.
264 Defining this macro with any value causes header files to expose
265 System V-derived definitions.
266 (SVID == System V Interface Definition; see
269 .BR _ATFILE_SOURCE " (since glibc 2.4)"
270 Defining this macro with any value causes header files to expose
271 declarations of a range of functions with the suffix "at";
274 Since glibc 2.10, this macro is also implicitly defined if
276 is defined with a value greater than or equal to 200809L.
279 Defining this macro (with any value) is equivalent to defining
283 .BR _LARGEFILE64_SOURCE ,
285 .BR _XOPEN_SOURCE_EXTENDED ,
288 with the value 200809L
289 (200112L in glibc versions before 2.10;
290 199506L in glibc versions before 2.5;
291 199309L in glibc versions before 2.1)
295 (600 in glibc versions before 2.10;
296 500 in glibc versions before 2.2).
297 In addition, various GNU-specific extensions are also exposed.
298 Where standards conflict, BSD definitions are disfavored.
301 Defining this macro exposes definitions of certain reentrant functions.
302 For multithreaded programs, use
309 provided for compatibility with some other implementations.
311 .BR _FORTIFY_SOURCE " (since glibc 2.3.4)"
312 .\" For more detail, see:
313 .\" http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
314 .\" [PATCH] Object size checking to prevent (some) buffer overflows
315 .\" * From: Jakub Jelinek <jakub at redhat dot com>
316 .\" * To: gcc-patches at gcc dot gnu dot org
317 .\" * Date: Tue, 21 Sep 2004 04:16:40 -0400
318 Defining this macro causes some lightweight checks to be performed
319 to detect some buffer overflow errors when employing
320 various string and memory manipulation functions.
321 Not all buffer overflows are detected, just some common cases.
322 In the current implementation checks are added for
341 is set to 1, with compiler optimization level 1
343 and above, checks that shouldn't change the behavior of
344 conforming programs are performed.
347 set to 2 some more checking is added, but
348 some conforming programs might fail.
349 Some of the checks can be performed at compile time,
350 and result in compiler warnings;
351 other checks take place at run time,
352 and result in a run-time error if the check fails.
353 Use of this macro requires compiler support, available with
356 .SS Default definitions, implicit definitions, and combining definitions
358 If no feature test macros are explicitly defined,
359 then the following feature test macros are defined by default:
364 .BR _POSIX_C_SOURCE =200809L
365 (200112L in glibc versions before 2.10;
366 199506L in glibc versions before 2.4;
367 199309L in glibc versions before 2.1).
370 .BR __STRICT_ANSI__ ,
373 .BR _POSIX_C_SOURCE ,
375 .BR _XOPEN_SOURCE_EXTENDED ,
379 is explicitly defined, then
383 are not defined by default.
389 are not explicitly defined,
394 is defined with a value of 500 or more, then
398 is defined with the value 1; and
401 is defined with one of the following values:
407 is defined with a value less than 500;
412 is defined with a value greater than or equal to 500 and less than 600;
415 (since glibc 2.4) 200112L,
418 is defined with a value greater than or equal to 600 and less than 700.
424 is defined with a value greater than or equal to 700.
426 Older versions of glibc do not know about the values
427 200112L and 200809L for
428 .BR _POSIX_C_SOURCE ,
429 and the setting of this macro will depend on the glibc version.
433 is undefined, then the setting of
435 depends on the glibc version:
436 199506L, in glibc versions before 2.4;
437 200112L, in glibc 2.4 to 2.9; and
438 200809L, since glibc 2.10.
442 Multiple macros can be defined; the results are additive.
445 .BR _POSIX_C_SOURCE ,
449 .B _XOPEN_SOURCE_EXTENDED
450 was specified by XPG4v2 (aka SUSv1).
453 is not specified by any standard,
454 but is employed on some other implementations.
460 .BR _FORTIFY_SOURCE ,
464 are specific to Linux (glibc).
467 is a Linux/glibc-specific header file.
468 Other systems have an analogous file, but typically with a different name.
469 This header file is automatically included by other header files as
470 required: it is not necessary to explicitly include it in order to
471 employ feature test macros.
473 According to which of the above feature test macros are defined,
475 internally defines various other macros that are checked by
476 other glibc header files.
477 These macros have names prefixed by two underscores (e.g.,
479 Programs should \fInever\fP define these macros directly:
480 instead, the appropriate feature test macro(s) from the
481 list above should be employed.
483 The program below can be used to explore how the various
484 feature test macros are set depending on the glibc version
485 and what feature test macros are explicitly set.
486 The following shell session, on a system with glibc 2.10,
487 shows some examples of what we would see:
493 _POSIX_SOURCE defined
494 _POSIX_C_SOURCE defined: 200809L
497 _ATFILE_SOURCE defined
498 $ \fBcc \-D_XOPEN_SOURCE=500 ftm.c\fP
500 _POSIX_SOURCE defined
501 _POSIX_C_SOURCE defined: 199506L
502 _XOPEN_SOURCE defined: 500
503 $ \fBcc \-D_GNU_SOURCE ftm.c\fP
505 _POSIX_SOURCE defined
506 _POSIX_C_SOURCE defined: 200809L
507 _ISOC99_SOURCE defined
508 _XOPEN_SOURCE defined: 700
509 _XOPEN_SOURCE_EXTENDED defined
510 _LARGEFILE64_SOURCE defined
513 _ATFILE_SOURCE defined
527 main(int argc, char *argv[])
530 printf("_POSIX_SOURCE defined\\n");
533 #ifdef _POSIX_C_SOURCE
534 printf("_POSIX_C_SOURCE defined: %ldL\\n", (long) _POSIX_C_SOURCE);
537 #ifdef _ISOC99_SOURCE
538 printf("_ISOC99_SOURCE defined\\n");
542 printf("_XOPEN_SOURCE defined: %d\\n", _XOPEN_SOURCE);
545 #ifdef _XOPEN_SOURCE_EXTENDED
546 printf("_XOPEN_SOURCE_EXTENDED defined\\n");
549 #ifdef _LARGEFILE64_SOURCE
550 printf("_LARGEFILE64_SOURCE defined\\n");
553 #ifdef _FILE_OFFSET_BITS
554 printf("_FILE_OFFSET_BITS defined: %d\\n", _FILE_OFFSET_BITS);
558 printf("_BSD_SOURCE defined\\n");
562 printf("_SVID_SOURCE defined\\n");
565 #ifdef _ATFILE_SOURCE
566 printf("_ATFILE_SOURCE defined\\n");
570 printf("_GNU_SOURCE defined\\n");
574 printf("_REENTRANT defined\\n");
578 printf("_THREAD_SAFE defined\\n");
581 #ifdef _FORTIFY_SOURCE
582 printf("_FORTIFY_SOURCE defined\\n");
592 The section "Feature Test Macros" under
594 .\" But beware: the info libc document is out of date (Jul 07, mtk)
596 .I /usr/include/features.h