Sun Dec 17 15:56:35 1995 Miles Bader <miles@gnu.ai.mit.edu>
[glibc.git] / manual / creature.texi
blob51bf53a0c26701b919c2c46e40774c36d62b9194
1 @node Feature Test Macros
2 @subsection Feature Test Macros
4 @cindex feature test macros
5 The exact set of features available when you compile a source file
6 is controlled by which @dfn{feature test macros} you define.
8 If you compile your programs using @samp{gcc -ansi}, you get only the
9 ANSI C library features, unless you explicitly request additional
10 features by defining one or more of the feature macros.
11 @xref{Invoking GCC,, GNU CC Command Options, gcc.info, The GNU CC Manual},
12 for more information about GCC options.@refill
14 You should define these macros by using @samp{#define} preprocessor
15 directives at the top of your source code files.  These directives
16 @emph{must} come before any @code{#include} of a system header file.  It
17 is best to make them the very first thing in the file, preceded only by
18 comments.  You could also use the @samp{-D} option to GCC, but it's
19 better if you make the source files indicate their own meaning in a
20 self-contained way.
22 @comment (none)
23 @comment POSIX.1
24 @defvr Macro _POSIX_SOURCE
25 If you define this macro, then the functionality from the POSIX.1
26 standard (IEEE Standard 1003.1) is available, as well as all of the
27 ANSI C facilities.
28 @end defvr
30 @comment (none)
31 @comment POSIX.2
32 @defvr Macro _POSIX_C_SOURCE
33 If you define this macro with a value of @code{1}, then the
34 functionality from the POSIX.1 standard (IEEE Standard 1003.1) is made
35 available.  If you define this macro with a value of @code{2}, then both
36 the functionality from the POSIX.1 standard and the functionality from
37 the POSIX.2 standard (IEEE Standard 1003.2) are made available.  This is
38 in addition to the ANSI C facilities.
39 @end defvr
41 @comment (none)
42 @comment GNU
43 @defvr Macro _BSD_SOURCE
44 If you define this macro, functionality derived from 4.3 BSD Unix is
45 included as well as the ANSI C, POSIX.1, and POSIX.2 material.
47 Some of the features derived from 4.3 BSD Unix conflict with the
48 corresponding features specified by the POSIX.1 standard.  If this
49 macro is defined, the 4.3 BSD definitions take precedence over the
50 POSIX definitions.
52 Due to the nature of some of the conflicts between 4.3 BSD and POSIX.1,
53 you need to use a special @dfn{BSD compatibility library} when linking
54 programs compiled for BSD compatibility.  This is because some functions
55 must be defined in two different ways, one of them in the normal C
56 library, and one of them in the compatibility library.  If your program
57 defines @code{_BSD_SOURCE}, you must give the option @samp{-lbsd-compat}
58 to the compiler or linker when linking the program, to tell it to find
59 functions in this special compatibility library before looking for them in
60 the normal C library.
61 @pindex -lbsd-compat
62 @pindex bsd-compat
63 @cindex BSD compatibility library.
64 @end defvr
66 @comment (none)
67 @comment GNU
68 @defvr Macro _SVID_SOURCE
69 If you define this macro, functionality derived from SVID is
70 included as well as the ANSI C, POSIX.1, and POSIX.2 material.
71 @end defvr
73 @comment (none)
74 @comment GNU
75 @defvr Macro _GNU_SOURCE
76 If you define this macro, everything is included: ANSI C, POSIX.1,
77 POSIX.2, BSD, SVID, and GNU extensions.  In the cases where POSIX.1
78 conflicts with BSD, the POSIX definitions take precedence.
80 If you want to get the full effect of @code{_GNU_SOURCE} but make the
81 BSD definitions take precedence over the POSIX definitions, use this
82 sequence of definitions:
84 @smallexample
85 #define _GNU_SOURCE
86 #define _BSD_SOURCE
87 #define _SVID_SOURCE
88 @end smallexample
90 Note that if you do this, you must link your program with the BSD
91 compatibility library by passing the @samp{-lbsd-compat} option to the
92 compiler or linker.  @strong{Note:} If you forget to do this, you may
93 get very strange errors at run time.
94 @end defvr
96 We recommend you use @code{_GNU_SOURCE} in new programs.  If you don't
97 specify the @samp{-ansi} option to GCC and don't define any of these macros
98 explicitly, the effect is the same as defining @code{_GNU_SOURCE}.
100 When you define a feature test macro to request a larger class of features,
101 it is harmless to define in addition a feature test macro for a subset of
102 those features.  For example, if you define @code{_POSIX_C_SOURCE}, then
103 defining @code{_POSIX_SOURCE} as well has no effect.  Likewise, if you
104 define @code{_GNU_SOURCE}, then defining either @code{_POSIX_SOURCE} or
105 @code{_POSIX_C_SOURCE} or @code{_SVID_SOURCE} as well has no effect.
107 Note, however, that the features of @code{_BSD_SOURCE} are not a subset of
108 any of the other feature test macros supported.  This is because it defines
109 BSD features that take precedence over the POSIX features that are
110 requested by the other macros.  For this reason, defining
111 @code{_BSD_SOURCE} in addition to the other feature test macros does have
112 an effect: it causes the BSD features to take priority over the conflicting
113 POSIX features.