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