]> git.ipfire.org Git - thirdparty/glibc.git/blob - manual/creature.texi
Merge branch 'master' into bug13658-branch
[thirdparty/glibc.git] / manual / creature.texi
1 @node Feature Test Macros
2 @subsection Feature Test Macros
3 @include macros.texi
4
5 @cindex feature test macros
6 The exact set of features available when you compile a source file
7 is controlled by which @dfn{feature test macros} you define.
8
9 If you compile your programs using @samp{gcc -ansi}, you get only the
10 @w{ISO C} library features, unless you explicitly request additional
11 features by defining one or more of the feature macros.
12 @xref{Invoking GCC,, GNU CC Command Options, gcc.info, The GNU CC Manual},
13 for more information about GCC options.@refill
14
15 You should define these macros by using @samp{#define} preprocessor
16 directives at the top of your source code files. These directives
17 @emph{must} come before any @code{#include} of a system header file. It
18 is best to make them the very first thing in the file, preceded only by
19 comments. You could also use the @samp{-D} option to GCC, but it's
20 better if you make the source files indicate their own meaning in a
21 self-contained way.
22
23 This system exists to allow the library to conform to multiple standards.
24 Although the different standards are often described as supersets of each
25 other, they are usually incompatible because larger standards require
26 functions with names that smaller ones reserve to the user program. This
27 is not mere pedantry --- it has been a problem in practice. For instance,
28 some non-GNU programs define functions named @code{getline} that have
29 nothing to do with this library's @code{getline}. They would not be
30 compilable if all features were enabled indiscriminately.
31
32 This should not be used to verify that a program conforms to a limited
33 standard. It is insufficient for this purpose, as it will not protect you
34 from including header files outside the standard, or relying on semantics
35 undefined within the standard.
36
37 @comment (none)
38 @comment POSIX.1
39 @defvr Macro _POSIX_SOURCE
40 If you define this macro, then the functionality from the POSIX.1
41 standard (IEEE Standard 1003.1) is available, as well as all of the
42 @w{ISO C} facilities.
43
44 The state of @code{_POSIX_SOURCE} is irrelevant if you define the
45 macro @code{_POSIX_C_SOURCE} to a positive integer.
46 @end defvr
47
48 @comment (none)
49 @comment POSIX.2
50 @defvr Macro _POSIX_C_SOURCE
51 Define this macro to a positive integer to control which POSIX
52 functionality is made available. The greater the value of this macro,
53 the more functionality is made available.
54
55 If you define this macro to a value greater than or equal to @code{1},
56 then the functionality from the 1990 edition of the POSIX.1 standard
57 (IEEE Standard 1003.1-1990) is made available.
58
59 If you define this macro to a value greater than or equal to @code{2},
60 then the functionality from the 1992 edition of the POSIX.2 standard
61 (IEEE Standard 1003.2-1992) is made available.
62
63 If you define this macro to a value greater than or equal to @code{199309L},
64 then the functionality from the 1993 edition of the POSIX.1b standard
65 (IEEE Standard 1003.1b-1993) is made available.
66
67 Greater values for @code{_POSIX_C_SOURCE} will enable future extensions.
68 The POSIX standards process will define these values as necessary, and
69 @theglibc{} should support them some time after they become standardized.
70 The 1996 edition of POSIX.1 (ISO/IEC 9945-1: 1996) states that
71 if you define @code{_POSIX_C_SOURCE} to a value greater than
72 or equal to @code{199506L}, then the functionality from the 1996
73 edition is made available.
74 @end defvr
75
76 @comment (none)
77 @comment GNU
78 @defvr Macro _BSD_SOURCE
79 If you define this macro, functionality derived from 4.3 BSD Unix is
80 included as well as the @w{ISO C}, POSIX.1, and POSIX.2 material.
81
82 Some of the features derived from 4.3 BSD Unix conflict with the
83 corresponding features specified by the POSIX.1 standard. If this
84 macro is defined, the 4.3 BSD definitions take precedence over the
85 POSIX definitions.
86
87 Due to the nature of some of the conflicts between 4.3 BSD and POSIX.1,
88 you need to use a special @dfn{BSD compatibility library} when linking
89 programs compiled for BSD compatibility. This is because some functions
90 must be defined in two different ways, one of them in the normal C
91 library, and one of them in the compatibility library. If your program
92 defines @code{_BSD_SOURCE}, you must give the option @samp{-lbsd-compat}
93 to the compiler or linker when linking the program, to tell it to find
94 functions in this special compatibility library before looking for them in
95 the normal C library.
96 @pindex -lbsd-compat
97 @pindex bsd-compat
98 @cindex BSD compatibility library.
99 @end defvr
100
101 @comment (none)
102 @comment GNU
103 @defvr Macro _SVID_SOURCE
104 If you define this macro, functionality derived from SVID is
105 included as well as the @w{ISO C}, POSIX.1, POSIX.2, and X/Open material.
106 @end defvr
107
108 @comment (none)
109 @comment X/Open
110 @defvr Macro _XOPEN_SOURCE
111 @comment (none)
112 @comment X/Open
113 @defvrx Macro _XOPEN_SOURCE_EXTENDED
114 If you define this macro, functionality described in the X/Open
115 Portability Guide is included. This is a superset of the POSIX.1 and
116 POSIX.2 functionality and in fact @code{_POSIX_SOURCE} and
117 @code{_POSIX_C_SOURCE} are automatically defined.
118
119 As the unification of all Unices, functionality only available in
120 BSD and SVID is also included.
121
122 If the macro @code{_XOPEN_SOURCE_EXTENDED} is also defined, even more
123 functionality is available. The extra functions will make all functions
124 available which are necessary for the X/Open Unix brand.
125
126 If the macro @code{_XOPEN_SOURCE} has the value @math{500} this includes
127 all functionality described so far plus some new definitions from the
128 Single Unix Specification, @w{version 2}.
129 @end defvr
130
131 @comment (NONE)
132 @comment X/Open
133 @defvr Macro _LARGEFILE_SOURCE
134 If this macro is defined some extra functions are available which
135 rectify a few shortcomings in all previous standards. Specifically,
136 the functions @code{fseeko} and @code{ftello} are available. Without
137 these functions the difference between the @w{ISO C} interface
138 (@code{fseek}, @code{ftell}) and the low-level POSIX interface
139 (@code{lseek}) would lead to problems.
140
141 This macro was introduced as part of the Large File Support extension (LFS).
142 @end defvr
143
144 @comment (NONE)
145 @comment X/Open
146 @defvr Macro _LARGEFILE64_SOURCE
147 If you define this macro an additional set of functions is made available
148 which enables @w{32 bit} systems to use files of sizes beyond
149 the usual limit of 2GB. This interface is not available if the system
150 does not support files that large. On systems where the natural file
151 size limit is greater than 2GB (i.e., on @w{64 bit} systems) the new
152 functions are identical to the replaced functions.
153
154 The new functionality is made available by a new set of types and
155 functions which replace the existing ones. The names of these new objects
156 contain @code{64} to indicate the intention, e.g., @code{off_t}
157 vs. @code{off64_t} and @code{fseeko} vs. @code{fseeko64}.
158
159 This macro was introduced as part of the Large File Support extension
160 (LFS). It is a transition interface for the period when @w{64 bit}
161 offsets are not generally used (see @code{_FILE_OFFSET_BITS}).
162 @end defvr
163
164 @comment (NONE)
165 @comment X/Open
166 @defvr Macro _FILE_OFFSET_BITS
167 This macro determines which file system interface shall be used, one
168 replacing the other. Whereas @code{_LARGEFILE64_SOURCE} makes the @w{64
169 bit} interface available as an additional interface,
170 @code{_FILE_OFFSET_BITS} allows the @w{64 bit} interface to
171 replace the old interface.
172
173 If @code{_FILE_OFFSET_BITS} is undefined, or if it is defined to the
174 value @code{32}, nothing changes. The @w{32 bit} interface is used and
175 types like @code{off_t} have a size of @w{32 bits} on @w{32 bit}
176 systems.
177
178 If the macro is defined to the value @code{64}, the large file interface
179 replaces the old interface. I.e., the functions are not made available
180 under different names (as they are with @code{_LARGEFILE64_SOURCE}).
181 Instead the old function names now reference the new functions, e.g., a
182 call to @code{fseeko} now indeed calls @code{fseeko64}.
183
184 This macro should only be selected if the system provides mechanisms for
185 handling large files. On @w{64 bit} systems this macro has no effect
186 since the @code{*64} functions are identical to the normal functions.
187
188 This macro was introduced as part of the Large File Support extension
189 (LFS).
190 @end defvr
191
192 @comment (none)
193 @comment GNU
194 @defvr Macro _ISOC99_SOURCE
195 Until the revised @w{ISO C} standard is widely adopted the new features
196 are not automatically enabled. @Theglibc{} nevertheless has a complete
197 implementation of the new standard and to enable the new features the
198 macro @code{_ISOC99_SOURCE} should be defined.
199 @end defvr
200
201 @comment (none)
202 @comment GNU
203 @defvr Macro _GNU_SOURCE
204 If you define this macro, everything is included: @w{ISO C89}, @w{ISO
205 C99}, POSIX.1, POSIX.2, BSD, SVID, X/Open, LFS, and GNU extensions. In
206 the cases where POSIX.1 conflicts with BSD, the POSIX definitions take
207 precedence.
208
209 If you want to get the full effect of @code{_GNU_SOURCE} but make the
210 BSD definitions take precedence over the POSIX definitions, use this
211 sequence of definitions:
212
213 @smallexample
214 #define _GNU_SOURCE
215 #define _BSD_SOURCE
216 #define _SVID_SOURCE
217 @end smallexample
218
219 Note that if you do this, you must link your program with the BSD
220 compatibility library by passing the @samp{-lbsd-compat} option to the
221 compiler or linker. @strong{NB:} If you forget to do this, you may
222 get very strange errors at run time.
223 @end defvr
224
225 @comment (none)
226 @comment GNU
227 @defvr Macro _REENTRANT
228 @defvrx Macro _THREAD_SAFE
229 If you define one of these macros, reentrant versions of several functions get
230 declared. Some of the functions are specified in POSIX.1c but many others
231 are only available on a few other systems or are unique to @theglibc{}.
232 The problem is the delay in the standardization of the thread safe C library
233 interface.
234
235 Unlike on some other systems, no special version of the C library must be
236 used for linking. There is only one version but while compiling this
237 it must have been specified to compile as thread safe.
238 @end defvr
239
240 We recommend you use @code{_GNU_SOURCE} in new programs. If you don't
241 specify the @samp{-ansi} option to GCC and don't define any of these
242 macros explicitly, the effect is the same as defining
243 @code{_POSIX_C_SOURCE} to 2 and @code{_POSIX_SOURCE},
244 @code{_SVID_SOURCE}, and @code{_BSD_SOURCE} to 1.
245
246 When you define a feature test macro to request a larger class of features,
247 it is harmless to define in addition a feature test macro for a subset of
248 those features. For example, if you define @code{_POSIX_C_SOURCE}, then
249 defining @code{_POSIX_SOURCE} as well has no effect. Likewise, if you
250 define @code{_GNU_SOURCE}, then defining either @code{_POSIX_SOURCE} or
251 @code{_POSIX_C_SOURCE} or @code{_SVID_SOURCE} as well has no effect.
252
253 Note, however, that the features of @code{_BSD_SOURCE} are not a subset of
254 any of the other feature test macros supported. This is because it defines
255 BSD features that take precedence over the POSIX features that are
256 requested by the other macros. For this reason, defining
257 @code{_BSD_SOURCE} in addition to the other feature test macros does have
258 an effect: it causes the BSD features to take priority over the conflicting
259 POSIX features.