OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / 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 2009-12-13 "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 This can be useful for creating portable applications,
36 by preventing nonstandard definitions from being exposed.
37 Other macros can be used to expose nonstandard definitions that
38 are not exposed by default.
39 The precise effects of each of the feature test macros described below
40 can be ascertained by inspecting the
41 .I <features.h>
42 header file.
43
44 In order to be effective, a feature test macro
45 .IR "must be defined before including any header files" .
46 This can either be done in the compilation command
47 .RI ( "cc \-DMACRO=value" )
48 or by defining the macro within the source code before
49 including any headers.
50 .SS Specification of feature test macro requirements in manual pages
51 When a function requires that a feature test macro is defined,
52 the manual page SYNOPSIS typically includes a note of the following form
53 (this example from the
54 .BR chmod (2)
55 manual page):
56 .RS
57 .sp
58 .B #include <sys/stat.h>
59 .sp
60 .BI "int chmod(const char *" path ", mode_t " mode );
61 .br
62 .BI "int fchmod(int " fd ", mode_t " mode );
63 .sp
64 .in -4n
65 Feature Test Macro Requirements for glibc (see
66 .BR feature_test_macros (7)):
67 .in
68 .sp
69 .BR fchmod ():
70 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
71 .RE
72 .PP
73 The \fB||\fP means that in order to obtain the declaration of
74 .BR fchmod (2)
75 from
76 .IR <sys/stat.h> ,
77 \fIeither\fP of the following macro
78 definitions must be made before including any header files:
79 .RS
80 .nf
81
82 #define _BSD_SOURCE
83 #define _XOPEN_SOURCE 500     /* or any value > 500 */
84 .fi
85 .RE
86 .PP
87 Alternatively, equivalent definitions can be included in the
88 compilation command:
89 .RS
90 .nf
91
92 cc \-D_BSD_SOURCE
93 cc \-D_XOPEN_SOURCE=500        # Or any value > 500
94 .fi
95 .RE
96 .PP
97 Note that, as described below,
98 .BR "some feature test macros are defined by default" ,
99 so that it may not always be necessary to
100 explicitly specify the feature test macro(s) shown in the
101 SYNOPSIS.
102
103 In a few cases, manual pages use a shorthand for expressing the
104 feature test macro requirements (this example from
105 .BR readahead (2)):
106 .RS
107 .nf
108
109 .B #define _GNU_SOURCE
110 .B #include <fcntl.h>
111 .sp
112 .BI "ssize_t readahead(int " fd ", off64_t *" offset ", size_t " count );
113 .fi
114 .RE
115 .PP
116 This format is employed in cases where only a single
117 feature test macro can be used to expose the function
118 declaration, and that macro is not defined by default.
119 .SS Feature test macros understood by glibc
120 The following paragraphs explain how feature test macros are handled
121 in Linux glibc 2.\fIx\fP, \fIx\fP > 0.
122 .\" The details in glibc 2.0 are simpler, but combining a
123 .\" a description of them with the details in later glibc versions
124 .\" would make for a complicated description.
125
126 Linux glibc understands the following feature test macros:
127 .TP
128 .B __STRICT_ANSI__
129 ISO Standard C.
130 This macro is implicitly defined by
131 .BR gcc (1)
132 when invoked with, for example, the
133 .I -std=c99
134 or
135 .I -ansi
136 flag.
137 .TP
138 .B _POSIX_C_SOURCE
139 Defining this macro causes header files to expose definitions as follows:
140 .RS
141 .IP \(bu 3
142 The value 1 exposes definitions conforming to POSIX.1-1990 and
143 ISO C (1990).
144 .IP \(bu
145 The value 2 or greater additionally exposes
146 definitions for POSIX.2-1992.
147 .IP \(bu
148 The value 199309L or greater additionally exposes
149 definitions for POSIX.1b (real-time extensions).
150 .\" 199506L functionality is only available since glibc 2.1
151 .IP \(bu
152 The value 199506L or greater additionally exposes
153 definitions for POSIX.1c (threads).
154 .IP \(bu
155 (Since glibc 2.3.3)
156 The value 200112L or greater exposes definitions corresponding
157 to the POSIX.1-2001 base specification (excluding the XSI extension).
158 .IP \(bu
159 (Since glibc 2.10)
160 The value 200809L or greater exposes definitions corresponding
161 to the POSIX.1-2008 base specification (excluding the XSI extension).
162 .RE
163 .TP
164 .B _POSIX_SOURCE
165 Defining this obsolete macro with any value is equivalent to defining
166 .B _POSIX_C_SOURCE
167 with the value 1.
168 .TP
169 .B _XOPEN_SOURCE
170 Defining this macro causes header files to expose definitions as follows:
171 .RS
172 .IP \(bu 3
173 Defining with any value exposes
174 definitions conforming to POSIX.1, POSIX.2, and XPG4.
175 .IP \(bu
176 The value 500 or greater additionally exposes
177 definitions for SUSv2 (UNIX 98).
178 .IP \(bu
179 (Since glibc 2.2) The value 600 or greater additionally exposes
180 definitions for SUSv3 (UNIX 03; i.e., the POSIX.1-2001 base specification
181 plus the XSI extension) and C99 definitions.
182 .IP \(bu
183 (Since glibc 2.10) The value 700 or greater additionally exposes
184 definitions for SUSv4 (i.e., the POSIX.1-2008 base specification
185 plus the XSI extension).
186 .RE
187 .TP
188 .B _XOPEN_SOURCE_EXTENDED
189 If this macro is defined, and
190 .B _XOPEN_SOURCE
191 is defined, then expose definitions corresponding to the XPG4v2
192 (SUSv1) UNIX extensions (UNIX 95).
193 This macro is also implicitly defined if
194 .B _XOPEN_SOURCE
195 is defined with a value of 500 or more.
196 .TP
197 .B _ISOC99_SOURCE
198 Exposes C99 extensions to ISO C (1990).
199 This macro is recognized since glibc 2.1.3;
200 earlier glibc 2.1.x versions recognized an equivalent macro named
201 .B _ISOC9X_SOURCE
202 (because the C99 standard had not then been finalized).
203 Although the use of the latter macro is obsolete, glibc continues
204 to recognize it for backwards compatibility.
205 .TP
206 .B _LARGEFILE64_SOURCE
207 Expose definitions for the alternative API specified by the
208 LFS (Large File Summit) as a "transitional extension" to the
209 Single UNIX Specification.
210 (See http://opengroup.org/platform/lfs.html.)
211 The alternative API consists of a set of new objects
212 (i.e., functions and types) whose names are suffixed with "64"
213 (e.g.,
214 .I off64_t
215 versus
216 .IR off_t ,
217 .BR lseek64 ()
218 versus
219 .BR lseek (),
220 etc.).
221 New programs should not employ this interface; instead
222 .I _FILE_OFFSET_BITS=64
223 should be employed.
224 .TP
225 .B _FILE_OFFSET_BITS
226 Defining this macro with the value 64
227 automatically converts references to 32-bit functions and data types
228 related to file I/O and file system operations into references to
229 their 64-bit counterparts.
230 This is useful for performing I/O on large files (> 2 Gigabytes)
231 on 32-bit systems.
232 (Defining this macro permits correctly written programs to use
233 large files with only a recompilation being required.)
234 64-bit systems naturally permit file sizes greater than 2 Gigabytes,
235 and on those systems this macro has no effect.
236 .TP
237 .B _BSD_SOURCE
238 Defining this macro with any value causes header files to expose
239 BSD-derived definitions.
240 Defining this macro also causes BSD definitions to be preferred in
241 some situations where standards conflict, unless one or more of
242 .BR _SVID_SOURCE ,
243 .BR _POSIX_SOURCE ,
244 .BR _POSIX_C_SOURCE ,
245 .BR _XOPEN_SOURCE ,
246 .BR _XOPEN_SOURCE_EXTENDED ,
247 or
248 .B _GNU_SOURCE
249 is defined, in which case BSD definitions are disfavored.
250 .TP
251 .B _SVID_SOURCE
252 Defining this macro with any value causes header files to expose
253 System V-derived definitions.
254 (SVID == System V Interface Definition; see
255 .BR standards (7).)
256 .TP
257 .BR _ATFILE_SOURCE " (since glibc 2.4)"
258 Defining this macro with any value causes header files to expose
259 declarations of a range of functions with the suffix "at";
260 see
261 .BR openat (2).
262 Since glibc 2.10, this macro is also implicitly defined if
263 .BR _POSIX_C_SOURCE
264 is defined with a value greater than or equal to 200809L.
265 .TP
266 .B _GNU_SOURCE
267 Defining this macro (with any value) is equivalent to defining
268 .BR _BSD_SOURCE ,
269 .BR _SVID_SOURCE ,
270 .BR _ATFILE_SOURCE ,
271 .BR _LARGEFILE64_SOURCE ,
272 .BR _ISOC99_SOURCE ,
273 .BR _XOPEN_SOURCE_EXTENDED ,
274 .BR _POSIX_SOURCE ,
275 .B _POSIX_C_SOURCE
276 with the value 200809L
277 (200112L in glibc versions before 2.10;
278 199506L in glibc versions before 2.5;
279 199309L in glibc versions before 2.1)
280 and
281 .B _XOPEN_SOURCE
282 with the value 700
283 (600 in glibc versions before 2.10;
284 500 in glibc versions before 2.2).
285 In addition, various GNU-specific extensions are also exposed.
286 Where standards conflict, BSD definitions are disfavored.
287 .TP
288 .B _REENTRANT
289 Defining this macro exposes definitions of certain reentrant functions.
290 For multithreaded programs, use
291 .I "cc\ \-pthread"
292 instead.
293 .TP
294 .B _THREAD_SAFE
295 Synonym for
296 .BR _REENTRANT ,
297 provided for compatibility with some other implementations.
298 .TP
299 .BR _FORTIFY_SOURCE " (since glibc 2.3.4)"
300 .\" For more detail, see:
301 .\" http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
302 .\" [PATCH] Object size checking to prevent (some) buffer overflows
303 .\" * From: Jakub Jelinek <jakub at redhat dot com>
304 .\" * To: gcc-patches at gcc dot gnu dot org
305 .\" * Date: Tue, 21 Sep 2004 04:16:40 -0400
306 Defining this macro causes some lightweight checks to be performed
307 to detect some buffer overflow errors when employing
308 various string and memory manipulation functions.
309 Not all buffer overflows are detected, just some common cases.
310 In the current implementation checks are added for
311 calls to
312 .BR memcpy (3),
313 .BR mempcpy (3),
314 .BR memmove (3),
315 .BR memset (3),
316 .BR stpcpy (3),
317 .BR strcpy (3),
318 .BR strncpy (3),
319 .BR strcat (3),
320 .BR strncat (3),
321 .BR sprintf (3),
322 .BR snprintf (3),
323 .BR vsprintf (3),
324 .BR vsnprintf (3),
325 and
326 .BR gets (3).
327 If
328 .B _FORTIFY_SOURCE
329 is set to 1, with compiler optimization level 1
330 .RI ( "gcc\ \-O1" )
331 and above, checks that shouldn't change the behavior of
332 conforming programs are performed.
333 With
334 .B _FORTIFY_SOURCE
335 set to 2 some more checking is added, but
336 some conforming programs might fail.
337 Some of the checks can be performed at compile time,
338 and result in compiler warnings;
339 other checks take place at run time,
340 and result in a run-time error if the check fails.
341 Use of this macro requires compiler support, available with
342 .BR gcc (1)
343 since version 4.0.
344 .SS Default definitions, implicit definitions, and combining definitions
345 .PP
346 If no feature test macros are explicitly defined,
347 then the following feature test macros are defined by default:
348 .BR _BSD_SOURCE ,
349 .BR _SVID_SOURCE ,
350 .BR _POSIX_SOURCE ,
351 and
352 .BR _POSIX_C_SOURCE =200809L
353 (200112L in glibc versions before 2.10;
354 199506L in glibc versions before 2.4;
355 199309L in glibc versions before 2.1).
356 .PP
357 If any of
358 .BR __STRICT_ANSI__ ,
359 .BR _ISOC99_SOURCE ,
360 .BR _POSIX_SOURCE ,
361 .BR _POSIX_C_SOURCE  ,
362 .BR _XOPEN_SOURCE ,
363 .BR _XOPEN_SOURCE_EXTENDED ,
364 .BR _BSD_SOURCE ,
365 or
366 .B _SVID_SOURCE
367 is explicitly defined, then
368 .BR _BSD_SOURCE ,
369 and
370 .B _SVID_SOURCE
371 are not defined by default.
372
373 If
374 .B _POSIX_SOURCE
375 and
376 .B _POSIX_C_SOURCE
377 are not explicitly defined,
378 and either
379 .B __STRICT_ANSI__
380 is not defined or
381 .B _XOPEN_SOURCE
382 is defined with a value of 500 or more, then
383 .RS 3
384 .IP * 3
385 .B _POSIX_SOURCE
386 is defined with the value 1; and
387 .IP *
388 .B _POSIX_C_SOURCE
389 is defined with one of the following values:
390 .RS 6
391 .IP \(bu 3
392 2,
393 if
394 .B XOPEN_SOURCE
395 is defined with a value less than 500;
396 .IP \(bu
397 199506L,
398 if
399 .B XOPEN_SOURCE
400 is defined with a value greater than or equal to 500 and less than 600;
401 or
402 .IP \(bu
403 (since glibc 2.4) 200112L,
404 if
405 .B XOPEN_SOURCE
406 is defined with a value greater than or equal to 600 and less than 700.
407 .IP \(bu
408 (Since glibc 2.10)
409 200809L,
410 if
411 .B XOPEN_SOURCE
412 is defined with a value greater than or equal to 700.
413 .IP \(bu
414 Older versions of glibc do not know about the values
415 200112L and 200809L for
416 .BR _POSIX_C_SOURCE ,
417 and the setting of this macro will depend on the glibc version.
418 .IP \(bu
419 If
420 .B _XOPEN_SOURCE
421 is undefined, then the setting of
422 .B _POSIX_C_SOURCE
423 depends on the glibc version:
424 199506L, in glibc versions before 2.4;
425 200112L, in glibc 2.4 to 2.9; and
426 200809L, since glibc 2.10.
427 .RE
428 .RE
429 .PP
430 Multiple macros can be defined; the results are additive.
431 .SH CONFORMING TO
432 POSIX.1 specifies
433 .BR _POSIX_C_SOURCE ,
434 .BR _POSIX_SOURCE ,
435 and
436 .BR _XOPEN_SOURCE .
437 .B _XOPEN_SOURCE_EXTENDED
438 was specified by XPG4v2 (aka SUSv1).
439
440 .B _FILE_OFFSET_BITS
441 is not specified by any standard,
442 but is employed on some other implementations.
443
444 .BR _BSD_SOURCE ,
445 .BR _SVID_SOURCE ,
446 .BR _ATFILE_SOURCE ,
447 .BR _GNU_SOURCE ,
448 .BR _FORTIFY_SOURCE ,
449 .BR _REENTRANT ,
450 and
451 .B _THREAD_SAFE
452 are specific to Linux (glibc).
453 .SH NOTES
454 .I <features.h>
455 is a Linux/glibc-specific header file.
456 Other systems have an analogous file, but typically with a different name.
457 This header file is automatically included by other header files as
458 required: it is not necessary to explicitly include it in order to
459 employ feature test macros.
460
461 According to which of the above feature test macros are defined,
462 .I <features.h>
463 internally defines various other macros that are checked by
464 other glibc header files.
465 These macros have names prefixed by two underscores (e.g.,
466 .BR __USE_MISC ).
467 Programs should \fInever\fP define these macros directly:
468 instead, the appropriate feature test macro(s) from the
469 list above should be employed.
470 .SH EXAMPLE
471 The program below can be used to explore how the various
472 feature test macros are set depending on the glibc version
473 and what feature test macros are explicitly set.
474 The following shell session, on a system with glibc 2.10,
475 shows some examples of what we would see:
476 .in +4n
477 .nf
478
479 $ \fBcc ftm.c\fP
480 $ \fB./a.out\fP
481 _POSIX_SOURCE defined
482 _POSIX_C_SOURCE defined: 200809L
483 _BSD_SOURCE defined
484 _SVID_SOURCE defined
485 _ATFILE_SOURCE defined
486 $ \fBcc -D_XOPEN_SOURCE=500 ftm.c\fP
487 $ \fB./a.out\fP
488 _POSIX_SOURCE defined
489 _POSIX_C_SOURCE defined: 199506L
490 _XOPEN_SOURCE defined: 500
491 $ \fBcc -D_GNU_SOURCE ftm.c\fP
492 $ \fB./a.out\fP
493 _POSIX_SOURCE defined
494 _POSIX_C_SOURCE defined: 200809L
495 _ISOC99_SOURCE defined
496 _XOPEN_SOURCE defined: 700
497 _XOPEN_SOURCE_EXTENDED defined
498 _LARGEFILE64_SOURCE defined
499 _BSD_SOURCE defined
500 _SVID_SOURCE defined
501 _ATFILE_SOURCE defined
502 _GNU_SOURCE defined
503 .fi
504 .in
505 .SS Program source
506 \&
507 .nf
508 /* ftm.c */
509
510 #include <stdio.h>
511 #include <unistd.h>
512 #include <stdlib.h>
513
514 int
515 main(int argc, char *argv[])
516 {
517 #ifdef _POSIX_SOURCE
518     printf("_POSIX_SOURCE defined\\n");
519 #endif
520
521 #ifdef _POSIX_C_SOURCE
522     printf("_POSIX_C_SOURCE defined: %ldL\\n", (long) _POSIX_C_SOURCE);
523 #endif
524
525 #ifdef _ISOC99_SOURCE
526     printf("_ISOC99_SOURCE defined\\n");
527 #endif
528
529 #ifdef _XOPEN_SOURCE
530     printf("_XOPEN_SOURCE defined: %d\\n", _XOPEN_SOURCE);
531 #endif
532
533 #ifdef _XOPEN_SOURCE_EXTENDED
534     printf("_XOPEN_SOURCE_EXTENDED defined\\n");
535 #endif
536
537 #ifdef _LARGEFILE64_SOURCE
538     printf("_LARGEFILE64_SOURCE defined\\n");
539 #endif
540
541 #ifdef _FILE_OFFSET_BITS
542     printf("_FILE_OFFSET_BITS defined: %d\\n", _FILE_OFFSET_BITS);
543 #endif
544
545 #ifdef _BSD_SOURCE
546     printf("_BSD_SOURCE defined\\n");
547 #endif
548
549 #ifdef _SVID_SOURCE
550     printf("_SVID_SOURCE defined\\n");
551 #endif
552
553 #ifdef _ATFILE_SOURCE
554     printf("_ATFILE_SOURCE defined\\n");
555 #endif
556
557 #ifdef _GNU_SOURCE
558     printf("_GNU_SOURCE defined\\n");
559 #endif
560
561 #ifdef _REENTRANT
562     printf("_REENTRANT defined\\n");
563 #endif
564
565 #ifdef _THREAD_SAFE
566     printf("_THREAD_SAFE defined\\n");
567 #endif
568
569 #ifdef _FORTIFY_SOURCE
570     printf("_FORTIFY_SOURCE defined\\n");
571 #endif
572
573     exit(EXIT_SUCCESS);
574 }
575 .fi
576 .SH SEE ALSO
577 .BR libc (7),
578 .BR standards (7)
579 .sp
580 The section "Feature Test Macros" under
581 .IR "info libc" .
582 .\" But beware: the info libc document is out of date (Jul 07, mtk)
583 .sp
584 .I /usr/include/features.h