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