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       nonstandard definitions from being exposed.  Other macros can  be  used
16       to expose nonstandard definitions that are not exposed by default.  The
17       precise effects of each of the feature test macros described below  can
18       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).  Since glibc 2.10,  this  macro  is  also  implicitly
179              defined  if _POSIX_C_SOURCE is defined with a value greater than
180              or equal to 200809L.
181
182       _GNU_SOURCE
183              Defining this macro (with any value) is equivalent  to  defining
184              _BSD_SOURCE,  _SVID_SOURCE, _ATFILE_SOURCE, _LARGEFILE64_SOURCE,
185              _ISOC99_SOURCE,      _XOPEN_SOURCE_EXTENDED,      _POSIX_SOURCE,
186              _POSIX_C_SOURCE  with  the  value 200809L (200112L in glibc ver‐
187              sions before 2.10; 199506L in glibc versions before 2.5; 199309L
188              in  glibc  versions before 2.1) and _XOPEN_SOURCE with the value
189              700 (600 in glibc versions before 2.10; 500  in  glibc  versions
190              before  2.2).   In addition, various GNU-specific extensions are
191              also exposed.  Where standards  conflict,  BSD  definitions  are
192              disfavored.
193
194       _REENTRANT
195              Defining  this  macro  exposes  definitions of certain reentrant
196              functions.  For multithreaded programs, use cc -pthread instead.
197
198       _THREAD_SAFE
199              Synonym for _REENTRANT, provided  for  compatibility  with  some
200              other implementations.
201
202       _FORTIFY_SOURCE (since glibc 2.3.4)
203              Defining  this  macro  causes some lightweight checks to be per‐
204              formed to detect some buffer overflow errors when employing var‐
205              ious  string  and memory manipulation functions.  Not all buffer
206              overflows are detected, just some common cases.  In the  current
207              implementation  checks  are  added  for calls to memcpy(3), mem‐
208              pcpy(3),   memmove(3),    memset(3),    stpcpy(3),    strcpy(3),
209              strncpy(3),   strcat(3),  strncat(3),  sprintf(3),  snprintf(3),
210              vsprintf(3), vsnprintf(3), and gets(3).  If  _FORTIFY_SOURCE  is
211              set  to  1,  with  compiler  optimization  level 1 (gcc -O1) and
212              above, checks that shouldn't change the behavior  of  conforming
213              programs are performed.  With _FORTIFY_SOURCE set to 2 some more
214              checking is added, but  some  conforming  programs  might  fail.
215              Some  of the checks can be performed at compile time, and result
216              in compiler warnings; other checks take place at run  time,  and
217              result  in  a  run-time  error  if the check fails.  Use of this
218              macro requires compiler support,  available  with  gcc(1)  since
219              version 4.0.
220
221   Default definitions, implicit definitions, and combining definitions
222       If  no  feature  test macros are explicitly defined, then the following
223       feature test macros are defined by default: _BSD_SOURCE,  _SVID_SOURCE,
224       _POSIX_SOURCE,  and  _POSIX_C_SOURCE=200809L (200112L in glibc versions
225       before 2.10; 199506L in glibc versions before  2.4;  199309L  in  glibc
226       versions before 2.1).
227
228       If    any    of    __STRICT_ANSI__,    _ISOC99_SOURCE,   _POSIX_SOURCE,
229       _POSIX_C_SOURCE, _XOPEN_SOURCE, _XOPEN_SOURCE_EXTENDED, _BSD_SOURCE, or
230       _SVID_SOURCE  is explicitly defined, then _BSD_SOURCE, and _SVID_SOURCE
231       are not defined by default.
232
233       If _POSIX_SOURCE and _POSIX_C_SOURCE are not  explicitly  defined,  and
234       either  __STRICT_ANSI__ is not defined or _XOPEN_SOURCE is defined with
235       a value of 500 or more, then
236
237          *  _POSIX_SOURCE is defined with the value 1; and
238
239          *  _POSIX_C_SOURCE is defined with one of the following values:
240
241                ·  2, if XOPEN_SOURCE is defined with a value less than 500;
242
243                ·  199506L, if XOPEN_SOURCE is defined with  a  value  greater
244                   than or equal to 500 and less than 600; or
245
246                ·  (since  glibc 2.4) 200112L, if XOPEN_SOURCE is defined with
247                   a value greater than or equal to 600 and less than 700.
248
249                ·  (Since glibc 2.10) 200809L, if XOPEN_SOURCE is defined with
250                   a value greater than or equal to 700.
251
252                ·  Older  versions  of  glibc  do  not  know  about the values
253                   200112L and 200809L for _POSIX_C_SOURCE, and the setting of
254                   this macro will depend on the glibc version.
255
256                ·  If   _XOPEN_SOURCE   is  undefined,  then  the  setting  of
257                   _POSIX_C_SOURCE depends on the glibc version:  199506L,  in
258                   glibc  versions  before  2.4; 200112L, in glibc 2.4 to 2.9;
259                   and 200809L, since glibc 2.10.
260
261       Multiple macros can be defined; the results are additive.
262

CONFORMING TO

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

NOTES

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

EXAMPLE

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

SEE ALSO

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

COLOPHON

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