Linux编程中 #define _XOPEN_SOURCE的作用

[误解]
#define _XOPEN_SOURCE
决不是简单的宏定义
它是使程序符合系统环境的不可缺少的部分

[概念]
Glibc 所实现全部或部分规范下的功能有:
1.ISO C: C语言国际标准.
2.POSIX: 操作系统的 ISO/IEC 9945 (aka IEEE 1003) 标准.
3.Berkeley Unix: BSD 和 SunOS.
4.SVID: V 系统接口描述.
5.XPG: The X/Open Portability Guide.

程序中,为了实现上述功能需要定义对应的宏。如第二个功能POSIX,应定义 _POSIX_SOURCE。类似的有 _BSD_SOURCE、_SVID_SOURCE、_XOPEN_SOURCE。
也就是说:
#define _XOPEN_SOURCE
是为了可以使用 5. The X/Open Portability Guide 的功能。

[实用方法]
使用上述五种功能的方法有两种:
    1)在cc命令中指定,如:cc -D _POSIX_SOURCE file.c
    2)将源程序的第一行设置为:#define _POSIX_SOURCE 1

[_XOPEN_SOURCE是什么?]
翻译成汉语表达很蹩脚,英文原版解释如下:
Macro: _XOPEN_SOURCE
If you define this macro, functionality described in the X/Open Portability Guide is included. This is a superset of the POSIX.1 and POSIX.2 functionality and in fact _POSIX_SOURCE and _POSIX_C_SOURCE are automatically defined.
As the unification of all Unices, functionality only available in BSD and SVID is also included.
If the macro _XOPEN_SOURCE_EXTENDED is also defined, even more functionality is available. The exa functions will make

相关推荐