为什么不应该包括 features.h?

Why not should you not include features.h?

我正在阅读 linux features test macro page 并且在注释部分中指出:

<features.h> is a Linux/glibc-specific header file. Other systems have an analogous file, but typically with a different name. This header file is automatically included by other header files as required: it is not necessary to explicitly include it in order to employ feature test macros.

我知道其他系统不会有与 features.h 相同的文件名,因此您不应该直接包含它。但是,如果你不包含头文件,你怎么知道你是否可以使用功能测试宏?

glibc 保证 #define 记录的特征选择宏(_POSIX_C_SOURCE_XOPEN_SOURCE_GNU_SOURCE 等)只要生效就会得到遵守在第一次包含 any public C 库 header 之前。例如

/* file header comment */
#define _XOPEN_SOURCE 700 // first non-comment line of the file
#include <unistd.h>       // first #include in the file
// ... more code here ...

保证 unistd.h 声明属于 POSIX.1-2008 的内容,包括 XSI 扩展。

这个保证的实现涉及features.h,但是features.h不是一个public C库header。直接包含 features.h 不是你应该做的事情,我们(glibc 开发者)不保证这个 header 甚至会在未来继续存在。 (例如,未来的版本可能会将其合并到 bits/libc-header-start.h。)