1FEATURE_TEST_MACROS(7) Linux Programmer's Manual FEATURE_TEST_MACROS(7)
2
3
4
6 feature_test_macros - feature test macros
7
9 #include <features.h>
10
12 Feature test macros allow the programmer to control the definitions
13 that are exposed by system header files when a program is compiled.
14
15 NOTE: In order to be effective, a feature test macro must be defined
16 before including any header files. This can be done either in the com‐
17 pilation command (cc -DMACRO=value) or by defining the macro within the
18 source code before including any headers.
19
20 Some feature test macros are useful for creating portable applications,
21 by preventing nonstandard definitions from being exposed. Other macros
22 can be used to expose nonstandard definitions that are not exposed by
23 default. The precise effects of each of the feature test macros
24 described below can be ascertained by inspecting the <features.h>
25 header file.
26
27 Specification of feature test macro requirements in manual pages
28 When a function requires that a feature test macro is defined, the man‐
29 ual page SYNOPSIS typically includes a note of the following form (this
30 example from the acct(2) manual page):
31
32 #include <unistd.h>
33
34 int acct(const char *filename);
35
36 Feature Test Macro Requirements for glibc (see
37 feature_test_macros(7)):
38
39 acct(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
40
41 The || means that in order to obtain the declaration of acct(2) from
42 <unistd.h>, either of the following macro definitions must be made
43 before including any header files:
44
45 #define _BSD_SOURCE
46 #define _XOPEN_SOURCE /* or any value < 500 */
47
48 Alternatively, equivalent definitions can be included in the compila‐
49 tion command:
50
51 cc -D_BSD_SOURCE
52 cc -D_XOPEN_SOURCE # Or any value < 500
53
54 Note that, as described below, some feature test macros are defined by
55 default, so that it may not always be necessary to explicitly specify
56 the feature test macro(s) shown in the SYNOPSIS.
57
58 In a few cases, manual pages use a shorthand for expressing the feature
59 test macro requirements (this example from readahead(2)):
60
61 #define _GNU_SOURCE
62 #include <fcntl.h>
63
64 ssize_t readahead(int fd, off64_t *offset, size_t count);
65
66 This format is employed in cases where only a single feature test macro
67 can be used to expose the function declaration, and that macro is not
68 defined by default.
69
70 Feature test macros understood by glibc
71 The following paragraphs explain how feature test macros are handled in
72 Linux glibc 2.x, x > 0.
73
74 Linux glibc understands the following feature test macros:
75
76 __STRICT_ANSI__
77 ISO Standard C. This macro is implicitly defined by gcc(1) when
78 invoked with, for example, the -std=c99 or -ansi flag.
79
80 _POSIX_C_SOURCE
81 Defining this macro causes header files to expose definitions as
82 follows:
83
84 · The value 1 exposes definitions conforming to POSIX.1-1990
85 and ISO C (1990).
86
87 · The value 2 or greater additionally exposes definitions for
88 POSIX.2-1992.
89
90 · The value 199309L or greater additionally exposes definitions
91 for POSIX.1b (real-time extensions).
92
93 · The value 199506L or greater additionally exposes definitions
94 for POSIX.1c (threads).
95
96 · (Since glibc 2.3.3) The value 200112L or greater exposes def‐
97 initions corresponding to the POSIX.1-2001 base specification
98 (excluding the XSI extension).
99
100 · (Since glibc 2.10) The value 200809L or greater exposes defi‐
101 nitions corresponding to the POSIX.1-2008 base specification
102 (excluding the XSI extension).
103
104 _POSIX_SOURCE
105 Defining this obsolete macro with any value is equivalent to
106 defining _POSIX_C_SOURCE with the value 1.
107
108 _XOPEN_SOURCE
109 Defining this macro causes header files to expose definitions as
110 follows:
111
112 · Defining with any value exposes definitions conforming to
113 POSIX.1, POSIX.2, and XPG4.
114
115 · The value 500 or greater additionally exposes definitions for
116 SUSv2 (UNIX 98).
117
118 · (Since glibc 2.2) The value 600 or greater additionally
119 exposes definitions for SUSv3 (UNIX 03; i.e., the
120 POSIX.1-2001 base specification plus the XSI extension) and
121 C99 definitions.
122
123 · (Since glibc 2.10) The value 700 or greater additionally
124 exposes definitions for SUSv4 (i.e., the POSIX.1-2008 base
125 specification plus the XSI extension).
126
127 _XOPEN_SOURCE_EXTENDED
128 If this macro is defined, and _XOPEN_SOURCE is defined, then
129 expose definitions corresponding to the XPG4v2 (SUSv1) UNIX
130 extensions (UNIX 95). This macro is also implicitly defined if
131 _XOPEN_SOURCE is defined with a value of 500 or more.
132
133 _ISOC95_SOURCE
134 Exposes ISO C (1990) Amendment 1 definitions (also known as
135 C95). This macro is recognized since glibc 2.12. The primary
136 change in C95 was support for international character sets. The
137 C95 changes were included in the subsequent C99 standard (in
138 other words, _ISOC99_SOURCE implies _ISOC95_SOURCE).
139
140 _ISOC99_SOURCE
141 Exposes C99 extensions to ISO C (1990). This macro is recog‐
142 nized since glibc 2.1.3; earlier glibc 2.1.x versions recognized
143 an equivalent macro named _ISOC9X_SOURCE (because the C99 stan‐
144 dard had not then been finalized). Although the use of the lat‐
145 ter macro is obsolete, glibc continues to recognize it for back‐
146 ward compatibility.
147
148 _ISOC11_SOURCE
149 Exposes declarations consistent with the ISO C11 standard. This
150 macro is recognized since glibc 2.16.
151
152 _LARGEFILE64_SOURCE
153 Expose definitions for the alternative API specified by the LFS
154 (Large File Summit) as a "transitional extension" to the Single
155 UNIX Specification. (See ⟨http://opengroup.org/platform
156 /lfs.html⟩) The alternative API consists of a set of new objects
157 (i.e., functions and types) whose names are suffixed with "64"
158 (e.g., off64_t versus off_t, lseek64() versus lseek(), etc.).
159 New programs should not employ this interface; instead
160 _FILE_OFFSET_BITS=64 should be employed.
161
162 _FILE_OFFSET_BITS
163 Defining this macro with the value 64 automatically converts
164 references to 32-bit functions and data types related to file
165 I/O and file system operations into references to their 64-bit
166 counterparts. This is useful for performing I/O on large files
167 (> 2 Gigabytes) on 32-bit systems. (Defining this macro permits
168 correctly written programs to use large files with only a recom‐
169 pilation being required.) 64-bit systems naturally permit file
170 sizes greater than 2 Gigabytes, and on those systems this macro
171 has no effect.
172
173 _BSD_SOURCE
174 Defining this macro with any value causes header files to expose
175 BSD-derived definitions. Defining this macro also causes BSD
176 definitions to be preferred in some situations where standards
177 conflict, unless one or more of _SVID_SOURCE, _POSIX_SOURCE,
178 _POSIX_C_SOURCE, _XOPEN_SOURCE, _XOPEN_SOURCE_EXTENDED, or
179 _GNU_SOURCE is defined, in which case BSD definitions are disfa‐
180 vored.
181
182 _SVID_SOURCE
183 Defining this macro with any value causes header files to expose
184 System V-derived definitions. (SVID == System V Interface Defi‐
185 nition; see standards(7).)
186
187 _ATFILE_SOURCE (since glibc 2.4)
188 Defining this macro with any value causes header files to expose
189 declarations of a range of functions with the suffix "at"; see
190 openat(2). Since glibc 2.10, this macro is also implicitly
191 defined if _POSIX_C_SOURCE is defined with a value greater than
192 or equal to 200809L.
193
194 _GNU_SOURCE
195 Defining this macro (with any value) is equivalent to defining
196 _BSD_SOURCE, _SVID_SOURCE, _ATFILE_SOURCE, _LARGEFILE64_SOURCE,
197 _ISOC99_SOURCE, _XOPEN_SOURCE_EXTENDED, _POSIX_SOURCE,
198 _POSIX_C_SOURCE with the value 200809L (200112L in glibc ver‐
199 sions before 2.10; 199506L in glibc versions before 2.5; 199309L
200 in glibc versions before 2.1) and _XOPEN_SOURCE with the value
201 700 (600 in glibc versions before 2.10; 500 in glibc versions
202 before 2.2). In addition, various GNU-specific extensions are
203 also exposed. Where standards conflict, BSD definitions are
204 disfavored.
205
206 _REENTRANT
207 Defining this macro exposes definitions of certain reentrant
208 functions. For multithreaded programs, use cc -pthread instead.
209
210 _THREAD_SAFE
211 Synonym for _REENTRANT, provided for compatibility with some
212 other implementations.
213
214 _FORTIFY_SOURCE (since glibc 2.3.4)
215 Defining this macro causes some lightweight checks to be per‐
216 formed to detect some buffer overflow errors when employing var‐
217 ious string and memory manipulation functions. Not all buffer
218 overflows are detected, just some common cases. In the current
219 implementation checks are added for calls to memcpy(3), mem‐
220 pcpy(3), memmove(3), memset(3), stpcpy(3), strcpy(3),
221 strncpy(3), strcat(3), strncat(3), sprintf(3), snprintf(3),
222 vsprintf(3), vsnprintf(3), and gets(3). If _FORTIFY_SOURCE is
223 set to 1, with compiler optimization level 1 (gcc -O1) and
224 above, checks that shouldn't change the behavior of conforming
225 programs are performed. With _FORTIFY_SOURCE set to 2 some more
226 checking is added, but some conforming programs might fail.
227 Some of the checks can be performed at compile time, and result
228 in compiler warnings; other checks take place at run time, and
229 result in a run-time error if the check fails. Use of this
230 macro requires compiler support, available with gcc(1) since
231 version 4.0.
232
233 Default definitions, implicit definitions, and combining definitions
234 If no feature test macros are explicitly defined, then the following
235 feature test macros are defined by default: _BSD_SOURCE, _SVID_SOURCE,
236 _POSIX_SOURCE, and _POSIX_C_SOURCE=200809L (200112L in glibc versions
237 before 2.10; 199506L in glibc versions before 2.4; 199309L in glibc
238 versions before 2.1).
239
240 If any of __STRICT_ANSI__, _ISOC99_SOURCE, _POSIX_SOURCE,
241 _POSIX_C_SOURCE, _XOPEN_SOURCE, _XOPEN_SOURCE_EXTENDED, _BSD_SOURCE, or
242 _SVID_SOURCE is explicitly defined, then _BSD_SOURCE, and _SVID_SOURCE
243 are not defined by default.
244
245 If _POSIX_SOURCE and _POSIX_C_SOURCE are not explicitly defined, and
246 either __STRICT_ANSI__ is not defined or _XOPEN_SOURCE is defined with
247 a value of 500 or more, then
248
249 * _POSIX_SOURCE is defined with the value 1; and
250
251 * _POSIX_C_SOURCE is defined with one of the following values:
252
253 · 2, if XOPEN_SOURCE is defined with a value less than 500;
254
255 · 199506L, if XOPEN_SOURCE is defined with a value greater
256 than or equal to 500 and less than 600; or
257
258 · (since glibc 2.4) 200112L, if XOPEN_SOURCE is defined with
259 a value greater than or equal to 600 and less than 700.
260
261 · (Since glibc 2.10) 200809L, if XOPEN_SOURCE is defined with
262 a value greater than or equal to 700.
263
264 · Older versions of glibc do not know about the values
265 200112L and 200809L for _POSIX_C_SOURCE, and the setting of
266 this macro will depend on the glibc version.
267
268 · If _XOPEN_SOURCE is undefined, then the setting of
269 _POSIX_C_SOURCE depends on the glibc version: 199506L, in
270 glibc versions before 2.4; 200112L, in glibc 2.4 to 2.9;
271 and 200809L, since glibc 2.10.
272
273 Multiple macros can be defined; the results are additive.
274
276 POSIX.1 specifies _POSIX_C_SOURCE, _POSIX_SOURCE, and _XOPEN_SOURCE.
277 _XOPEN_SOURCE_EXTENDED was specified by XPG4v2 (aka SUSv1).
278
279 _FILE_OFFSET_BITS is not specified by any standard, but is employed on
280 some other implementations.
281
282 _BSD_SOURCE, _SVID_SOURCE, _ATFILE_SOURCE, _GNU_SOURCE, _FOR‐
283 TIFY_SOURCE, _REENTRANT, and _THREAD_SAFE are specific to Linux
284 (glibc).
285
287 <features.h> is a Linux/glibc-specific header file. Other systems have
288 an analogous file, but typically with a different name. This header
289 file is automatically included by other header files as required: it is
290 not necessary to explicitly include it in order to employ feature test
291 macros.
292
293 According to which of the above feature test macros are defined, <fea‐
294 tures.h> internally defines various other macros that are checked by
295 other glibc header files. These macros have names prefixed by two
296 underscores (e.g., __USE_MISC). Programs should never define these
297 macros directly: instead, the appropriate feature test macro(s) from
298 the list above should be employed.
299
301 The program below can be used to explore how the various feature test
302 macros are set depending on the glibc version and what feature test
303 macros are explicitly set. The following shell session, on a system
304 with glibc 2.10, shows some examples of what we would see:
305
306 $ cc ftm.c
307 $ ./a.out
308 _POSIX_SOURCE defined
309 _POSIX_C_SOURCE defined: 200809L
310 _BSD_SOURCE defined
311 _SVID_SOURCE defined
312 _ATFILE_SOURCE defined
313 $ cc -D_XOPEN_SOURCE=500 ftm.c
314 $ ./a.out
315 _POSIX_SOURCE defined
316 _POSIX_C_SOURCE defined: 199506L
317 _XOPEN_SOURCE defined: 500
318 $ cc -D_GNU_SOURCE ftm.c
319 $ ./a.out
320 _POSIX_SOURCE defined
321 _POSIX_C_SOURCE defined: 200809L
322 _ISOC99_SOURCE defined
323 _XOPEN_SOURCE defined: 700
324 _XOPEN_SOURCE_EXTENDED defined
325 _LARGEFILE64_SOURCE defined
326 _BSD_SOURCE defined
327 _SVID_SOURCE defined
328 _ATFILE_SOURCE defined
329 _GNU_SOURCE defined
330
331 Program source
332
333 /* ftm.c */
334
335 #include <stdio.h>
336 #include <unistd.h>
337 #include <stdlib.h>
338
339 int
340 main(int argc, char *argv[])
341 {
342 #ifdef _POSIX_SOURCE
343 printf("_POSIX_SOURCE defined\n");
344 #endif
345
346 #ifdef _POSIX_C_SOURCE
347 printf("_POSIX_C_SOURCE defined: %ldL\n", (long) _POSIX_C_SOURCE);
348 #endif
349
350 #ifdef _ISOC99_SOURCE
351 printf("_ISOC99_SOURCE defined\n");
352 #endif
353
354 #ifdef _XOPEN_SOURCE
355 printf("_XOPEN_SOURCE defined: %d\n", _XOPEN_SOURCE);
356 #endif
357
358 #ifdef _XOPEN_SOURCE_EXTENDED
359 printf("_XOPEN_SOURCE_EXTENDED defined\n");
360 #endif
361
362 #ifdef _LARGEFILE64_SOURCE
363 printf("_LARGEFILE64_SOURCE defined\n");
364 #endif
365
366 #ifdef _FILE_OFFSET_BITS
367 printf("_FILE_OFFSET_BITS defined: %d\n", _FILE_OFFSET_BITS);
368 #endif
369
370 #ifdef _BSD_SOURCE
371 printf("_BSD_SOURCE defined\n");
372 #endif
373
374 #ifdef _SVID_SOURCE
375 printf("_SVID_SOURCE defined\n");
376 #endif
377
378 #ifdef _ATFILE_SOURCE
379 printf("_ATFILE_SOURCE defined\n");
380 #endif
381
382 #ifdef _GNU_SOURCE
383 printf("_GNU_SOURCE defined\n");
384 #endif
385
386 #ifdef _REENTRANT
387 printf("_REENTRANT defined\n");
388 #endif
389
390 #ifdef _THREAD_SAFE
391 printf("_THREAD_SAFE defined\n");
392 #endif
393
394 #ifdef _FORTIFY_SOURCE
395 printf("_FORTIFY_SOURCE defined\n");
396 #endif
397
398 exit(EXIT_SUCCESS);
399 }
400
402 libc(7), standards(7)
403
404 The section "Feature Test Macros" under info libc.
405
406 /usr/include/features.h
407
409 This page is part of release 3.53 of the Linux man-pages project. A
410 description of the project, and information about reporting bugs, can
411 be found at http://www.kernel.org/doc/man-pages/.
412
413
414
415Linux 2012-08-05 FEATURE_TEST_MACROS(7)