-D_XOPEN_SOURCE 是什么意思? [英] What does -D_XOPEN_SOURCE do/mean?
问题描述
我最近遇到了一些没有这个参数 gcc 将无法编译的代码.我检查了 gcc 手册页,但没有找到这个特定选项.我确实找到了 XOPEN_SOURCE
,但几乎没有解释它的作用.
I recently encountered some code that gcc would not compile without this arg. I checked the gcc man page, but did not find this specific option. I did find XOPEN_SOURCE
, but there was little explanation of what it does.
有人能详细说明一下吗?我知道 -D_XOPEN_SOURCE
可以设置为不同的值,例如 400
、600
,但它们有什么作用?
Can someone please elaborate? I know -D_XOPEN_SOURCE
can be set to different values, such 400
, 600
, but what do those do?
推荐答案
什么时候做
#define _XOPEN_SOURCE <some number>
或
cc -D_XOPEN_SOURCE=<some number>
它告诉您的编译器包含一些在 X/Open 和 POSIX 标准中定义的额外函数的定义.
it tells your compiler to include definitions for some extra functions that are defined in the X/Open and POSIX standards.
这将为您提供一些在最新的 UNIX/BSD/Linux 系统上存在的额外功能,但在其他系统(如 Windows)上可能不存在.
This will give you some extra functionality that exists on most recent UNIX/BSD/Linux systems, but probably doesn't exist on other systems such as Windows.
数字代表标准的不同版本.
The numbers refer to different versions of the standard.
- 500 - X/Open 5,结合 POSIX 1995
- 600 - X/Open 6,结合 POSIX 2004
- 700 - X/Open 7,结合 POSIX 2008
您可以通过查看您调用的每个函数的手册页来判断您需要哪个函数(如果有).
You can tell which one you need (if any) by looking at the man page for each function you call.
例如,man strdup
说:
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
strdup(): _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500
strndup(), strdupa(), strndupa(): _GNU_SOURCE
这意味着您应该输入其中之一:
Which means that you should put one of these:
#define _SVID_SOURCE
#define _BSD_SOURCE
#define _XOPEN_SOURCE 500
#define _XOPEN_SOURCE 600
#define _XOPEN_SOURCE 700
如果您想使用 strdup
,请在执行任何 #include
之前在源文件的顶部.
at the top of your source file before doing any #include
s if you want to use strdup
.
或者你可以把
#define _GNU_SOURCE
相反,它启用了所有功能,但缺点是它可能无法在 Solaris、FreeBSD、Mac OS X 等上编译.
there instead, which enables all functionality, with the downside that it might not compile on Solaris, FreeBSD, Mac OS X, etc.
在执行 #include
、#define
或使用新函数之前检查每个手册页是个好主意,因为有时它们的行为会根据选项发生变化和 #define
s,例如 basename(3).
It's a good idea to check each man page before doing a #include
, #define
, or using a new function, because sometimes their behavior changes depending on what options and #define
s you have, for example with basename(3).
另见:
- Linux:gcc with -std=c99 抱怨不知道 struct timespec
- glibc 功能测试宏
- 编译环境 - 开放组基础规范问题 6(又名 X/Open 6))
- POSIX - 维基百科
- 单一 UNIX 规范 - 维基百科
这篇关于-D_XOPEN_SOURCE 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!