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