Blame manual/creature.texi

Packit 6c4009
@node Feature Test Macros
Packit 6c4009
@subsection Feature Test Macros
Packit 6c4009
Packit 6c4009
@cindex feature test macros
Packit 6c4009
The exact set of features available when you compile a source file
Packit 6c4009
is controlled by which @dfn{feature test macros} you define.
Packit 6c4009
Packit 6c4009
If you compile your programs using @samp{gcc -ansi}, you get only the
Packit 6c4009
@w{ISO C} library features, unless you explicitly request additional
Packit 6c4009
features by defining one or more of the feature macros.
Packit 6c4009
@xref{Invoking GCC,, GNU CC Command Options, gcc, The GNU CC Manual},
Packit 6c4009
for more information about GCC options.@refill
Packit 6c4009
Packit 6c4009
You should define these macros by using @samp{#define} preprocessor
Packit 6c4009
directives at the top of your source code files.  These directives
Packit 6c4009
@emph{must} come before any @code{#include} of a system header file.  It
Packit 6c4009
is best to make them the very first thing in the file, preceded only by
Packit 6c4009
comments.  You could also use the @samp{-D} option to GCC, but it's
Packit 6c4009
better if you make the source files indicate their own meaning in a
Packit 6c4009
self-contained way.
Packit 6c4009
Packit 6c4009
This system exists to allow the library to conform to multiple standards.
Packit 6c4009
Although the different standards are often described as supersets of each
Packit 6c4009
other, they are usually incompatible because larger standards require
Packit 6c4009
functions with names that smaller ones reserve to the user program.  This
Packit 6c4009
is not mere pedantry --- it has been a problem in practice.  For instance,
Packit 6c4009
some non-GNU programs define functions named @code{getline} that have
Packit 6c4009
nothing to do with this library's @code{getline}.  They would not be
Packit 6c4009
compilable if all features were enabled indiscriminately.
Packit 6c4009
Packit 6c4009
This should not be used to verify that a program conforms to a limited
Packit 6c4009
standard.  It is insufficient for this purpose, as it will not protect you
Packit 6c4009
from including header files outside the standard, or relying on semantics
Packit 6c4009
undefined within the standard.
Packit 6c4009
Packit 6c4009
@defvr Macro _POSIX_SOURCE
Packit 6c4009
@standards{POSIX.1, (none)}
Packit 6c4009
If you define this macro, then the functionality from the POSIX.1
Packit 6c4009
standard (IEEE Standard 1003.1) is available, as well as all of the
Packit 6c4009
@w{ISO C} facilities.
Packit 6c4009
Packit 6c4009
The state of @code{_POSIX_SOURCE} is irrelevant if you define the
Packit 6c4009
macro @code{_POSIX_C_SOURCE} to a positive integer.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro _POSIX_C_SOURCE
Packit 6c4009
@standards{POSIX.2, (none)}
Packit 6c4009
Define this macro to a positive integer to control which POSIX
Packit 6c4009
functionality is made available.  The greater the value of this macro,
Packit 6c4009
the more functionality is made available.
Packit 6c4009
Packit 6c4009
If you define this macro to a value greater than or equal to @code{1},
Packit 6c4009
then the functionality from the 1990 edition of the POSIX.1 standard
Packit 6c4009
(IEEE Standard 1003.1-1990) is made available.
Packit 6c4009
Packit 6c4009
If you define this macro to a value greater than or equal to @code{2},
Packit 6c4009
then the functionality from the 1992 edition of the POSIX.2 standard
Packit 6c4009
(IEEE Standard 1003.2-1992) is made available.
Packit 6c4009
Packit 6c4009
If you define this macro to a value greater than or equal to @code{199309L},
Packit 6c4009
then the functionality from the 1993 edition of the POSIX.1b standard
Packit 6c4009
(IEEE Standard 1003.1b-1993) is made available.
Packit 6c4009
Packit 6c4009
If you define this macro to a value greater than or equal to
Packit 6c4009
@code{199506L}, then the functionality from the 1995 edition of the
Packit 6c4009
POSIX.1c standard (IEEE Standard 1003.1c-1995) is made available.
Packit 6c4009
Packit 6c4009
If you define this macro to a value greater than or equal to
Packit 6c4009
@code{200112L}, then the functionality from the 2001 edition of the
Packit 6c4009
POSIX standard (IEEE Standard 1003.1-2001) is made available.
Packit 6c4009
Packit 6c4009
If you define this macro to a value greater than or equal to
Packit 6c4009
@code{200809L}, then the functionality from the 2008 edition of the
Packit 6c4009
POSIX standard (IEEE Standard 1003.1-2008) is made available.
Packit 6c4009
Packit 6c4009
Greater values for @code{_POSIX_C_SOURCE} will enable future extensions.
Packit 6c4009
The POSIX standards process will define these values as necessary, and
Packit 6c4009
@theglibc{} should support them some time after they become standardized.
Packit 6c4009
The 1996 edition of POSIX.1 (ISO/IEC 9945-1: 1996) states that
Packit 6c4009
if you define @code{_POSIX_C_SOURCE} to a value greater than
Packit 6c4009
or equal to @code{199506L}, then the functionality from the 1996
Packit 6c4009
edition is made available.  In general, in @theglibc{}, bugfixes to
Packit 6c4009
the standards are included when specifying the base version; e.g.,
Packit 6c4009
POSIX.1-2004 will always be included with a value of @code{200112L}.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro _XOPEN_SOURCE
Packit 6c4009
@defvrx Macro _XOPEN_SOURCE_EXTENDED
Packit 6c4009
@standards{X/Open, (none)}
Packit 6c4009
If you define this macro, functionality described in the X/Open
Packit 6c4009
Portability Guide is included.  This is a superset of the POSIX.1 and
Packit 6c4009
POSIX.2 functionality and in fact @code{_POSIX_SOURCE} and
Packit 6c4009
@code{_POSIX_C_SOURCE} are automatically defined.
Packit 6c4009
Packit 6c4009
As the unification of all Unices, functionality only available in
Packit 6c4009
BSD and SVID is also included.
Packit 6c4009
Packit 6c4009
If the macro @code{_XOPEN_SOURCE_EXTENDED} is also defined, even more
Packit 6c4009
functionality is available.  The extra functions will make all functions
Packit 6c4009
available which are necessary for the X/Open Unix brand.
Packit 6c4009
Packit 6c4009
If the macro @code{_XOPEN_SOURCE} has the value @math{500} this includes
Packit 6c4009
all functionality described so far plus some new definitions from the
Packit 6c4009
Single Unix Specification, @w{version 2}.  The value @math{600}
Packit 6c4009
(corresponding to the sixth revision) includes definitions from SUSv3,
Packit 6c4009
and using @math{700} (the seventh revision) includes definitions from
Packit 6c4009
SUSv4.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro _LARGEFILE_SOURCE
Packit 6c4009
@standards{X/Open, (NONE)}
Packit 6c4009
If this macro is defined some extra functions are available which
Packit 6c4009
rectify a few shortcomings in all previous standards.  Specifically,
Packit 6c4009
the functions @code{fseeko} and @code{ftello} are available.  Without
Packit 6c4009
these functions the difference between the @w{ISO C} interface
Packit 6c4009
(@code{fseek}, @code{ftell}) and the low-level POSIX interface
Packit 6c4009
(@code{lseek}) would lead to problems.
Packit 6c4009
Packit 6c4009
This macro was introduced as part of the Large File Support extension (LFS).
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro _LARGEFILE64_SOURCE
Packit 6c4009
@standards{X/Open, (NONE)}
Packit 6c4009
If you define this macro an additional set of functions is made available
Packit 6c4009
which enables @w{32 bit} systems to use files of sizes beyond
Packit 6c4009
the usual limit of 2GB.  This interface is not available if the system
Packit 6c4009
does not support files that large.  On systems where the natural file
Packit 6c4009
size limit is greater than 2GB (i.e., on @w{64 bit} systems) the new
Packit 6c4009
functions are identical to the replaced functions.
Packit 6c4009
Packit 6c4009
The new functionality is made available by a new set of types and
Packit 6c4009
functions which replace the existing ones.  The names of these new objects
Packit 6c4009
contain @code{64} to indicate the intention, e.g., @code{off_t}
Packit 6c4009
vs. @code{off64_t} and @code{fseeko} vs. @code{fseeko64}.
Packit 6c4009
Packit 6c4009
This macro was introduced as part of the Large File Support extension
Packit 6c4009
(LFS).  It is a transition interface for the period when @w{64 bit}
Packit 6c4009
offsets are not generally used (see @code{_FILE_OFFSET_BITS}).
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro _FILE_OFFSET_BITS
Packit 6c4009
@standards{X/Open, (NONE)}
Packit 6c4009
This macro determines which file system interface shall be used, one
Packit 6c4009
replacing the other.  Whereas @code{_LARGEFILE64_SOURCE} makes the @w{64
Packit 6c4009
bit} interface available as an additional interface,
Packit 6c4009
@code{_FILE_OFFSET_BITS} allows the @w{64 bit} interface to
Packit 6c4009
replace the old interface.
Packit 6c4009
Packit 6c4009
If @code{_FILE_OFFSET_BITS} is undefined, or if it is defined to the
Packit 6c4009
value @code{32}, nothing changes.  The @w{32 bit} interface is used and
Packit 6c4009
types like @code{off_t} have a size of @w{32 bits} on @w{32 bit}
Packit 6c4009
systems.
Packit 6c4009
Packit 6c4009
If the macro is defined to the value @code{64}, the large file interface
Packit 6c4009
replaces the old interface.  I.e., the functions are not made available
Packit 6c4009
under different names (as they are with @code{_LARGEFILE64_SOURCE}).
Packit 6c4009
Instead the old function names now reference the new functions, e.g., a
Packit 6c4009
call to @code{fseeko} now indeed calls @code{fseeko64}.
Packit 6c4009
Packit 6c4009
This macro should only be selected if the system provides mechanisms for
Packit 6c4009
handling large files.  On @w{64 bit} systems this macro has no effect
Packit 6c4009
since the @code{*64} functions are identical to the normal functions.
Packit 6c4009
Packit 6c4009
This macro was introduced as part of the Large File Support extension
Packit 6c4009
(LFS).
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro _ISOC99_SOURCE
Packit 6c4009
@standards{GNU, (none)}
Packit 6c4009
If this macro is defined, features from ISO C99 are included.  Since
Packit 6c4009
these features are included by default, this macro is mostly relevant
Packit 6c4009
when the compiler uses an earlier language version.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro _ISOC11_SOURCE
Packit 6c4009
@standards{C11, (none)}
Packit 6c4009
If this macro is defined, ISO C11 extensions to ISO C99 are included.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro __STDC_WANT_LIB_EXT2__
Packit 6c4009
@standards{ISO, (none)}
Packit 6c4009
If you define this macro to the value @code{1}, features from ISO/IEC
Packit 6c4009
TR 24731-2:2010 (Dynamic Allocation Functions) are enabled.  Only some
Packit 6c4009
of the features from this TR are supported by @theglibc{}.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro __STDC_WANT_IEC_60559_BFP_EXT__
Packit 6c4009
@standards{ISO, (none)}
Packit 6c4009
If you define this macro, features from ISO/IEC TS 18661-1:2014
Packit 6c4009
(Floating-point extensions for C: Binary floating-point arithmetic)
Packit 6c4009
are enabled.  Only some of the features from this TS are supported by
Packit 6c4009
@theglibc{}.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro __STDC_WANT_IEC_60559_FUNCS_EXT__
Packit 6c4009
@standards{ISO, (none)}
Packit 6c4009
If you define this macro, features from ISO/IEC TS 18661-4:2015
Packit 6c4009
(Floating-point extensions for C: Supplementary functions) are
Packit 6c4009
enabled.  Only some of the features from this TS are supported by
Packit 6c4009
@theglibc{}.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro __STDC_WANT_IEC_60559_TYPES_EXT__
Packit 6c4009
@standards{ISO, (none)}
Packit 6c4009
If you define this macro, features from ISO/IEC TS 18661-3:2015
Packit 6c4009
(Floating-point extensions for C: Interchange and extended types) are
Packit 6c4009
enabled.  Only some of the features from this TS are supported by
Packit 6c4009
@theglibc{}.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro _GNU_SOURCE
Packit 6c4009
@standards{GNU, (none)}
Packit 6c4009
If you define this macro, everything is included: @w{ISO C89}, @w{ISO
Packit 6c4009
C99}, POSIX.1, POSIX.2, BSD, SVID, X/Open, LFS, and GNU extensions.  In
Packit 6c4009
the cases where POSIX.1 conflicts with BSD, the POSIX definitions take
Packit 6c4009
precedence.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro _DEFAULT_SOURCE
Packit 6c4009
@standards{GNU, (none)}
Packit 6c4009
If you define this macro, most features are included apart from
Packit 6c4009
X/Open, LFS and GNU extensions: the effect is to enable features from
Packit 6c4009
the 2008 edition of POSIX, as well as certain BSD and SVID features
Packit 6c4009
without a separate feature test macro to control them.
Packit 6c4009
Packit 6c4009
Be aware that compiler options also affect included features:
Packit 6c4009
Packit 6c4009
@itemize
Packit 6c4009
@item
Packit 6c4009
If you use a strict conformance option, features beyond those from the
Packit 6c4009
compiler's language version will be disabled, though feature test
Packit 6c4009
macros may be used to enable them.
Packit 6c4009
Packit 6c4009
@item
Packit 6c4009
Features enabled by compiler options are not overridden by feature
Packit 6c4009
test macros.
Packit 6c4009
@end itemize
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro _ATFILE_SOURCE
Packit 6c4009
@standards{GNU, (none)}
Packit 6c4009
If this macro is defined, additional @code{*at} interfaces are
Packit 6c4009
included.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro _FORTIFY_SOURCE
Packit 6c4009
@standards{GNU, (none)}
Packit 6c4009
If this macro is defined to @math{1}, security hardening is added to
Packit 6c4009
various library functions.  If defined to @math{2}, even stricter
Packit 6c4009
checks are applied.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
@defvr Macro _REENTRANT
Packit 6c4009
@defvrx Macro _THREAD_SAFE
Packit 6c4009
@standards{Obsolete, (none)}
Packit 6c4009
These macros are obsolete.  They have the same effect as defining
Packit 6c4009
@code{_POSIX_C_SOURCE} with the value @code{199506L}.
Packit 6c4009
Packit 6c4009
Some very old C libraries required one of these macros to be defined
Packit 6c4009
for basic functionality (e.g.@: @code{getchar}) to be thread-safe.
Packit 6c4009
@end defvr
Packit 6c4009
Packit 6c4009
We recommend you use @code{_GNU_SOURCE} in new programs.  If you don't
Packit 6c4009
specify the @samp{-ansi} option to GCC, or other conformance options
Packit 6c4009
such as @option{-std=c99}, and don't define any of these macros
Packit 6c4009
explicitly, the effect is the same as defining @code{_DEFAULT_SOURCE}
Packit 6c4009
to 1.
Packit 6c4009
Packit 6c4009
When you define a feature test macro to request a larger class of features,
Packit 6c4009
it is harmless to define in addition a feature test macro for a subset of
Packit 6c4009
those features.  For example, if you define @code{_POSIX_C_SOURCE}, then
Packit 6c4009
defining @code{_POSIX_SOURCE} as well has no effect.  Likewise, if you
Packit 6c4009
define @code{_GNU_SOURCE}, then defining either @code{_POSIX_SOURCE} or
Packit 6c4009
@code{_POSIX_C_SOURCE} as well has no effect.