OSDN Git Service

(split) LDP man-pages の original/ を v3.29 に更新。
[linuxjm/LDP_man-pages.git] / original / man7 / feature_test_macros.7
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" This manpage is Copyright (C) 2006, Michael Kerrisk
4 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\"
25 .TH FEATURE_TEST_MACROS 7 2010-09-10 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 feature_test_macros \- feature test macros
28 .SH SYNOPSIS
29 .nf
30 .B #include <features.h>
31 .fi
32 .SH DESCRIPTION
33 Feature test macros allow the programmer to control the definitions that
34 are exposed by system header files when a program is compiled.
35
36 .B NOTE:
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.
43
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
50 .I <features.h>
51 header file.
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
56 .BR chmod (2)
57 manual page):
58 .RS 10
59 .sp
60 .B #include <sys/stat.h>
61 .sp
62 .BI "int chmod(const char *" path ", mode_t " mode );
63 .br
64 .BI "int fchmod(int " fd ", mode_t " mode );
65 .sp
66 .nf
67 .in -4n
68 Feature Test Macro Requirements for glibc (see
69 .BR feature_test_macros (7)):
70 .fi
71 .in
72 .sp
73 .BR fchmod ():
74 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
75 .RE
76 .PP
77 The \fB||\fP means that in order to obtain the declaration of
78 .BR fchmod (2)
79 from
80 .IR <sys/stat.h> ,
81 \fIeither\fP of the following macro
82 definitions must be made before including any header files:
83 .RS
84 .nf
85
86 #define _BSD_SOURCE
87 #define _XOPEN_SOURCE 500     /* or any value > 500 */
88 .fi
89 .RE
90 .PP
91 Alternatively, equivalent definitions can be included in the
92 compilation command:
93 .RS
94 .nf
95
96 cc \-D_BSD_SOURCE
97 cc \-D_XOPEN_SOURCE=500        # Or any value > 500
98 .fi
99 .RE
100 .PP
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
105 SYNOPSIS.
106
107 In a few cases, manual pages use a shorthand for expressing the
108 feature test macro requirements (this example from
109 .BR readahead (2)):
110 .RS
111 .nf
112
113 .B #define _GNU_SOURCE
114 .B #include <fcntl.h>
115 .sp
116 .BI "ssize_t readahead(int " fd ", off64_t *" offset ", size_t " count );
117 .fi
118 .RE
119 .PP
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.
129
130 Linux glibc understands the following feature test macros:
131 .TP
132 .B __STRICT_ANSI__
133 ISO Standard C.
134 This macro is implicitly defined by
135 .BR gcc (1)
136 when invoked with, for example, the
137 .I -std=c99
138 or
139 .I -ansi
140 flag.
141 .TP
142 .B _POSIX_C_SOURCE
143 Defining this macro causes header files to expose definitions as follows:
144 .RS
145 .IP \(bu 3
146 The value 1 exposes definitions conforming to POSIX.1-1990 and
147 ISO C (1990).
148 .IP \(bu
149 The value 2 or greater additionally exposes
150 definitions for POSIX.2-1992.
151 .IP \(bu
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
155 .IP \(bu
156 The value 199506L or greater additionally exposes
157 definitions for POSIX.1c (threads).
158 .IP \(bu
159 (Since glibc 2.3.3)
160 The value 200112L or greater exposes definitions corresponding
161 to the POSIX.1-2001 base specification (excluding the XSI extension).
162 .IP \(bu
163 (Since glibc 2.10)
164 The value 200809L or greater exposes definitions corresponding
165 to the POSIX.1-2008 base specification (excluding the XSI extension).
166 .RE
167 .TP
168 .B _POSIX_SOURCE
169 Defining this obsolete macro with any value is equivalent to defining
170 .B _POSIX_C_SOURCE
171 with the value 1.
172 .TP
173 .B _XOPEN_SOURCE
174 Defining this macro causes header files to expose definitions as follows:
175 .RS
176 .IP \(bu 3
177 Defining with any value exposes
178 definitions conforming to POSIX.1, POSIX.2, and XPG4.
179 .IP \(bu
180 The value 500 or greater additionally exposes
181 definitions for SUSv2 (UNIX 98).
182 .IP \(bu
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.
186 .IP \(bu
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).
190 .RE
191 .TP
192 .B _XOPEN_SOURCE_EXTENDED
193 If this macro is defined, and
194 .B _XOPEN_SOURCE
195 is defined, then expose definitions corresponding to the XPG4v2
196 (SUSv1) UNIX extensions (UNIX 95).
197 This macro is also implicitly defined if
198 .B _XOPEN_SOURCE
199 is defined with a value of 500 or more.
200 .TP
201 .B _ISOC99_SOURCE
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
205 .B _ISOC9X_SOURCE
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.
209 .TP
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"
217 (e.g.,
218 .I off64_t
219 versus
220 .IR off_t ,
221 .BR lseek64 ()
222 versus
223 .BR lseek (),
224 etc.).
225 New programs should not employ this interface; instead
226 .I _FILE_OFFSET_BITS=64
227 should be employed.
228 .TP
229 .B _FILE_OFFSET_BITS
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)
235 on 32-bit systems.
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.
240 .TP
241 .B _BSD_SOURCE
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
246 .BR _SVID_SOURCE ,
247 .BR _POSIX_SOURCE ,
248 .BR _POSIX_C_SOURCE ,
249 .BR _XOPEN_SOURCE ,
250 .BR _XOPEN_SOURCE_EXTENDED ,
251 or
252 .B _GNU_SOURCE
253 is defined, in which case BSD definitions are disfavored.
254 .TP
255 .B _SVID_SOURCE
256 Defining this macro with any value causes header files to expose
257 System V-derived definitions.
258 (SVID == System V Interface Definition; see
259 .BR standards (7).)
260 .TP
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";
264 see
265 .BR openat (2).
266 Since glibc 2.10, this macro is also implicitly defined if
267 .BR _POSIX_C_SOURCE
268 is defined with a value greater than or equal to 200809L.
269 .TP
270 .B _GNU_SOURCE
271 Defining this macro (with any value) is equivalent to defining
272 .BR _BSD_SOURCE ,
273 .BR _SVID_SOURCE ,
274 .BR _ATFILE_SOURCE ,
275 .BR _LARGEFILE64_SOURCE ,
276 .BR _ISOC99_SOURCE ,
277 .BR _XOPEN_SOURCE_EXTENDED ,
278 .BR _POSIX_SOURCE ,
279 .B _POSIX_C_SOURCE
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)
284 and
285 .B _XOPEN_SOURCE
286 with the value 700
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.
291 .TP
292 .B _REENTRANT
293 Defining this macro exposes definitions of certain reentrant functions.
294 For multithreaded programs, use
295 .I "cc\ \-pthread"
296 instead.
297 .TP
298 .B _THREAD_SAFE
299 Synonym for
300 .BR _REENTRANT ,
301 provided for compatibility with some other implementations.
302 .TP
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
315 calls to
316 .BR memcpy (3),
317 .BR mempcpy (3),
318 .BR memmove (3),
319 .BR memset (3),
320 .BR stpcpy (3),
321 .BR strcpy (3),
322 .BR strncpy (3),
323 .BR strcat (3),
324 .BR strncat (3),
325 .BR sprintf (3),
326 .BR snprintf (3),
327 .BR vsprintf (3),
328 .BR vsnprintf (3),
329 and
330 .BR gets (3).
331 If
332 .B _FORTIFY_SOURCE
333 is set to 1, with compiler optimization level 1
334 .RI ( "gcc\ \-O1" )
335 and above, checks that shouldn't change the behavior of
336 conforming programs are performed.
337 With
338 .B _FORTIFY_SOURCE
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
346 .BR gcc (1)
347 since version 4.0.
348 .SS Default definitions, implicit definitions, and combining definitions
349 .PP
350 If no feature test macros are explicitly defined,
351 then the following feature test macros are defined by default:
352 .BR _BSD_SOURCE ,
353 .BR _SVID_SOURCE ,
354 .BR _POSIX_SOURCE ,
355 and
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).
360 .PP
361 If any of
362 .BR __STRICT_ANSI__ ,
363 .BR _ISOC99_SOURCE ,
364 .BR _POSIX_SOURCE ,
365 .BR _POSIX_C_SOURCE  ,
366 .BR _XOPEN_SOURCE ,
367 .BR _XOPEN_SOURCE_EXTENDED ,
368 .BR _BSD_SOURCE ,
369 or
370 .B _SVID_SOURCE
371 is explicitly defined, then
372 .BR _BSD_SOURCE ,
373 and
374 .B _SVID_SOURCE
375 are not defined by default.
376
377 If
378 .B _POSIX_SOURCE
379 and
380 .B _POSIX_C_SOURCE
381 are not explicitly defined,
382 and either
383 .B __STRICT_ANSI__
384 is not defined or
385 .B _XOPEN_SOURCE
386 is defined with a value of 500 or more, then
387 .RS 3
388 .IP * 3
389 .B _POSIX_SOURCE
390 is defined with the value 1; and
391 .IP *
392 .B _POSIX_C_SOURCE
393 is defined with one of the following values:
394 .RS 6
395 .IP \(bu 3
396 2,
397 if
398 .B XOPEN_SOURCE
399 is defined with a value less than 500;
400 .IP \(bu
401 199506L,
402 if
403 .B XOPEN_SOURCE
404 is defined with a value greater than or equal to 500 and less than 600;
405 or
406 .IP \(bu
407 (since glibc 2.4) 200112L,
408 if
409 .B XOPEN_SOURCE
410 is defined with a value greater than or equal to 600 and less than 700.
411 .IP \(bu
412 (Since glibc 2.10)
413 200809L,
414 if
415 .B XOPEN_SOURCE
416 is defined with a value greater than or equal to 700.
417 .IP \(bu
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.
422 .IP \(bu
423 If
424 .B _XOPEN_SOURCE
425 is undefined, then the setting of
426 .B _POSIX_C_SOURCE
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.
431 .RE
432 .RE
433 .PP
434 Multiple macros can be defined; the results are additive.
435 .SH CONFORMING TO
436 POSIX.1 specifies
437 .BR _POSIX_C_SOURCE ,
438 .BR _POSIX_SOURCE ,
439 and
440 .BR _XOPEN_SOURCE .
441 .B _XOPEN_SOURCE_EXTENDED
442 was specified by XPG4v2 (aka SUSv1).
443
444 .B _FILE_OFFSET_BITS
445 is not specified by any standard,
446 but is employed on some other implementations.
447
448 .BR _BSD_SOURCE ,
449 .BR _SVID_SOURCE ,
450 .BR _ATFILE_SOURCE ,
451 .BR _GNU_SOURCE ,
452 .BR _FORTIFY_SOURCE ,
453 .BR _REENTRANT ,
454 and
455 .B _THREAD_SAFE
456 are specific to Linux (glibc).
457 .SH NOTES
458 .I <features.h>
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.
464
465 According to which of the above feature test macros are defined,
466 .I <features.h>
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.,
470 .BR __USE_MISC ).
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.
474 .SH EXAMPLE
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:
480 .in +4n
481 .nf
482
483 $ \fBcc ftm.c\fP
484 $ \fB./a.out\fP
485 _POSIX_SOURCE defined
486 _POSIX_C_SOURCE defined: 200809L
487 _BSD_SOURCE defined
488 _SVID_SOURCE defined
489 _ATFILE_SOURCE defined
490 $ \fBcc -D_XOPEN_SOURCE=500 ftm.c\fP
491 $ \fB./a.out\fP
492 _POSIX_SOURCE defined
493 _POSIX_C_SOURCE defined: 199506L
494 _XOPEN_SOURCE defined: 500
495 $ \fBcc -D_GNU_SOURCE ftm.c\fP
496 $ \fB./a.out\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
503 _BSD_SOURCE defined
504 _SVID_SOURCE defined
505 _ATFILE_SOURCE defined
506 _GNU_SOURCE defined
507 .fi
508 .in
509 .SS Program source
510 \&
511 .nf
512 /* ftm.c */
513
514 #include <stdio.h>
515 #include <unistd.h>
516 #include <stdlib.h>
517
518 int
519 main(int argc, char *argv[])
520 {
521 #ifdef _POSIX_SOURCE
522     printf("_POSIX_SOURCE defined\\n");
523 #endif
524
525 #ifdef _POSIX_C_SOURCE
526     printf("_POSIX_C_SOURCE defined: %ldL\\n", (long) _POSIX_C_SOURCE);
527 #endif
528
529 #ifdef _ISOC99_SOURCE
530     printf("_ISOC99_SOURCE defined\\n");
531 #endif
532
533 #ifdef _XOPEN_SOURCE
534     printf("_XOPEN_SOURCE defined: %d\\n", _XOPEN_SOURCE);
535 #endif
536
537 #ifdef _XOPEN_SOURCE_EXTENDED
538     printf("_XOPEN_SOURCE_EXTENDED defined\\n");
539 #endif
540
541 #ifdef _LARGEFILE64_SOURCE
542     printf("_LARGEFILE64_SOURCE defined\\n");
543 #endif
544
545 #ifdef _FILE_OFFSET_BITS
546     printf("_FILE_OFFSET_BITS defined: %d\\n", _FILE_OFFSET_BITS);
547 #endif
548
549 #ifdef _BSD_SOURCE
550     printf("_BSD_SOURCE defined\\n");
551 #endif
552
553 #ifdef _SVID_SOURCE
554     printf("_SVID_SOURCE defined\\n");
555 #endif
556
557 #ifdef _ATFILE_SOURCE
558     printf("_ATFILE_SOURCE defined\\n");
559 #endif
560
561 #ifdef _GNU_SOURCE
562     printf("_GNU_SOURCE defined\\n");
563 #endif
564
565 #ifdef _REENTRANT
566     printf("_REENTRANT defined\\n");
567 #endif
568
569 #ifdef _THREAD_SAFE
570     printf("_THREAD_SAFE defined\\n");
571 #endif
572
573 #ifdef _FORTIFY_SOURCE
574     printf("_FORTIFY_SOURCE defined\\n");
575 #endif
576
577     exit(EXIT_SUCCESS);
578 }
579 .fi
580 .SH SEE ALSO
581 .BR libc (7),
582 .BR standards (7)
583 .sp
584 The section "Feature Test Macros" under
585 .IR "info libc" .
586 .\" But beware: the info libc document is out of date (Jul 07, mtk)
587 .sp
588 .I /usr/include/features.h