1FEATURE_TEST_MACROS(7)     Linux Programmer's Manual    FEATURE_TEST_MACROS(7)
2
3
4

NAME

6       feature_test_macros - feature test macros
7

SYNOPSIS

9       #include <features.h>
10

DESCRIPTION

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       This  can  be  useful for creating portable applications, by preventing
15       non-standard definitions from being exposed.  Other macros can be  used
16       to  expose  non-standard  definitions  that are not exposed by default.
17       The precise effects of each of the feature test macros described  below
18       can be ascertained by inspecting the <features.h> header file.
19
20       In  order  to be effective, a feature test macro must be defined before
21       including any header files.  This can either be done in the compilation
22       command  (cc  -DMACRO=value) or by defining the macro within the source
23       code before including any headers.
24
25   Specification of feature test macro requirements in manual pages
26       When a function requires that a feature test macro is defined, the man‐
27       ual page SYNOPSIS typically includes a note of the following form (this
28       example from the chmod(2) manual page):
29
30              #include <sys/stat.h>
31
32              int chmod(const char *path, mode_t mode);
33              int fchmod(int fd, mode_t mode);
34
35          Feature   Test   Macro   Requirements   for    glibc    (see    fea‐
36          ture_test_macros(7)):
37
38              fchmod(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
39
40       The  || means that in order to obtain the declaration of fchmod(2) from
41       <sys/stat.h>, either of the following macro definitions  must  be  made
42       before including any header files:
43
44              #define _BSD_SOURCE
45              #define _XOPEN_SOURCE 500     /* or any value > 500 */
46
47       Alternatively,  equivalent  definitions can be included in the compila‐
48       tion command:
49
50              cc -D_BSD_SOURCE
51              cc -D_XOPEN_SOURCE=500        # Or any value > 500
52
53       Note that, as described below, some feature test macros are defined  by
54       default,  so  that it may not always be necessary to explicitly specify
55       the feature test macro(s) shown in the SYNOPSIS.
56
57       In a few cases, manual pages use a shorthand for expressing the feature
58       test macro requirements (this example from readahead(2)):
59
60              #define _GNU_SOURCE
61              #include <fcntl.h>
62
63              ssize_t readahead(int fd, off64_t *offset, size_t count);
64
65       This format is employed in cases where only a single feature test macro
66       can be used to expose the function declaration, and that macro  is  not
67       defined by default.
68
69   Feature test macros understood by glibc
70       The following paragraphs explain how feature test macros are handled in
71       Linux glibc 2.x, x > 0.
72
73       Linux glibc understands the following feature test macros:
74
75       __STRICT_ANSI__
76              ISO Standard C.  This macro is implicitly defined by gcc(1) when
77              invoked with, for example, the -std=c99 or -ansi flag.
78
79       _POSIX_C_SOURCE
80              Defining this macro causes header files to expose definitions as
81              follows:
82
83              ·  The value 1 exposes definitions  conforming  to  POSIX.1-1990
84                 and ISO C (1990).
85
86              ·  The  value  2 or greater additionally exposes definitions for
87                 POSIX.2-1992.
88
89              ·  The value 199309L or greater additionally exposes definitions
90                 for POSIX.1b (real-time extensions).
91
92              ·  The value 199506L or greater additionally exposes definitions
93                 for POSIX.1c (threads).
94
95              ·  (Since glibc 2.3.3) The value 200112L or greater exposes def‐
96                 initions corresponding to the POSIX.1-2001 base specification
97                 (excluding the XSI extension).
98
99              ·  (Since glibc 2.10) The value 200809L or greater exposes defi‐
100                 nitions  corresponding to the POSIX.1-2008 base specification
101                 (excluding the XSI extension).
102
103       _POSIX_SOURCE
104              Defining this obsolete macro with any  value  is  equivalent  to
105              defining _POSIX_C_SOURCE with the value 1.
106
107       _XOPEN_SOURCE
108              Defining this macro causes header files to expose definitions as
109              follows:
110
111              ·  Defining with any value  exposes  definitions  conforming  to
112                 POSIX.1, POSIX.2, and XPG4.
113
114              ·  The value 500 or greater additionally exposes definitions for
115                 SUSv2 (UNIX 98).
116
117              ·  (Since glibc 2.2)  The  value  600  or  greater  additionally
118                 exposes   definitions   for   SUSv3   (UNIX   03;  i.e.,  the
119                 POSIX.1-2001 base specification plus the XSI  extension)  and
120                 C99 definitions.
121
122              ·  (Since  glibc  2.10)  The  value  700 or greater additionally
123                 exposes definitions for SUSv4 (i.e.,  the  POSIX.1-2008  base
124                 specification plus the XSI extension).
125
126       _XOPEN_SOURCE_EXTENDED
127              If  this  macro  is  defined, and _XOPEN_SOURCE is defined, then
128              expose definitions corresponding  to  the  XPG4v2  (SUSv1)  UNIX
129              extensions  (UNIX 95).  This macro is also implicitly defined if
130              _XOPEN_SOURCE is defined with a value of 500 or more.
131
132       _ISOC99_SOURCE
133              Exposes C99 extensions to ISO C (1990).  This  macro  is  recog‐
134              nized since glibc 2.1.3; earlier glibc 2.1.x versions recognized
135              an equivalent macro named _ISOC9X_SOURCE (because the C99  stan‐
136              dard had not then been finalized).  Although the use of the lat‐
137              ter macro is obsolete, glibc continues to recognize it for back‐
138              wards compatibility.
139
140       _LARGEFILE64_SOURCE
141              Expose  definitions for the alternative API specified by the LFS
142              (Large File Summit) as a "transitional extension" to the  Single
143              UNIX     Specification.      (See     http://opengroup.org/plat
144              form/lfs.html.)  The alternative API consists of a  set  of  new
145              objects  (i.e.,  functions  and  types) whose names are suffixed
146              with "64" (e.g., off64_t versus off_t, lseek64() versus lseek(),
147              etc.).   New  programs should not employ this interface; instead
148              _FILE_OFFSET_BITS=64 should be employed.
149
150       _FILE_OFFSET_BITS
151              Defining this macro with the  value  64  automatically  converts
152              references  to  32-bit  functions and data types related to file
153              I/O and file system operations into references to  their  64-bit
154              counterparts.   This is useful for performing I/O on large files
155              (> 2 Gigabytes) on 32-bit systems.  (Defining this macro permits
156              correctly written programs to use large files with only a recom‐
157              pilation being required.)  64-bit systems naturally permit  file
158              sizes  greater than 2 Gigabytes, and on those systems this macro
159              has no effect.
160
161       _BSD_SOURCE
162              Defining this macro with any value causes header files to expose
163              BSD-derived  definitions.   Defining  this macro also causes BSD
164              definitions to be preferred in some situations  where  standards
165              conflict,  unless  one  or  more of _SVID_SOURCE, _POSIX_SOURCE,
166              _POSIX_C_SOURCE,   _XOPEN_SOURCE,   _XOPEN_SOURCE_EXTENDED,   or
167              _GNU_SOURCE is defined, in which case BSD definitions are disfa‐
168              vored.
169
170       _SVID_SOURCE
171              Defining this macro with any value causes header files to expose
172              System V-derived definitions.  (SVID == System V Interface Defi‐
173              nition; see standards(7).)
174
175       _ATFILE_SOURCE (since glibc 2.4)
176              Defining this macro with any value causes header files to expose
177              declarations  of  a range of functions with the suffix "at"; see
178              openat(2).
179
180       _GNU_SOURCE
181              Defining this macro (with any value) is equivalent  to  defining
182              _BSD_SOURCE,  _SVID_SOURCE, _ATFILE_SOURCE, _LARGEFILE64_SOURCE,
183              _ISOC99_SOURCE,      _XOPEN_SOURCE_EXTENDED,      _POSIX_SOURCE,
184              _POSIX_C_SOURCE  with  the  value 200809L (200112L in glibc ver‐
185              sions before 2.10; 199506L in glibc versions before 2.5; 199309L
186              in  glibc  versions before 2.1) and _XOPEN_SOURCE with the value
187              700 (600 in glibc versions before 2.10; 500  in  glibc  versions
188              before  2.2).   In addition, various GNU-specific extensions are
189              also exposed.  Where standards  conflict,  BSD  definitions  are
190              disfavored.
191
192       _REENTRANT
193              Defining  this  macro  exposes  definitions of certain reentrant
194              functions.  For multithreaded programs, use cc -pthread instead.
195
196       _THREAD_SAFE
197              Synonym for _REENTRANT, provided  for  compatibility  with  some
198              other implementations.
199
200       _FORTIFY_SOURCE (since glibc 2.3.4)
201              Defining  this  macro  causes some lightweight checks to be per‐
202              formed to detect some buffer overflow errors when employing var‐
203              ious  string  and memory manipulation functions.  Not all buffer
204              overflows are detected, just some common cases.  In the  current
205              implementation  checks  are  added  for calls to memcpy(3), mem‐
206              pcpy(3),   memmove(3),    memset(3),    stpcpy(3),    strcpy(3),
207              strncpy(3),   strcat(3),  strncat(3),  sprintf(3),  snprintf(3),
208              vsprintf(3), vsnprintf(3), and gets(3).  If  _FORTIFY_SOURCE  is
209              set  to  1,  with  compiler  optimization  level 1 (gcc -O1) and
210              above, checks that shouldn't change the behavior  of  conforming
211              programs are performed.  With _FORTIFY_SOURCE set to 2 some more
212              checking is added, but  some  conforming  programs  might  fail.
213              Some  of the checks can be performed at compile time, and result
214              in compiler warnings; other checks take place at run  time,  and
215              result  in  a  run-time  error  if the check fails.  Use of this
216              macro requires compiler support,  available  with  gcc(1)  since
217              version 4.0.
218
219   Default definitions, implicit definitions, and combining definitions
220       If  no  feature  test macros are explicitly defined, then the following
221       feature test macros are defined by default: _BSD_SOURCE,  _SVID_SOURCE,
222       _POSIX_SOURCE,  and  _POSIX_C_SOURCE=200909L (200112L in glibc versions
223       before 2.10; 199506L in glibc versions before  2.4;  199309L  in  glibc
224       versions before 2.1).
225
226       If    any    of    __STRICT_ANSI__,    _ISOC99_SOURCE,   _POSIX_SOURCE,
227       _POSIX_C_SOURCE, _XOPEN_SOURCE, _XOPEN_SOURCE_EXTENDED, _BSD_SOURCE, or
228       _SVID_SOURCE  is explicitly defined, then _BSD_SOURCE, and _SVID_SOURCE
229       are not defined by default.
230
231       If _POSIX_SOURCE and _POSIX_C_SOURCE are not  explicitly  defined,  and
232       either  __STRICT_ANSI__ is not defined or _XOPEN_SOURCE is defined with
233       a value of 500 or more, then
234
235          *  _POSIX_SOURCE is defined with the value 1; and
236
237          *  _POSIX_C_SOURCE is defined with one of the following values:
238
239                ·  2, if XOPEN_SOURCE is defined with a value less than 500;
240
241                ·  199506L, if XOPEN_SOURCE is defined with  a  value  greater
242                   than or equal to 500 and less than 600; or
243
244                ·  (since  glibc 2.4) 200112L, if XOPEN_SOURCE is defined with
245                   a value greater than or equal to 600 and less than 700.
246
247                ·  (Since glibc 2.10) 200809L, if XOPEN_SOURCE is defined with
248                   a value greater than or equal to 700.
249
250                ·  Older  versions  of  glibc  do  not  know  about the values
251                   2001212L and 200809L for _POSIX_C_SOURCE, and  the  setting
252                   of this macro will depend on the glibc version.
253
254                ·  If   _XOPEN_SOURCE   is  undefined,  then  the  setting  of
255                   _POSIX_C_SOURCE depends on the glibc version:  199506L,  in
256                   glibc  versions  before  2.4; 200112L, in glibc 2.4 to 2.9;
257                   and 200809L, since glibc 2.10.
258
259       Multiple macros can be defined; the results are additive.
260

CONFORMING TO

262       POSIX.1 specifies _POSIX_C_SOURCE,  _POSIX_SOURCE,  and  _XOPEN_SOURCE.
263       _XOPEN_SOURCE_EXTENDED was specified by XPG4v2 (aka SUSv1).
264
265       _FILE_OFFSET_BITS  is not specified by any standard, but is employed on
266       some other implementations.
267
268       _BSD_SOURCE,   _SVID_SOURCE,   _ATFILE_SOURCE,    _GNU_SOURCE,    _FOR‐
269       TIFY_SOURCE,   _REENTRANT,  and  _THREAD_SAFE  are  specific  to  Linux
270       (glibc).
271

NOTES

273       <features.h> is a Linux/glibc-specific header file.  Other systems have
274       an  analogous  file,  but typically with a different name.  This header
275       file is automatically included by other header files as required: it is
276       not  necessary to explicitly include it in order to employ feature test
277       macros.
278
279       According to which of the above feature test macros are defined,  <fea‐
280       tures.h>  internally  defines  various other macros that are checked by
281       other glibc header files.  These macros  have  names  prefixed  by  two
282       underscores  (e.g.,  __USE_MISC).   Programs  should never define these
283       macros directly: instead, the appropriate feature  test  macro(s)  from
284       the list above should be employed.
285

EXAMPLE

287       The  program  below can be used to explore how the various feature test
288       macros are set depending on the glibc version  and  what  feature  test
289       macros  are  explicitly  set.  The following shell session, on a system
290       with glibc 2.10, shows some examples of what we would see:
291
292           $ cc ftm.c
293           $ ./a.out
294           _POSIX_SOURCE defined
295           _POSIX_C_SOURCE defined: 200809L
296           _BSD_SOURCE defined
297           _SVID_SOURCE defined
298           _ATFILE_SOURCE defined
299           $ cc -D_XOPEN_SOURCE=500 ftm.c
300           $ ./a.out
301           _POSIX_SOURCE defined
302           _POSIX_C_SOURCE defined: 199506L
303           _XOPEN_SOURCE defined: 500
304           $ cc -D_GNU_SOURCE ftm.c
305           $ ./a.out
306           _POSIX_SOURCE defined
307           _POSIX_C_SOURCE defined: 200809L
308           _ISOC99_SOURCE defined
309           _XOPEN_SOURCE defined: 700
310           _XOPEN_SOURCE_EXTENDED defined
311           _LARGEFILE64_SOURCE defined
312           _BSD_SOURCE defined
313           _SVID_SOURCE defined
314           _ATFILE_SOURCE defined
315           _GNU_SOURCE defined
316
317   Program source
318
319       /* ftm.c */
320
321       #include <stdio.h>
322       #include <unistd.h>
323       #include <stdlib.h>
324
325       int
326       main(int argc, char *argv[])
327       {
328       #ifdef _POSIX_SOURCE
329           printf("_POSIX_SOURCE defined\n");
330       #endif
331
332       #ifdef _POSIX_C_SOURCE
333           printf("_POSIX_C_SOURCE defined: %ldL\n", (long) _POSIX_C_SOURCE);
334       #endif
335
336       #ifdef _ISOC99_SOURCE
337           printf("_ISOC99_SOURCE defined\n");
338       #endif
339
340       #ifdef _XOPEN_SOURCE
341           printf("_XOPEN_SOURCE defined: %d\n", _XOPEN_SOURCE);
342       #endif
343
344       #ifdef _XOPEN_SOURCE_EXTENDED
345           printf("_XOPEN_SOURCE_EXTENDED defined\n");
346       #endif
347
348       #ifdef _LARGEFILE64_SOURCE
349           printf("_LARGEFILE64_SOURCE defined\n");
350       #endif
351
352       #ifdef _FILE_OFFSET_BITS
353           printf("_FILE_OFFSET_BITS defined: %d\n", _FILE_OFFSET_BITS);
354       #endif
355
356       #ifdef _BSD_SOURCE
357           printf("_BSD_SOURCE defined\n");
358       #endif
359
360       #ifdef _SVID_SOURCE
361           printf("_SVID_SOURCE defined\n");
362       #endif
363
364       #ifdef _ATFILE_SOURCE
365           printf("_ATFILE_SOURCE defined\n");
366       #endif
367
368       #ifdef _GNU_SOURCE
369           printf("_GNU_SOURCE defined\n");
370       #endif
371
372       #ifdef _REENTRANT
373           printf("_REENTRANT defined\n");
374       #endif
375
376       #ifdef _THREAD_SAFE
377           printf("_THREAD_SAFE defined\n");
378       #endif
379
380       #ifdef _FORTIFY_SOURCE
381           printf("_FORTIFY_SOURCE defined\n");
382       #endif
383
384           exit(EXIT_SUCCESS);
385       }
386

SEE ALSO

388       libc(7), standards(7)
389
390       The section "Feature Test Macros" under info libc.
391
392       /usr/include/features.h
393

COLOPHON

395       This page is part of release 3.22 of the Linux  man-pages  project.   A
396       description  of  the project, and information about reporting bugs, can
397       be found at http://www.kernel.org/doc/man-pages/.
398
399
400
401Linux                             2009-04-03            FEATURE_TEST_MACROS(7)
Impressum