]> git.ipfire.org Git - thirdparty/glibc.git/blame - manual/creature.texi
update from main archive 961217
[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
22@comment (none)
23@comment POSIX.1
24@defvr Macro _POSIX_SOURCE
25If you define this macro, then the functionality from the POSIX.1
26standard (IEEE Standard 1003.1) is available, as well as all of the
f65fd747 27@w{ISO C} facilities.
28f540f4
RM
28@end defvr
29
30@comment (none)
31@comment POSIX.2
32@defvr Macro _POSIX_C_SOURCE
33If you define this macro with a value of @code{1}, then the
34functionality from the POSIX.1 standard (IEEE Standard 1003.1) is made
35available. If you define this macro with a value of @code{2}, then both
36the functionality from the POSIX.1 standard and the functionality from
37the POSIX.2 standard (IEEE Standard 1003.2) are made available. This is
f65fd747 38in addition to the @w{ISO C} facilities.
28f540f4
RM
39@end defvr
40
41@comment (none)
42@comment GNU
43@defvr Macro _BSD_SOURCE
44If you define this macro, functionality derived from 4.3 BSD Unix is
f65fd747 45included as well as the @w{ISO C}, POSIX.1, and POSIX.2 material.
28f540f4
RM
46
47Some of the features derived from 4.3 BSD Unix conflict with the
48corresponding features specified by the POSIX.1 standard. If this
49macro is defined, the 4.3 BSD definitions take precedence over the
50POSIX definitions.
51
52Due to the nature of some of the conflicts between 4.3 BSD and POSIX.1,
53you need to use a special @dfn{BSD compatibility library} when linking
54programs compiled for BSD compatibility. This is because some functions
55must be defined in two different ways, one of them in the normal C
56library, and one of them in the compatibility library. If your program
57defines @code{_BSD_SOURCE}, you must give the option @samp{-lbsd-compat}
58to the compiler or linker when linking the program, to tell it to find
59functions in this special compatibility library before looking for them in
60the normal C library.
61@pindex -lbsd-compat
62@pindex bsd-compat
63@cindex BSD compatibility library.
64@end defvr
65
66@comment (none)
67@comment GNU
68@defvr Macro _SVID_SOURCE
69If you define this macro, functionality derived from SVID is
f65fd747 70included as well as the @w{ISO C}, POSIX.1, POSIX.2, and X/Open material.
2c6fe0bd
UD
71@end defvr
72
73@comment (none)
74@comment XOPEN
75@defvr Macro _XOPEN_SOURCE
76If you define these macro, functionality described in the X/Open
77Portability Guide is included. This is an superset of the POSIX.1 and
78POSIX.2 functionality and in fact @code{_POSIX_SOURCE} and
79@code{_POSIX_C_SOURCE} get automatically be defined.
80
26761c28 81But as the great unifiction of all Unices there is also functionality
2c6fe0bd
UD
82only available in BSD and SVID is included.
83
84If the macro @code{_XOPEN_SOURCE_EXTENDED} is also defined, even more
85functionality is available. The extra functions will make all functions
86available which are necessary for the X/Open Unix brand.
28f540f4
RM
87@end defvr
88
89@comment (none)
90@comment GNU
91@defvr Macro _GNU_SOURCE
f65fd747 92If you define this macro, everything is included: @w{ISO C}, POSIX.1,
2c6fe0bd
UD
93POSIX.2, BSD, SVID, X/Open, and GNU extensions. In the cases where
94POSIX.1 conflicts with BSD, the POSIX definitions take precedence.
28f540f4
RM
95
96If you want to get the full effect of @code{_GNU_SOURCE} but make the
97BSD definitions take precedence over the POSIX definitions, use this
98sequence of definitions:
99
100@smallexample
101#define _GNU_SOURCE
102#define _BSD_SOURCE
103#define _SVID_SOURCE
104@end smallexample
105
106Note that if you do this, you must link your program with the BSD
107compatibility library by passing the @samp{-lbsd-compat} option to the
108compiler or linker. @strong{Note:} If you forget to do this, you may
109get very strange errors at run time.
110@end defvr
111
ba1ffaa1
UD
112@comment (none)
113@comment GNU
26761c28
UD
114@defvr Macro _REENTRANT
115@defvrx Macro _THREAD_SAFE
116If you define one of these macros, reentrant versions of several functions get
2c6fe0bd 117declared. Some of the functions are specified in POSIX.1c but many others
ba1ffaa1
UD
118are only available on a few other systems or are unique to GNU libc.
119The problem is that the standardization of the thread safe C library
120interface still is behind.
121
122Unlike on some other systems no special version of the C library must be
123used for linking. There is only one version but while compiling this
124it must have been specified to compile as thread safe.
125@end defvr
126
28f540f4 127We recommend you use @code{_GNU_SOURCE} in new programs. If you don't
2303f5fd
UD
128specify the @samp{-ansi} option to GCC and don't define any of these
129macros explicitly, the effect is the same as defining
130@code{_POSIX_C_SOURCE} to 2 and @code{_POSIX_SOURCE},
131@code{_SVID_SOURCE}, and @code{_BSD_SOURCE} to 1.
28f540f4
RM
132
133When you define a feature test macro to request a larger class of features,
134it is harmless to define in addition a feature test macro for a subset of
135those features. For example, if you define @code{_POSIX_C_SOURCE}, then
136defining @code{_POSIX_SOURCE} as well has no effect. Likewise, if you
137define @code{_GNU_SOURCE}, then defining either @code{_POSIX_SOURCE} or
138@code{_POSIX_C_SOURCE} or @code{_SVID_SOURCE} as well has no effect.
139
140Note, however, that the features of @code{_BSD_SOURCE} are not a subset of
141any of the other feature test macros supported. This is because it defines
142BSD features that take precedence over the POSIX features that are
143requested by the other macros. For this reason, defining
144@code{_BSD_SOURCE} in addition to the other feature test macros does have
145an effect: it causes the BSD features to take priority over the conflicting
146POSIX features.