blob: 3c686165f1ee44160474e8a37576d4f396fc64e8 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001@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
9@w{ISO C} library features, unless you explicitly request additional
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
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 indiscriminately.
30
31This should not be used to verify that a program conforms to a limited
32standard. It is insufficient 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
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
41@w{ISO C} facilities.
42
43The state of @code{_POSIX_SOURCE} is irrelevant if you define the
44macro @code{_POSIX_C_SOURCE} to a positive integer.
45@end defvr
46
47@comment (none)
48@comment POSIX.2
49@defvr Macro _POSIX_C_SOURCE
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
68@theglibc{} 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.
73@end defvr
74
75@comment (none)
76@comment X/Open
77@defvr Macro _XOPEN_SOURCE
78@comment (none)
79@comment X/Open
80@defvrx Macro _XOPEN_SOURCE_EXTENDED
81If you define this macro, functionality described in the X/Open
82Portability Guide is included. This is a superset of the POSIX.1 and
83POSIX.2 functionality and in fact @code{_POSIX_SOURCE} and
84@code{_POSIX_C_SOURCE} are automatically defined.
85
86As the unification of all Unices, functionality only available in
87BSD and SVID is also included.
88
89If the macro @code{_XOPEN_SOURCE_EXTENDED} is also defined, even more
90functionality is available. The extra functions will make all functions
91available which are necessary for the X/Open Unix brand.
92
93If the macro @code{_XOPEN_SOURCE} has the value @math{500} this includes
94all functionality described so far plus some new definitions from the
95Single Unix Specification, @w{version 2}.
96@end defvr
97
98@comment (NONE)
99@comment X/Open
100@defvr Macro _LARGEFILE_SOURCE
101If this macro is defined some extra functions are available which
102rectify a few shortcomings in all previous standards. Specifically,
103the functions @code{fseeko} and @code{ftello} are available. Without
104these functions the difference between the @w{ISO C} interface
105(@code{fseek}, @code{ftell}) and the low-level POSIX interface
106(@code{lseek}) would lead to problems.
107
108This macro was introduced as part of the Large File Support extension (LFS).
109@end defvr
110
111@comment (NONE)
112@comment X/Open
113@defvr Macro _LARGEFILE64_SOURCE
114If you define this macro an additional set of functions is made available
115which enables @w{32 bit} systems to use files of sizes beyond
116the usual limit of 2GB. This interface is not available if the system
117does not support files that large. On systems where the natural file
118size limit is greater than 2GB (i.e., on @w{64 bit} systems) the new
119functions are identical to the replaced functions.
120
121The new functionality is made available by a new set of types and
122functions which replace the existing ones. The names of these new objects
123contain @code{64} to indicate the intention, e.g., @code{off_t}
124vs. @code{off64_t} and @code{fseeko} vs. @code{fseeko64}.
125
126This macro was introduced as part of the Large File Support extension
127(LFS). It is a transition interface for the period when @w{64 bit}
128offsets are not generally used (see @code{_FILE_OFFSET_BITS}).
129@end defvr
130
131@comment (NONE)
132@comment X/Open
133@defvr Macro _FILE_OFFSET_BITS
134This macro determines which file system interface shall be used, one
135replacing the other. Whereas @code{_LARGEFILE64_SOURCE} makes the @w{64
136bit} interface available as an additional interface,
137@code{_FILE_OFFSET_BITS} allows the @w{64 bit} interface to
138replace the old interface.
139
140If @code{_FILE_OFFSET_BITS} is undefined, or if it is defined to the
141value @code{32}, nothing changes. The @w{32 bit} interface is used and
142types like @code{off_t} have a size of @w{32 bits} on @w{32 bit}
143systems.
144
145If the macro is defined to the value @code{64}, the large file interface
146replaces the old interface. I.e., the functions are not made available
147under different names (as they are with @code{_LARGEFILE64_SOURCE}).
148Instead the old function names now reference the new functions, e.g., a
149call to @code{fseeko} now indeed calls @code{fseeko64}.
150
151This macro should only be selected if the system provides mechanisms for
152handling large files. On @w{64 bit} systems this macro has no effect
153since the @code{*64} functions are identical to the normal functions.
154
155This macro was introduced as part of the Large File Support extension
156(LFS).
157@end defvr
158
159@comment (none)
160@comment GNU
161@defvr Macro _ISOC99_SOURCE
162Until the revised @w{ISO C} standard is widely adopted the new features
163are not automatically enabled. @Theglibc{} nevertheless has a complete
164implementation of the new standard and to enable the new features the
165macro @code{_ISOC99_SOURCE} should be defined.
166@end defvr
167
168@comment (none)
169@comment GNU
170@defvr Macro _GNU_SOURCE
171If you define this macro, everything is included: @w{ISO C89}, @w{ISO
172C99}, POSIX.1, POSIX.2, BSD, SVID, X/Open, LFS, and GNU extensions. In
173the cases where POSIX.1 conflicts with BSD, the POSIX definitions take
174precedence.
175@end defvr
176
177@comment (none)
178@comment GNU
179@defvr Macro _DEFAULT_SOURCE
180If you define this macro, most features are included apart from
181X/Open, LFS and GNU extensions: the effect is to enable features from
182the 2008 edition of POSIX, as well as certain BSD and SVID features
183without a separate feature test macro to control them. Defining this
184macro, on its own and without using compiler options such as
185@option{-ansi} or @option{-std=c99}, has the same effect as not
186defining any feature test macros; defining it together with other
187feature test macros, or when options such as @option{-ansi} are used,
188enables those features even when the other options would otherwise
189cause them to be disabled.
190@end defvr
191
192@comment (none)
193@comment GNU
194@defvr Macro _REENTRANT
195@defvrx Macro _THREAD_SAFE
196If you define one of these macros, reentrant versions of several functions get
197declared. Some of the functions are specified in POSIX.1c but many others
198are only available on a few other systems or are unique to @theglibc{}.
199The problem is the delay in the standardization of the thread safe C library
200interface.
201
202Unlike on some other systems, no special version of the C library must be
203used for linking. There is only one version but while compiling this
204it must have been specified to compile as thread safe.
205@end defvr
206
207We recommend you use @code{_GNU_SOURCE} in new programs. If you don't
208specify the @samp{-ansi} option to GCC, or other conformance options
209such as @option{-std=c99}, and don't define any of these macros
210explicitly, the effect is the same as defining @code{_DEFAULT_SOURCE}
211to 1.
212
213When you define a feature test macro to request a larger class of features,
214it is harmless to define in addition a feature test macro for a subset of
215those features. For example, if you define @code{_POSIX_C_SOURCE}, then
216defining @code{_POSIX_SOURCE} as well has no effect. Likewise, if you
217define @code{_GNU_SOURCE}, then defining either @code{_POSIX_SOURCE} or
218@code{_POSIX_C_SOURCE} as well has no effect.