Feature test macros
From NetBSD Wiki
Feature test macros are defined in C or C++ source files or on the command line to the compiler. They indicate the environment in which the source file is compiled. The most commonly known feature test macros are _POSIX_SOURCE, _XOPEN_SOURCE, _GNU_SOURCE.
From the standard's point of view, it is clearly defined who may define the macros and who may use them: They are defined by »the application« before the first system header is included. Then, the system headers decide which features to provide, based on these macros.
There is a practical problem with these feature macros, which is that »the application« is a quite broad term. For example, in a pkgsrc environment, does that mean:
- the pkgsrc user, in mk.conf,
- the package maintainer, in a package Makefile,
- the developers of a package, in the configure script,
- the developer of a single source code file,
- the developer of a single header file,
- someone else?
I have come up against this problem when building packages on Solaris, with USE_LANGUAGES=c99. The C99 compiler requires that _XOPEN_SOURCE is set to 600. So far, there is no problem. But that comes up when buildling packages that also define that macro, especially when they use a different value (500) for it.
Another problem is that _XOPEN_SOURCE_EXTENDED must not be defined in C99 mode, or the feature set from XPG4 will be selected instead of the one of XPG6.
How can that conflict be resolved? Maybe the feature macros should never be defined unconditionally but always be surrounded by proper #ifndef ... #endif directives. This follows the principle of providing a useful default value but let the »user« override it if he needs. The »user« in this case is not the software developer anymore, but the package maintainer or the pkgsrc user. One less problem. :)
See also
- http://www.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_02.html
- http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
- http://www.die.net/doc/linux/man/man7/feature_test_macros.7.html
- http://bama.ua.edu/cgi-bin/man-cgi?standards+5
- http://h71000.www7.hp.com/doc/83final/5763/5763pro_003.html
