]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/feature_test_macros.7
proc.5: Add "um" and "uw" to VmFlags in /proc/[pid]/smaps
[thirdparty/man-pages.git] / man7 / feature_test_macros.7
CommitLineData
55b726d1
MK
1.\" This manpage is Copyright (C) 2006, Michael Kerrisk
2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
55b726d1
MK
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.
c13182ef 12.\"
55b726d1
MK
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.
c13182ef 20.\"
55b726d1
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
55b726d1 24.\"
e8426ca2 25.TH FEATURE_TEST_MACROS 7 2020-04-11 "Linux" "Linux Programmer's Manual"
55b726d1 26.SH NAME
a8e7c990 27feature_test_macros \- feature test macros
55b726d1
MK
28.SH DESCRIPTION
29Feature test macros allow the programmer to control the definitions that
30are exposed by system header files when a program is compiled.
5711c04f 31.PP
e0530cbb
MK
32.B NOTE:
33In order to be effective, a feature test macro
34.IR "must be defined before including any header files" .
b220f1b5 35This can be done either in the compilation command
e0530cbb
MK
36.RI ( "cc \-DMACRO=value" )
37or by defining the macro within the source code before
38including any headers.
1be4da28
MK
39The requirement that the macro must be defined before including any
40header file exists because header files may freely include one another.
41Thus, for example, in the following lines, defining the
42.B _GNU_SOURCE
43macro may have no effect because the header
44.I <abc.h>
45itself 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
5711c04f 56.PP
f8b21213 57Some feature test macros are useful for creating portable applications,
c8f2dd47
MK
58by preventing nonstandard definitions from being exposed.
59Other macros can be used to expose nonstandard definitions that
55b726d1 60are not exposed by default.
5711c04f 61.PP
c13182ef 62The precise effects of each of the feature test macros described below
55b726d1
MK
63can be ascertained by inspecting the
64.I <features.h>
65header file.
6e558a81
MK
66.BR Note :
67applications do
68.I not
69need to directly include
70.IR <features.h> ;
71indeed, doing so is actively discouraged.
72See NOTES.
18409fb8
MK
73.SS Specification of feature test macro requirements in manual pages
74When a function requires that a feature test macro is defined,
922cf8f6 75the manual page SYNOPSIS typically includes a note of the following form
18409fb8 76(this example from the
cab82d65 77.BR acct (2)
e9600b20 78manual page):
6545cc56 79.PP
cab82d65 80.RS 8
cab82d65 81.B #include <unistd.h>
6545cc56 82.PP
cab82d65 83.BI "int acct(const char *" filename );
6545cc56 84.PP
b9c93deb 85.EX
18409fb8
MK
86.in -4n
87Feature Test Macro Requirements for glibc (see
88.BR feature_test_macros (7)):
b8302363 89.EE
18409fb8 90.in
6545cc56 91.PP
cab82d65
MK
92.BR acct ():
93_BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500)
18409fb8
MK
94.RE
95.PP
aeb9b6a6
MK
96The
97.B ||
98means that in order to obtain the declaration of
cab82d65 99.BR acct (2)
18409fb8 100from
cab82d65 101.IR <unistd.h> ,
aeb9b6a6
MK
102.I either
103of the following macro
18409fb8 104definitions must be made before including any header files:
9c40f2b9
MK
105.PP
106.in +4n
107.EX
922cf8f6 108#define _BSD_SOURCE
cab82d65 109#define _XOPEN_SOURCE /* or any value < 500 */
9c40f2b9
MK
110.EE
111.in
18409fb8
MK
112.PP
113Alternatively, equivalent definitions can be included in the
114compilation command:
408731d4
MK
115.PP
116.in +4n
117.EX
5b8dbfd4 118cc \-D_BSD_SOURCE
cab82d65 119cc \-D_XOPEN_SOURCE # Or any value < 500
408731d4
MK
120.EE
121.in
18409fb8 122.PP
922cf8f6 123Note that, as described below,
7f9a8f08 124.BR "some feature test macros are defined by default" ,
922cf8f6 125so that it may not always be necessary to
e9600b20
MK
126explicitly specify the feature test macro(s) shown in the
127SYNOPSIS.
5711c04f 128.PP
e9600b20
MK
129In a few cases, manual pages use a shorthand for expressing the
130feature test macro requirements (this example from
131.BR readahead (2)):
6545cc56 132.PP
408731d4
MK
133.in +4n
134.EX
e9600b20
MK
135.B #define _GNU_SOURCE
136.B #include <fcntl.h>
6545cc56 137.PP
e9600b20 138.BI "ssize_t readahead(int " fd ", off64_t *" offset ", size_t " count );
408731d4
MK
139.EE
140.in
e9600b20 141.PP
e0bf9127 142This format is employed in cases where only a single
e9600b20
MK
143feature test macro can be used to expose the function
144declaration, and that macro is not defined by default.
18409fb8 145.SS Feature test macros understood by glibc
f125c769 146The paragraphs below explain how feature test macros are handled
aeb9b6a6
MK
147in Linux glibc 2.\fIx\fP,
148.I x
149> 0.
5711c04f 150.PP
f125c769
MK
151First, though a summary of a few details for the impatient:
152.IP * 3
153The 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
1d229d9f 157(for definitions from various versions of SUS),
f125c769
MK
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 *
38d9e98e 163Certain macros are defined with default values.
2c767761 164Thus, although one or more macros may be indicated as being
38d9e98e
MK
165required in the SYNOPSIS of a man page,
166it may not be necessary to define them explicitly.
167Full details of the defaults are given later in this man page.
168.IP *
f125c769
MK
169Defining
170.BR _XOPEN_SOURCE
171with a value of 600 or greater produces the same effects as defining
172.BR _POSIX_C_SOURCE
173with a value of 200112L or greater.
174Where one sees
408731d4
MK
175.IP
176.in +4n
177.EX
178_POSIX_C_SOURCE >= 200112L
179.EE
180.in
f125c769
MK
181.IP
182in the feature test macro requirements in the SYNOPSIS of a man page,
183it is implicit that the following has the same effect:
408731d4
MK
184.IP
185.in +4n
186.EX
187_XOPEN_SOURCE >= 600
188.EE
189.in
f125c769
MK
190.IP *
191Defining
192.BR _XOPEN_SOURCE
193with a value of 700 or greater produces the same effects as defining
194.BR _POSIX_C_SOURCE
195with a value of 200809L or greater.
196Where one sees
408731d4
MK
197.IP
198.in +4n
199.EX
200_POSIX_C_SOURCE >= 200809L
201.EE
202.in
f125c769
MK
203.IP
204in the feature test macro requirements in the SYNOPSIS of a man page,
205it is implicit that the following has the same effect:
408731d4
MK
206.IP
207.in +4n
208.EX
209_XOPEN_SOURCE >= 700
210.EE
211.in
3efa27b5 212.\" The details in glibc 2.0 are simpler, but combining a
93d5a2a1
MK
213.\" a description of them with the details in later glibc versions
214.\" would make for a complicated description.
5711c04f 215.PP
93d5a2a1 216Linux glibc understands the following feature test macros:
0019177e 217.TP
4769db24
MK
218.B __STRICT_ANSI__
219ISO Standard C.
220This macro is implicitly defined by
221.BR gcc (1)
222when invoked with, for example, the
223.I -std=c99
224or
225.I -ansi
226flag.
55b726d1
MK
227.TP
228.B _POSIX_C_SOURCE
922cf8f6
MK
229Defining this macro causes header files to expose definitions as follows:
230.RS
231.IP \(bu 3
232The value 1 exposes definitions conforming to POSIX.1-1990 and
233ISO C (1990).
234.IP \(bu
235The value 2 or greater additionally exposes
18409fb8 236definitions for POSIX.2-1992.
922cf8f6
MK
237.IP \(bu
238The value 199309L or greater additionally exposes
55b726d1 239definitions for POSIX.1b (real-time extensions).
33a0ccb2 240.\" 199506L functionality is available only since glibc 2.1
922cf8f6
MK
241.IP \(bu
242The value 199506L or greater additionally exposes
55b726d1 243definitions for POSIX.1c (threads).
922cf8f6
MK
244.IP \(bu
245(Since glibc 2.3.3)
b694081a 246The value 200112L or greater additionally exposes definitions corresponding
f04d68ba
MK
247to the POSIX.1-2001 base specification (excluding the XSI extension).
248This value also causes C95 (since glibc 2.12) and
249C99 (since glibc 2.10) features to be exposed
250(in other words, the equivalent of defining
251.BR _ISOC99_SOURCE ).
6e047f16
MK
252.IP \(bu
253(Since glibc 2.10)
b694081a 254The value 200809L or greater additionally exposes definitions corresponding
6e047f16 255to the POSIX.1-2008 base specification (excluding the XSI extension).
922cf8f6 256.RE
c13182ef 257.TP
bcb564f2 258.B _POSIX_SOURCE
55b726d1
MK
259Defining this obsolete macro with any value is equivalent to defining
260.B _POSIX_C_SOURCE
261with the value 1.
5711c04f 262.IP
c1016e87
MK
263Since this macro is obsolete,
264its usage is generally not documented when discussing
265feature test macro requirements in the man pages.
55b726d1
MK
266.TP
267.B _XOPEN_SOURCE
922cf8f6
MK
268Defining this macro causes header files to expose definitions as follows:
269.RS
270.IP \(bu 3
271Defining with any value exposes
55b726d1 272definitions conforming to POSIX.1, POSIX.2, and XPG4.
922cf8f6
MK
273.IP \(bu
274The value 500 or greater additionally exposes
55b726d1 275definitions for SUSv2 (UNIX 98).
922cf8f6
MK
276.IP \(bu
277(Since glibc 2.2) The value 600 or greater additionally exposes
c13182ef 278definitions for SUSv3 (UNIX 03; i.e., the POSIX.1-2001 base specification
5709731e 279plus the XSI extension) and C99 definitions.
6e047f16
MK
280.IP \(bu
281(Since glibc 2.10) The value 700 or greater additionally exposes
282definitions for SUSv4 (i.e., the POSIX.1-2008 base specification
283plus the XSI extension).
922cf8f6 284.RE
d4e80b4d
MK
285.IP
286If
287.B __STRICT_ANSI__
288is not defined, or
289.BR _XOPEN_SOURCE
290is defined with a value greater than or equal to 500
291.I and
292neither
293.B _POSIX_SOURCE
25ca8ac7 294nor
d4e80b4d
MK
295.B _POSIX_C_SOURCE
296is explicitly defined, then
297the following macros are implicitly defined:
298.RS
299.IP \(bu 3
300.B _POSIX_SOURCE
301is defined with the value 1.
302.IP \(bu
303.B _POSIX_C_SOURCE
304is defined, according to the value of
305.BR _XOPEN_SOURCE :
a62ca28a 306.RS
d4e80b4d
MK
307.TP
308.BR _XOPEN_SOURCE " < 500"
309.B _POSIX_C_SOURCE
310is defined with the value 2.
311.TP
312.RB "500 <= " _XOPEN_SOURCE " < 600"
313.B _POSIX_C_SOURCE
314is defined with the value 199506L.
315.TP
316.RB "600 <= " _XOPEN_SOURCE " < 700"
317.B _POSIX_C_SOURCE
318is defined with the value 200112L.
319.TP
320.RB "700 <= " _XOPEN_SOURCE " (since glibc 2.10)"
321.B _POSIX_C_SOURCE
322is defined with the value 200809L.
323.RE
324.RE
a7d7000f
MK
325.IP
326In addition, defining
327.BR _XOPEN_SOURCE
328with a value of 500 or greater produces the same effects as defining
329.BR _XOPEN_SOURCE_EXTENDED .
55b726d1
MK
330.TP
331.B _XOPEN_SOURCE_EXTENDED
49d2e094
MK
332If this macro is defined,
333.I and
0daa9e92 334.B _XOPEN_SOURCE
55b726d1 335is defined, then expose definitions corresponding to the XPG4v2
18409fb8 336(SUSv1) UNIX extensions (UNIX 95).
a7d7000f 337Defining
18409fb8 338.B _XOPEN_SOURCE
a7d7000f
MK
339with a value of 500 or more also produces the same effect as defining
340.BR _XOPEN_SOURCE_EXTENDED .
d1473a83
MK
341Use of
342.BR _XOPEN_SOURCE_EXTENDED
343in new source code should be avoided.
5711c04f 344.IP
1e92200f
MK
345Since defining
346.B _XOPEN_SOURCE
347with a value of 500 or more has the same effect as defining
348.BR _XOPEN_SOURCE_EXTENDED ,
349the latter (obsolete) feature test macro is generally not described in the
350SYNOPSIS in man pages.
55b726d1 351.TP
df553d91 352.BR _ISOC99_SOURCE " (since glibc 2.1.3)"
72807508 353Exposes declarations consistent with the ISO C99 standard.
5711c04f 354.IP
df553d91 355Earlier glibc 2.1.x versions recognized an equivalent macro named
a4de892a
MK
356.B _ISOC9X_SOURCE
357(because the C99 standard had not then been finalized).
df553d91 358Although the use of this macro is obsolete, glibc continues
5fab2e7c 359to recognize it for backward compatibility.
5711c04f 360.IP
cfba52fc
MK
361Defining
362.B _ISOC99_SOURCE
363also exposes ISO C (1990) Amendment 1 ("C95") definitions.
364(The primary change in C95 was support for international character sets.)
5711c04f 365.IP
50d844a9
MK
366Invoking the C compiler with the option
367.IR \-std=c99
368produces the same effects as defining this macro.
55b726d1 369.TP
d6ced64f 370.BR _ISOC11_SOURCE " (since glibc 2.16)"
ea22a037 371Exposes declarations consistent with the ISO C11 standard.
8effdb2d
MK
372Defining this macro also enables C99 and C95 features (like
373.BR _ISOC99_SOURCE ).
5711c04f 374.IP
50d844a9
MK
375Invoking the C compiler with the option
376.IR \-std=c11
377produces the same effects as defining this macro.
ea22a037 378.TP
55b726d1 379.B _LARGEFILE64_SOURCE
c13182ef
MK
380Expose definitions for the alternative API specified by the
381LFS (Large File Summit) as a "transitional extension" to the
382Single UNIX Specification.
608bf950
SK
383(See
384.UR http:\:/\:/opengroup.org\:/platform\:/lfs.html
034a3f2f 385.UE .)
18409fb8
MK
386The 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
390versus
391.IR off_t ,
392.BR lseek64 ()
393versus
394.BR lseek (),
395etc.).
a007cc29 396New programs should not employ this macro; instead
0daa9e92 397.I _FILE_OFFSET_BITS=64
18409fb8 398should be employed.
55b726d1 399.TP
d84dea22
MK
400.BR _LARGEFILE_SOURCE
401This macro was historically used to expose certain functions (specifically
402.BR fseeko (3)
403and
404.BR ftello (3))
405that address limitations of earlier APIs
0c12fe8f 406.RB ( fseek (3)
d84dea22
MK
407and
408.BR ftell (3))
409that use
410.IR "long int"
411for file offsets.
412This macro is implicitly defined if
413.BR _XOPEN_SOURCE
414is defined with a value greater than or equal to 500.
415New programs should not employ this macro;
416defining
417.BR _XOPEN_SOURCE
418as just described or defining
419.B _FILE_OFFSET_BITS
420with the value 64 is the preferred mechanism to achieve the same result.
421.TP
55b726d1 422.B _FILE_OFFSET_BITS
c13182ef
MK
423Defining this macro with the value 64
424automatically converts references to 32-bit functions and data types
9ee4a2b6 425related to file I/O and filesystem operations into references to
55b726d1
MK
426their 64-bit counterparts.
427This is useful for performing I/O on large files (> 2 Gigabytes)
428on 32-bit systems.
18409fb8
MK
429(Defining this macro permits correctly written programs to use
430large files with only a recompilation being required.)
5711c04f 431.IP
18409fb8
MK
43264-bit systems naturally permit file sizes greater than 2 Gigabytes,
433and on those systems this macro has no effect.
55b726d1 434.TP
d6472258 435.BR _BSD_SOURCE " (deprecated since glibc 2.20)"
93d5a2a1 436Defining this macro with any value causes header files to expose
55b726d1 437BSD-derived definitions.
5711c04f 438.IP
0385c88e
MK
439In glibc versions up to and including 2.18,
440defining this macro also causes BSD definitions to be preferred in
18409fb8
MK
441some 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 ,
447or
0daa9e92 448.B _GNU_SOURCE
18409fb8 449is defined, in which case BSD definitions are disfavored.
0385c88e
MK
450Since glibc 2.19,
451.B _BSD_SOURCE
452no longer causes BSD definitions to be preferred in case of conflicts.
5711c04f 453.IP
d6472258
MK
454Since glibc 2.20, this macro is deprecated.
455.\" commit c941736c92fa3a319221f65f6755659b2a5e0a20
456.\" commit 498afc54dfee41d33ba519f496e96480badace8e
457.\" commit acd7f096d79c181866d56d4aaf3b043e741f1e2c
458It now has the same effect as defining
459.BR _DEFAULT_SOURCE ,
460but generates a compile-time warning (unless
461.BR _DEFAULT_SOURCE
462.\" commit ade40b10ff5fa59a318cf55b9d8414b758e8df78
463is also defined).
464Use
465.B _DEFAULT_SOURCE
466instead.
da6aad02
MK
467To allow code that requires
468.BR _BSD_SOURCE
469in glibc 2.19 and earlier and
470.BR _DEFAULT_SOURCE
471in glibc 2.20 and later to compile without warnings, define
472.I both
473.B _BSD_SOURCE
474and
475.BR _DEFAULT_SOURCE .
55b726d1 476.TP
d6472258 477.BR _SVID_SOURCE " (deprecated since glibc 2.20)"
93d5a2a1 478Defining this macro with any value causes header files to expose
c13182ef 479System V-derived definitions.
4dec66f9
MK
480(SVID == System V Interface Definition; see
481.BR standards (7).)
5711c04f 482.IP
d6472258
MK
483Since glibc 2.20, this macro is deprecated in the same fashion as
484.BR _BSD_SOURCE .
55b726d1 485.TP
ba694dd0 486.BR _DEFAULT_SOURCE " (since glibc 2.19)"
ba694dd0
MK
487This macro can be defined to ensure that the "default"
488definitions are provided even when the defaults would otherwise
489be disabled,
490as happens when individual macros are explicitly defined,
491or the compiler is invoked in one of its "standard" modes (e.g.,
492.IR "cc\ \-std=c99" ).
dfb1232c
MK
493Defining
494.B _DEFAULT_SOURCE
495without defining other individual macros
496or invoking the compiler in one of its "standard" modes has no effect.
5711c04f 497.IP
f04d68ba
MK
498The "default" definitions comprise those required by POSIX.1-2008 and ISO C99,
499as well as various definitions originally derived from BSD and System V.
dfb1232c
MK
500On glibc 2.19 and earlier, these defaults were approximately equivalent
501to explicitly defining the following:
5711c04f 502.IP
dfb1232c 503 cc \-D_BSD_SOURCE \-D_SVID_SOURCE \-D_POSIX_C_SOURCE=200809
ba694dd0 504.TP
a4de892a 505.BR _ATFILE_SOURCE " (since glibc 2.4)"
93d5a2a1 506Defining this macro with any value causes header files to expose
4769db24
MK
507declarations of a range of functions with the suffix "at";
508see
509.BR openat (2).
8a8c6284
MK
510Since glibc 2.10, this macro is also implicitly defined if
511.BR _POSIX_C_SOURCE
512is defined with a value greater than or equal to 200809L.
4769db24 513.TP
55b726d1 514.B _GNU_SOURCE
08d69565 515Defining this macro (with any value) implicitly defines
4769db24 516.BR _ATFILE_SOURCE ,
55b726d1 517.BR _LARGEFILE64_SOURCE ,
4769db24
MK
518.BR _ISOC99_SOURCE ,
519.BR _XOPEN_SOURCE_EXTENDED ,
520.BR _POSIX_SOURCE ,
0daa9e92 521.B _POSIX_C_SOURCE
6e047f16
MK
522with the value 200809L
523(200112L in glibc versions before 2.10;
524199506L in glibc versions before 2.5;
525199309L in glibc versions before 2.1)
922cf8f6 526and
0daa9e92 527.B _XOPEN_SOURCE
6e047f16
MK
528with the value 700
529(600 in glibc versions before 2.10;
530500 in glibc versions before 2.2).
55b726d1 531In addition, various GNU-specific extensions are also exposed.
5711c04f 532.IP
dfb1232c
MK
533Since glibc 2.19, defining
534.BR _GNU_SOURCE
08d69565 535also has the effect of implicitly defining
dfb1232c
MK
536.BR _DEFAULT_SOURCE .
537In glibc versions before 2.20, defining
538.BR _GNU_SOURCE
08d69565 539also had the effect of implicitly defining
dfb1232c
MK
540.BR _BSD_SOURCE
541and
542.BR _SVID_SOURCE .
d3bbf18d
MK
543.TP
544.B _REENTRANT
03fb4543
MK
545Historically, on various C libraries
546it was necessary to define this macro in all
f74991be 547multithreaded code.
03fb4543
MK
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.
f74991be
MK
552(Some C libraries may still require this.)
553In glibc,
554this macro also exposed definitions of certain reentrant functions.
5711c04f 555.IP
f74991be
MK
556However, glibc has been thread-safe by default for many years;
557since glibc 2.3, the only effect of defining
558.BR _REENTRANT
559has been to enable one or two of the same declarations that
560are also enabled by defining
75b554e7
MK
561.BR _POSIX_C_SOURCE
562with a value of 199606L or greater.
5711c04f 563.IP
f74991be 564.B _REENTRANT
03fb4543
MK
565is now obsolete.
566In glibc 2.25 and later, defining
567.B _REENTRANT
568is equivalent to defining
f74991be
MK
569.B _POSIX_C_SOURCE
570with the value 199606L.
571If a higher POSIX conformance level is
572selected by any other means (such as
573.B _POSIX_C_SOURCE
574itself,
575.BR _XOPEN_SOURCE ,
576.BR _DEFAULT_SOURCE ,
577or
578.BR _GNU_SOURCE ),
03fb4543 579then defining
f74991be
MK
580.B _REENTRANT
581has no effect.
5711c04f 582.IP
03fb4543 583This macro is automatically defined if one compiles with
26719dcf 584.IR "cc\ \-pthread" .
d3bbf18d
MK
585.TP
586.B _THREAD_SAFE
75b554e7 587Synonym for the (deprecated)
d3bbf18d
MK
588.BR _REENTRANT ,
589provided for compatibility with some other implementations.
590.TP
a4de892a 591.BR _FORTIFY_SOURCE " (since glibc 2.3.4)"
d3bbf18d
MK
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
598Defining this macro causes some lightweight checks to be performed
599to detect some buffer overflow errors when employing
29ca3cb0 600various string and memory manipulation functions (for example,
d3bbf18d 601.BR memcpy (3),
d3bbf18d
MK
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),
29ca3cb0
MK
612.BR gets (3),
613and wide character variants thereof).
614For some functions, argument consistency is checked;
615for example, a check is made that
616.BR open (2)
617has been supplied with a
618.I mode
619argument when the specified flags include
620.BR O_CREAT .
621Not all problems are detected, just some common cases.
622.\" Look for __USE_FORTIFY_LEVEL in the header files
5711c04f 623.IP
c13182ef
MK
624If
625.B _FORTIFY_SOURCE
626is set to 1, with compiler optimization level 1
5b8dbfd4 627.RI ( "gcc\ \-O1" )
d9bfdb9c 628and above, checks that shouldn't change the behavior of
d3bbf18d 629conforming programs are performed.
c13182ef
MK
630With
631.B _FORTIFY_SOURCE
29ca3cb0 632set to 2, some more checking is added, but
d3bbf18d 633some conforming programs might fail.
ef797056
MK
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
29c0586f 641.\" cause the following diagnostic at run time at the snprintf() call
ef797056
MK
642.\"
643.\" *** %n in writable segment detected ***
644.\" Aborted (core dumped)
645.\"
5711c04f 646.IP
29ca3cb0
MK
647Some of the checks can be performed at compile time
648(via macros logic implemented in header files),
c13182ef
MK
649and result in compiler warnings;
650other checks take place at run time,
d3bbf18d 651and result in a run-time error if the check fails.
5711c04f 652.IP
d3bbf18d
MK
653Use of this macro requires compiler support, available with
654.BR gcc (1)
655since version 4.0.
4769db24 656.SS Default definitions, implicit definitions, and combining definitions
9eaee631 657.PP
93d5a2a1
MK
658If no feature test macros are explicitly defined,
659then the following feature test macros are defined by default:
e9f22b0a 660.BR _BSD_SOURCE
d6472258 661(in glibc 2.19 and earlier),
e9f22b0a 662.BR _SVID_SOURCE
d6472258 663(in glibc 2.19 and earlier),
dfb1232c
MK
664.BR _DEFAULT_SOURCE
665(since glibc 2.19),
9eaee631
MK
666.BR _POSIX_SOURCE ,
667and