]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/feature_test_macros.7
168cc3543399e03260bc734bfbcaaee09b384b3e
[thirdparty/man-pages.git] / man7 / feature_test_macros.7
1 .\" This manpage is Copyright (C) 2006, Michael Kerrisk
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .TH FEATURE_TEST_MACROS 7 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
6 .SH NAME
7 feature_test_macros \- feature test macros
8 .SH DESCRIPTION
9 Feature test macros allow the programmer to control the definitions that
10 are exposed by system header files when a program is compiled.
11 .PP
12 .B NOTE:
13 In order to be effective, a feature test macro
14 .IR "must be defined before including any header files" .
15 This can be done either in the compilation command
16 .RI ( "cc \-DMACRO=value" )
17 or by defining the macro within the source code before
18 including any headers.
19 The requirement that the macro must be defined before including any
20 header file exists because header files may freely include one another.
21 Thus, for example, in the following lines, defining the
22 .B _GNU_SOURCE
23 macro may have no effect because the header
24 .I <abc.h>
25 itself includes
26 .I <xyz.h>
27 (POSIX explicitly allows this):
28 .PP
29 .in +4n
30 .EX
31 #include <abc.h>
32 #define _GNU_SOURCE
33 #include <xyz.h>
34 .EE
35 .in
36 .PP
37 Some feature test macros are useful for creating portable applications,
38 by preventing nonstandard definitions from being exposed.
39 Other macros can be used to expose nonstandard definitions that
40 are not exposed by default.
41 .PP
42 The precise effects of each of the feature test macros described below
43 can be ascertained by inspecting the
44 .I <features.h>
45 header file.
46 .BR Note :
47 applications do
48 .I not
49 need to directly include
50 .IR <features.h> ;
51 indeed, doing so is actively discouraged.
52 See NOTES.
53 .SS Specification of feature test macro requirements in manual pages
54 When a function requires that a feature test macro is defined,
55 the manual page SYNOPSIS typically includes a note of the following form
56 (this example from the
57 .BR acct (2)
58 manual page):
59 .PP
60 .RS
61 .B #include <unistd.h>
62 .PP
63 .BI "int acct(const char *" filename );
64 .PP
65 .RS -4
66 .EX
67 Feature Test Macro Requirements for glibc (see
68 .BR feature_test_macros (7)):
69 .EE
70 .RE
71 .PP
72 .BR acct ():
73 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
74 .RE
75 .PP
76 The
77 .B ||
78 means that in order to obtain the declaration of
79 .BR acct (2)
80 from
81 .IR <unistd.h> ,
82 .I either
83 of the following macro
84 definitions must be made before including any header files:
85 .PP
86 .in +4n
87 .EX
88 #define _BSD_SOURCE
89 #define _XOPEN_SOURCE /* or any value < 500 */
90 .EE
91 .in
92 .PP
93 Alternatively, equivalent definitions can be included in the
94 compilation command:
95 .PP
96 .in +4n
97 .EX
98 cc \-D_BSD_SOURCE
99 cc \-D_XOPEN_SOURCE # Or any value < 500
100 .EE
101 .in
102 .PP
103 Note that, as described below,
104 .BR "some feature test macros are defined by default" ,
105 so that it may not always be necessary to
106 explicitly specify the feature test macro(s) shown in the
107 SYNOPSIS.
108 .PP
109 In a few cases, manual pages use a shorthand for expressing the
110 feature test macro requirements (this example from
111 .BR readahead (2)):
112 .PP
113 .RS +4
114 .EX
115 .B #define _GNU_SOURCE
116 .B #include <fcntl.h>
117 .PP
118 .BI "ssize_t readahead(int " fd ", off64_t *" offset ", size_t " count );
119 .EE
120 .RE
121 .PP
122 This format is employed in cases where only a single
123 feature test macro can be used to expose the function
124 declaration, and that macro is not defined by default.
125 .SS Feature test macros understood by glibc
126 The paragraphs below explain how feature test macros are handled
127 in glibc 2.\fIx\fP,
128 .I x
129 > 0.
130 .PP
131 First, though, a summary of a few details for the impatient:
132 .IP * 3
133 The macros that you most likely need to use in modern source code are
134 .B _POSIX_C_SOURCE
135 (for definitions from various versions of POSIX.1),
136 .B _XOPEN_SOURCE
137 (for definitions from various versions of SUS),
138 .B _GNU_SOURCE
139 (for GNU and/or Linux specific stuff), and
140 .B _DEFAULT_SOURCE
141 (to get definitions that would normally be provided by default).
142 .IP *
143 Certain macros are defined with default values.
144 Thus, although one or more macros may be indicated as being
145 required in the SYNOPSIS of a man page,
146 it may not be necessary to define them explicitly.
147 Full details of the defaults are given later in this man page.
148 .IP *
149 Defining
150 .B _XOPEN_SOURCE
151 with a value of 600 or greater produces the same effects as defining
152 .B _POSIX_C_SOURCE
153 with a value of 200112L or greater.
154 Where one sees
155 .IP
156 .in +4n
157 .EX
158 _POSIX_C_SOURCE >= 200112L
159 .EE
160 .in
161 .IP
162 in the feature test macro requirements in the SYNOPSIS of a man page,
163 it is implicit that the following has the same effect:
164 .IP
165 .in +4n
166 .EX
167 _XOPEN_SOURCE >= 600
168 .EE
169 .in
170 .IP *
171 Defining
172 .B _XOPEN_SOURCE
173 with a value of 700 or greater produces the same effects as defining
174 .B _POSIX_C_SOURCE
175 with a value of 200809L or greater.
176 Where one sees
177 .IP
178 .in +4n
179 .EX
180 _POSIX_C_SOURCE >= 200809L
181 .EE
182 .in
183 .IP
184 in the feature test macro requirements in the SYNOPSIS of a man page,
185 it is implicit that the following has the same effect:
186 .IP
187 .in +4n
188 .EX
189 _XOPEN_SOURCE >= 700
190 .EE
191 .in
192 .\" The details in glibc 2.0 are simpler, but combining a
193 .\" a description of them with the details in later glibc versions
194 .\" would make for a complicated description.
195 .PP
196 Glibc understands the following feature test macros:
197 .TP
198 .B __STRICT_ANSI__
199 ISO Standard C.
200 This macro is implicitly defined by
201 .BR gcc (1)
202 when invoked with, for example, the
203 .I \-std=c99
204 or
205 .I \-ansi
206 flag.
207 .TP
208 .B _POSIX_C_SOURCE
209 Defining this macro causes header files to expose definitions as follows:
210 .RS
211 .IP \(bu 3
212 The value 1 exposes definitions conforming to POSIX.1-1990 and
213 ISO C (1990).
214 .IP \(bu
215 The value 2 or greater additionally exposes
216 definitions for POSIX.2-1992.
217 .IP \(bu
218 The value 199309L or greater additionally exposes
219 definitions for POSIX.1b (real-time extensions).
220 .\" 199506L functionality is available only since glibc 2.1
221 .IP \(bu
222 The value 199506L or greater additionally exposes
223 definitions for POSIX.1c (threads).
224 .IP \(bu
225 (Since glibc 2.3.3)
226 The value 200112L or greater additionally exposes definitions corresponding
227 to the POSIX.1-2001 base specification (excluding the XSI extension).
228 This value also causes C95 (since glibc 2.12) and
229 C99 (since glibc 2.10) features to be exposed
230 (in other words, the equivalent of defining
231 .BR _ISOC99_SOURCE ).
232 .IP \(bu
233 (Since glibc 2.10)
234 The value 200809L or greater additionally exposes definitions corresponding
235 to the POSIX.1-2008 base specification (excluding the XSI extension).
236 .RE
237 .TP
238 .B _POSIX_SOURCE
239 Defining this obsolete macro with any value is equivalent to defining
240 .B _POSIX_C_SOURCE
241 with the value 1.
242 .IP
243 Since this macro is obsolete,
244 its usage is generally not documented when discussing
245 feature test macro requirements in the man pages.
246 .TP
247 .B _XOPEN_SOURCE
248 Defining this macro causes header files to expose definitions as follows:
249 .RS
250 .IP \(bu 3
251 Defining with any value exposes
252 definitions conforming to POSIX.1, POSIX.2, and XPG4.
253 .IP \(bu
254 The value 500 or greater additionally exposes
255 definitions for SUSv2 (UNIX 98).
256 .IP \(bu
257 (Since glibc 2.2) The value 600 or greater additionally exposes
258 definitions for SUSv3 (UNIX 03; i.e., the POSIX.1-2001 base specification
259 plus the XSI extension) and C99 definitions.
260 .IP \(bu
261 (Since glibc 2.10) The value 700 or greater additionally exposes
262 definitions for SUSv4 (i.e., the POSIX.1-2008 base specification
263 plus the XSI extension).
264 .RE
265 .IP
266 If
267 .B __STRICT_ANSI__
268 is not defined, or
269 .B _XOPEN_SOURCE
270 is defined with a value greater than or equal to 500
271 .I and
272 neither
273 .B _POSIX_SOURCE
274 nor
275 .B _POSIX_C_SOURCE
276 is explicitly defined, then
277 the following macros are implicitly defined:
278 .RS
279 .IP \(bu 3
280 .B _POSIX_SOURCE
281 is defined with the value 1.
282 .IP \(bu
283 .B _POSIX_C_SOURCE
284 is defined, according to the value of
285 .BR _XOPEN_SOURCE :
286 .RS
287 .TP
288 .BR _XOPEN_SOURCE " < 500"
289 .B _POSIX_C_SOURCE
290 is defined with the value 2.
291 .TP
292 .RB "500 <= " _XOPEN_SOURCE " < 600"
293 .B _POSIX_C_SOURCE
294 is defined with the value 199506L.
295 .TP
296 .RB "600 <= " _XOPEN_SOURCE " < 700"
297 .B _POSIX_C_SOURCE
298 is defined with the value 200112L.
299 .TP
300 .RB "700 <= " _XOPEN_SOURCE " (since glibc 2.10)"
301 .B _POSIX_C_SOURCE
302 is defined with the value 200809L.
303 .RE
304 .RE
305 .IP
306 In addition, defining
307 .B _XOPEN_SOURCE
308 with a value of 500 or greater produces the same effects as defining
309 .BR _XOPEN_SOURCE_EXTENDED .
310 .TP
311 .B _XOPEN_SOURCE_EXTENDED
312 If this macro is defined,
313 .I and
314 .B _XOPEN_SOURCE
315 is defined, then expose definitions corresponding to the XPG4v2
316 (SUSv1) UNIX extensions (UNIX 95).
317 Defining
318 .B _XOPEN_SOURCE
319 with a value of 500 or more also produces the same effect as defining
320 .BR _XOPEN_SOURCE_EXTENDED .
321 Use of
322 .B _XOPEN_SOURCE_EXTENDED
323 in new source code should be avoided.
324 .IP
325 Since defining
326 .B _XOPEN_SOURCE
327 with a value of 500 or more has the same effect as defining
328 .BR _XOPEN_SOURCE_EXTENDED ,
329 the latter (obsolete) feature test macro is generally not described in the
330 SYNOPSIS in man pages.
331 .TP
332 .BR _ISOC99_SOURCE " (since glibc 2.1.3)"
333 Exposes declarations consistent with the ISO C99 standard.
334 .IP
335 Earlier glibc 2.1.x versions recognized an equivalent macro named
336 .B _ISOC9X_SOURCE
337 (because the C99 standard had not then been finalized).
338 Although the use of this macro is obsolete, glibc continues
339 to recognize it for backward compatibility.
340 .IP
341 Defining
342 .B _ISOC99_SOURCE
343 also exposes ISO C (1990) Amendment 1 ("C95") definitions.
344 (The primary change in C95 was support for international character sets.)
345 .IP
346 Invoking the C compiler with the option
347 .I \-std=c99
348 produces the same effects as defining this macro.
349 .TP
350 .BR _ISOC11_SOURCE " (since glibc 2.16)"
351 Exposes declarations consistent with the ISO C11 standard.
352 Defining this macro also enables C99 and C95 features (like
353 .BR _ISOC99_SOURCE ).
354 .IP
355 Invoking the C compiler with the option
356 .I \-std=c11
357 produces the same effects as defining this macro.
358 .TP
359 .B _LARGEFILE64_SOURCE
360 Expose definitions for the alternative API specified by the
361 LFS (Large File Summit) as a "transitional extension" to the
362 Single UNIX Specification.
363 (See
364 .UR http:\:/\:/opengroup.org\:/platform\:/lfs.html
365 .UE .)
366 The alternative API consists of a set of new objects
367 (i.e., functions and types) whose names are suffixed with "64"
368 (e.g.,
369 .I off64_t
370 versus
371 .IR off_t ,
372 .BR lseek64 ()
373 versus
374 .BR lseek (),
375 etc.).
376 New programs should not employ this macro; instead
377 .I _FILE_OFFSET_BITS=64
378 should be employed.
379 .TP
380 .B _LARGEFILE_SOURCE
381 This macro was historically used to expose certain functions (specifically
382 .BR fseeko (3)
383 and
384 .BR ftello (3))
385 that address limitations of earlier APIs
386 .RB ( fseek (3)
387 and
388 .BR ftell (3))
389 that use
390 .I long
391 for file offsets.
392 This macro is implicitly defined if
393 .B _XOPEN_SOURCE
394 is defined with a value greater than or equal to 500.
395 New programs should not employ this macro;
396 defining
397 .B _XOPEN_SOURCE
398 as just described or defining
399 .B _FILE_OFFSET_BITS
400 with the value 64 is the preferred mechanism to achieve the same result.
401 .TP
402 .B _FILE_OFFSET_BITS
403 Defining this macro with the value 64
404 automatically converts references to 32-bit functions and data types
405 related to file I/O and filesystem operations into references to
406 their 64-bit counterparts.
407 This is useful for performing I/O on large files (> 2 Gigabytes)
408 on 32-bit systems.
409 (Defining this macro permits correctly written programs to use
410 large files with only a recompilation being required.)
411 .IP
412 64-bit systems naturally permit file sizes greater than 2 Gigabytes,
413 and on those systems this macro has no effect.
414 .TP
415 .BR _BSD_SOURCE " (deprecated since glibc 2.20)"
416 Defining this macro with any value causes header files to expose
417 BSD-derived definitions.
418 .IP
419 In glibc versions up to and including 2.18,
420 defining this macro also causes BSD definitions to be preferred in
421 some situations where standards conflict, unless one or more of
422 .BR _SVID_SOURCE ,
423 .BR _POSIX_SOURCE ,
424 .BR _POSIX_C_SOURCE ,
425 .BR _XOPEN_SOURCE ,
426 .BR _XOPEN_SOURCE_EXTENDED ,
427 or
428 .B _GNU_SOURCE
429 is defined, in which case BSD definitions are disfavored.
430 Since glibc 2.19,
431 .B _BSD_SOURCE
432 no longer causes BSD definitions to be preferred in case of conflicts.
433 .IP
434 Since glibc 2.20, this macro is deprecated.
435 .\" commit c941736c92fa3a319221f65f6755659b2a5e0a20
436 .\" commit 498afc54dfee41d33ba519f496e96480badace8e
437 .\" commit acd7f096d79c181866d56d4aaf3b043e741f1e2c
438 It now has the same effect as defining
439 .BR _DEFAULT_SOURCE ,
440 but generates a compile-time warning (unless
441 .B _DEFAULT_SOURCE
442 .\" commit ade40b10ff5fa59a318cf55b9d8414b758e8df78
443 is also defined).
444 Use
445 .B _DEFAULT_SOURCE
446 instead.
447 To allow code that requires
448 .B _BSD_SOURCE
449 in glibc 2.19 and earlier and
450 .B _DEFAULT_SOURCE
451 in glibc 2.20 and later to compile without warnings, define
452 .I both
453 .B _BSD_SOURCE
454 and
455 .BR _DEFAULT_SOURCE .
456 .TP
457 .BR _SVID_SOURCE " (deprecated since glibc 2.20)"
458 Defining this macro with any value causes header files to expose
459 System V-derived definitions.
460 (SVID == System V Interface Definition; see
461 .BR standards (7).)
462 .IP
463 Since glibc 2.20, this macro is deprecated in the same fashion as
464 .BR _BSD_SOURCE .
465 .TP
466 .BR _DEFAULT_SOURCE " (since glibc 2.19)"
467 This macro can be defined to ensure that the "default"
468 definitions are provided even when the defaults would otherwise
469 be disabled,
470 as happens when individual macros are explicitly defined,
471 or the compiler is invoked in one of its "standard" modes (e.g.,
472 .IR cc\~\-std=c99 ).
473 Defining
474 .B _DEFAULT_SOURCE
475 without defining other individual macros
476 or invoking the compiler in one of its "standard" modes has no effect.
477 .IP
478 The "default" definitions comprise those required by POSIX.1-2008 and ISO C99,
479 as well as various definitions originally derived from BSD and System V.
480 On glibc 2.19 and earlier, these defaults were approximately equivalent
481 to explicitly defining the following:
482 .IP
483 .in +4n
484 .EX
485 cc \-D_BSD_SOURCE \-D_SVID_SOURCE \-D_POSIX_C_SOURCE=200809
486 .EE
487 .in
488 .TP
489 .BR _ATFILE_SOURCE " (since glibc 2.4)"
490 Defining this macro with any value causes header files to expose
491 declarations of a range of functions with the suffix "at";
492 see
493 .BR openat (2).
494 Since glibc 2.10, this macro is also implicitly defined if
495 .B _POSIX_C_SOURCE
496 is defined with a value greater than or equal to 200809L.
497 .TP
498 .B _GNU_SOURCE
499 Defining this macro (with any value) implicitly defines
500 .BR _ATFILE_SOURCE ,
501 .BR _LARGEFILE64_SOURCE ,
502 .BR _ISOC99_SOURCE ,
503 .BR _XOPEN_SOURCE_EXTENDED ,
504 .BR _POSIX_SOURCE ,
505 .B _POSIX_C_SOURCE
506 with the value 200809L
507 (200112L in glibc versions before 2.10;
508 199506L in glibc versions before 2.5;
509 199309L in glibc versions before 2.1)
510 and
511 .B _XOPEN_SOURCE
512 with the value 700
513 (600 in glibc versions before 2.10;
514 500 in glibc versions before 2.2).
515 In addition, various GNU-specific extensions are also exposed.
516 .IP
517 Since glibc 2.19, defining
518 .B _GNU_SOURCE
519 also has the effect of implicitly defining
520 .BR _DEFAULT_SOURCE .
521 In glibc versions before 2.20, defining
522 .B _GNU_SOURCE
523 also had the effect of implicitly defining
524 .B _BSD_SOURCE
525 and
526 .BR _SVID_SOURCE .
527 .TP
528 .B _REENTRANT
529 Historically, on various C libraries
530 it was necessary to define this macro in all
531 multithreaded code.
532 .\" Zack Weinberg
533 .\" There did once exist C libraries where it was necessary. The ones
534 .\" I remember were proprietary Unix vendor libcs from the mid-1990s
535 .\" You would get completely unlocked stdio without _REENTRANT.
536 (Some C libraries may still require this.)
537 In glibc,
538 this macro also exposed definitions of certain reentrant functions.
539 .IP
540 However, glibc has been thread-safe by default for many years;
541 since glibc 2.3, the only effect of defining
542 .B _REENTRANT
543 has been to enable one or two of the same declarations that
544 are also enabled by defining
545 .B _POSIX_C_SOURCE
546 with a value of 199606L or greater.
547 .IP
548 .B _REENTRANT
549 is now obsolete.
550 In glibc 2.25 and later, defining
551 .B _REENTRANT
552 is equivalent to defining
553 .B _POSIX_C_SOURCE
554 with the value 199606L.
555 If a higher POSIX conformance level is
556 selected by any other means (such as
557 .B _POSIX_C_SOURCE
558 itself,
559 .BR _XOPEN_SOURCE ,
560 .BR _DEFAULT_SOURCE ,
561 or
562 .BR _GNU_SOURCE ),
563 then defining
564 .B _REENTRANT
565 has no effect.
566 .IP
567 This macro is automatically defined if one compiles with
568 .IR cc\~\-pthread .
569 .TP
570 .B _THREAD_SAFE
571 Synonym for the (deprecated)
572 .BR _REENTRANT ,
573 provided for compatibility with some other implementations.
574 .TP
575 .BR _FORTIFY_SOURCE " (since glibc 2.3.4)"
576 .\" For more detail, see:
577 .\" http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
578 .\" [PATCH] Object size checking to prevent (some) buffer overflows
579 .\" * From: Jakub Jelinek <jakub at redhat dot com>
580 .\" * To: gcc-patches at gcc dot gnu dot org
581 .\" * Date: Tue, 21 Sep 2004 04:16:40 -0400
582 Defining this macro causes some lightweight checks to be performed
583 to detect some buffer overflow errors when employing
584 various string and memory manipulation functions (for example,
585 .BR memcpy (3),
586 .BR memset (3),
587 .BR stpcpy (3),
588 .BR strcpy (3),
589 .BR strncpy (3),
590 .BR strcat (3),
591 .BR strncat (3),
592 .BR sprintf (3),
593 .BR snprintf (3),
594 .BR vsprintf (3),
595 .BR vsnprintf (3),
596 .BR gets (3),
597 and wide character variants thereof).
598 For some functions, argument consistency is checked;
599 for example, a check is made that
600 .BR open (2)
601 has been supplied with a
602 .I mode
603 argument when the specified flags include
604 .BR O_CREAT .
605 Not all problems are detected, just some common cases.
606 .\" Look for __USE_FORTIFY_LEVEL in the header files
607 .IP
608 If
609 .B _FORTIFY_SOURCE
610 is set to 1, with compiler optimization level 1
611 .RI ( "gcc\ \-O1" )
612 and above, checks that shouldn't change the behavior of
613 conforming programs are performed.
614 With
615 .B _FORTIFY_SOURCE
616 set to 2, some more checking is added, but
617 some conforming programs might fail.
618 .\" For example, given the following code
619 .\" int d;
620 .\" char buf[1000], buf[1000];
621 .\" strcpy(fmt, "Hello world\n%n");
622 .\" snprintf(buf, sizeof(buf), fmt, &d);
623 .\"
624 .\" Compiling with "gcc -D_FORTIFY_SOURCE=2 -O1" and then running will
625 .\" cause the following diagnostic at run time at the snprintf() call
626 .\"
627 .\" *** %n in writable segment detected ***
628 .\" Aborted (core dumped)
629 .\"
630 .IP
631 Some of the checks can be performed at compile time
632 (via macros logic implemented in header files),
633 and result in compiler warnings;
634 other checks take place at run time,
635 and result in a run-time error if the check fails.
636 .IP
637 Use of this macro requires compiler support, available with
638 .BR gcc (1)
639 since version 4.0.
640 .SS Default definitions, implicit definitions, and combining definitions
641 If no feature test macros are explicitly defined,
642 then the following feature test macros are defined by default:
643 .B _BSD_SOURCE
644 (in glibc 2.19 and earlier),
645 .B _SVID_SOURCE
646 (in glibc 2.19 and earlier),
647 .B _DEFAULT_SOURCE
648 (since glibc 2.19),
649 .BR _POSIX_SOURCE ,
650 and
651 .BR _POSIX_C_SOURCE =200809L
652 (200112L in glibc versions before 2.10;
653 199506L in glibc versions before 2.4;
654 199309L in glibc versions before 2.1).
655 .PP
656 If any of
657 .BR __STRICT_ANSI__ ,
658 .BR _ISOC99_SOURCE ,
659 .B _ISOC11_SOURCE
660 (since glibc 2.18),
661 .BR _POSIX_SOURCE ,
662 .BR _POSIX_C_SOURCE ,
663 .BR _XOPEN_SOURCE ,
664 .B _XOPEN_SOURCE_EXTENDED
665 (in glibc 2.11 and earlier),
666 .B _BSD_SOURCE
667 (in glibc 2.19 and earlier),
668 or
669 .B _SVID_SOURCE
670 (in glibc 2.19 and earlier)
671 is explicitly defined, then
672 .BR _BSD_SOURCE ,
673 .BR _SVID_SOURCE ,
674 and
675 .B _DEFAULT_SOURCE
676 are not defined by default.
677 .PP
678 If
679 .B _POSIX_SOURCE
680 and
681 .B _POSIX_C_SOURCE
682 are not explicitly defined,
683 and either
684 .B __STRICT_ANSI__
685 is not defined or
686 .B _XOPEN_SOURCE
687 is defined with a value of 500 or more, then
688 .IP * 3
689 .B _POSIX_SOURCE
690 is defined with the value 1; and
691 .IP *
692 .B _POSIX_C_SOURCE
693 is defined with one of the following values:
694 .RS 3
695 .IP \(bu 3
696 2,
697 if
698 .B _XOPEN_SOURCE
699 is defined with a value less than 500;
700 .IP \(bu
701 199506L,
702 if
703 .B _XOPEN_SOURCE
704 is defined with a value greater than or equal to 500 and less than 600;
705 or
706 .IP \(bu
707 (since glibc 2.4) 200112L,
708 if
709 .B _XOPEN_SOURCE
710 is defined with a value greater than or equal to 600 and less than 700.
711 .IP \(bu
712 (Since glibc 2.10)
713 200809L,
714 if
715 .B _XOPEN_SOURCE
716 is defined with a value greater than or equal to 700.
717 .IP \(bu
718 Older versions of glibc do not know about the values
719 200112L and 200809L for
720 .BR _POSIX_C_SOURCE ,
721 and the setting of this macro will depend on the glibc version.
722 .IP \(bu
723 If
724 .B _XOPEN_SOURCE
725 is undefined, then the setting of
726 .B _POSIX_C_SOURCE
727 depends on the glibc version:
728 199506L, in glibc versions before 2.4;
729 200112L, in glibc 2.4 to 2.9; and
730 200809L, since glibc 2.10.
731 .RE
732 .PP
733 Multiple macros can be defined; the results are additive.
734 .SH STANDARDS
735 POSIX.1 specifies
736 .BR _POSIX_C_SOURCE ,
737 .BR _POSIX_SOURCE ,
738 and
739 .BR _XOPEN_SOURCE .
740 .PP
741 .B _XOPEN_SOURCE_EXTENDED
742 was specified by XPG4v2 (aka SUSv1), but is not present in SUSv2 and later.
743 .B _FILE_OFFSET_BITS
744 is not specified by any standard,
745 but is employed on some other implementations.
746 .PP
747 .BR _BSD_SOURCE ,
748 .BR _SVID_SOURCE ,
749 .BR _DEFAULT_SOURCE ,
750 .BR _ATFILE_SOURCE ,
751 .BR _GNU_SOURCE ,
752 .BR _FORTIFY_SOURCE ,
753 .BR _REENTRANT ,
754 and
755 .B _THREAD_SAFE
756 are specific to Linux (glibc).
757 .SH NOTES
758 .I <features.h>
759 is a Linux/glibc-specific header file.
760 Other systems have an analogous file, but typically with a different name.
761 This header file is automatically included by other header files as
762 required: it is not necessary to explicitly include it in order to
763 employ feature test macros.
764 .PP
765 According to which of the above feature test macros are defined,
766 .I <features.h>
767 internally defines various other macros that are checked by
768 other glibc header files.
769 These macros have names prefixed by two underscores (e.g.,
770 .BR __USE_MISC ).
771 Programs should
772 .I never
773 define these macros directly:
774 instead, the appropriate feature test macro(s) from the
775 list above should be employed.
776 .SH EXAMPLES
777 The program below can be used to explore how the various
778 feature test macros are set depending on the glibc version
779 and what feature test macros are explicitly set.
780 The following shell session, on a system with glibc 2.10,
781 shows some examples of what we would see:
782 .PP
783 .in +4n
784 .EX
785 $ \fBcc ftm.c\fP
786 $ \fB./a.out\fP
787 _POSIX_SOURCE defined
788 _POSIX_C_SOURCE defined: 200809L
789 _BSD_SOURCE defined
790 _SVID_SOURCE defined
791 _ATFILE_SOURCE defined
792 $ \fBcc \-D_XOPEN_SOURCE=500 ftm.c\fP
793 $ \fB./a.out\fP
794 _POSIX_SOURCE defined
795 _POSIX_C_SOURCE defined: 199506L
796 _XOPEN_SOURCE defined: 500
797 $ \fBcc \-D_GNU_SOURCE ftm.c\fP
798 $ \fB./a.out\fP
799 _POSIX_SOURCE defined
800 _POSIX_C_SOURCE defined: 200809L
801 _ISOC99_SOURCE defined
802 _XOPEN_SOURCE defined: 700
803 _XOPEN_SOURCE_EXTENDED defined
804 _LARGEFILE64_SOURCE defined
805 _BSD_SOURCE defined
806 _SVID_SOURCE defined
807 _ATFILE_SOURCE defined
808 _GNU_SOURCE defined
809 .EE
810 .in
811 .SS Program source
812 \&
813 .EX
814 /* ftm.c */
815
816 #include <stdint.h>
817 #include <stdio.h>
818 #include <unistd.h>
819 #include <stdlib.h>
820
821 int
822 main(int argc, char *argv[])
823 {
824 #ifdef _POSIX_SOURCE
825 printf("_POSIX_SOURCE defined\en");
826 #endif
827
828 #ifdef _POSIX_C_SOURCE
829 printf("_POSIX_C_SOURCE defined: %jdL\en",
830 (intmax_t) _POSIX_C_SOURCE);
831 #endif
832
833 #ifdef _ISOC99_SOURCE
834 printf("_ISOC99_SOURCE defined\en");
835 #endif
836
837 #ifdef _ISOC11_SOURCE
838 printf("_ISOC11_SOURCE defined\en");
839 #endif
840
841 #ifdef _XOPEN_SOURCE
842 printf("_XOPEN_SOURCE defined: %d\en", _XOPEN_SOURCE);
843 #endif
844
845 #ifdef _XOPEN_SOURCE_EXTENDED
846 printf("_XOPEN_SOURCE_EXTENDED defined\en");
847 #endif
848
849 #ifdef _LARGEFILE64_SOURCE
850 printf("_LARGEFILE64_SOURCE defined\en");
851 #endif
852
853 #ifdef _FILE_OFFSET_BITS
854 printf("_FILE_OFFSET_BITS defined: %d\en", _FILE_OFFSET_BITS);
855 #endif
856
857 #ifdef _BSD_SOURCE
858 printf("_BSD_SOURCE defined\en");
859 #endif
860
861 #ifdef _SVID_SOURCE
862 printf("_SVID_SOURCE defined\en");
863 #endif
864
865 #ifdef _DEFAULT_SOURCE
866 printf("_DEFAULT_SOURCE defined\en");
867 #endif
868
869 #ifdef _ATFILE_SOURCE
870 printf("_ATFILE_SOURCE defined\en");
871 #endif
872
873 #ifdef _GNU_SOURCE
874 printf("_GNU_SOURCE defined\en");
875 #endif
876
877 #ifdef _REENTRANT
878 printf("_REENTRANT defined\en");
879 #endif
880
881 #ifdef _THREAD_SAFE
882 printf("_THREAD_SAFE defined\en");
883 #endif
884
885 #ifdef _FORTIFY_SOURCE
886 printf("_FORTIFY_SOURCE defined\en");
887 #endif
888
889 exit(EXIT_SUCCESS);
890 }
891 .EE
892 .SH SEE ALSO
893 .BR libc (7),
894 .BR standards (7),
895 .BR system_data_types (7)
896 .PP
897 The section "Feature Test Macros" under
898 .IR "info libc" .
899 .\" But beware: the info libc document is out of date (Jul 07, mtk)
900 .PP
901 .I /usr/include/features.h