I'm just wondering how __USE_MISC
works and where it's documented. I see a bunch of functions inside .h
files require it and mention it. But it seems something is actively undefining it and I would like to know is doing that and how it makes that decision?
#define __USE_MISC 1
#include <stdio.h>
int main()
{
printf("%d\n", __USE_MISC);
}
Returns
error: use of undeclared identifier '
__USE_MISC
'
That error goes away if I call it anything else, like _USE_MISC
, or USE_MISC
or foobar
.
I've tried this with clang and gcc.
Best Answer
The
__USE_*
macros are not supposed to be defined and checked by the user, they're a mechanism to implement the feature selectors (_XOPEN_SOURCE
,_GNU_SOURCE
, etc) in glibc headers.The whole mechanism is documented in a comment in the
features.h
header.The
features.h
header is included from most standard headers (eg. fromstdio.h
) and the first thing it does is to undefine all the__USE_*
macros, and then define them selectively depending on the_POSIX_C_SOURCE
,_GNU_SOURCE
, etc. macros defined by the user. Then the standard headers will ifdef things in and out based on those__USE_*
macros.One may wonder how to map back from a
__USE_*
macro to a_*_SOURCE
macro that turns it on; for instance, one may want to compile their program withgcc -ansi
, see that thepsignal()
function is only defined when the__USE_XOPEN2K8
is on, and wants to know what_*_SOURCE
selector will get them that. Here is a table, generated with a script that had rungcc -ansi -E -dM "-D$source" -include features.h
with different selectors: