C语言 -D_XOPEN_SOURCE 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5378778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does -D_XOPEN_SOURCE do/mean?
提问by anio
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.
我最近遇到了一些没有这个参数 gcc 将无法编译的代码。我检查了 gcc 手册页,但没有找到这个特定选项。我确实找到了XOPEN_SOURCE,但几乎没有解释它的作用。
Can someone please elaborate? I know -D_XOPEN_SOURCEcan be set to different values, such 400, 600, but what do those do?
有人可以详细说明吗?我知道-D_XOPEN_SOURCE可以设置为不同的值,例如400, 600,但那些有什么作用?
回答by Mikel
When you do
当你做
#define _XOPEN_SOURCE <some number>
or
或者
cc -D_XOPEN_SOURCE=<some number>
it tells your compiler to include definitions for some extra functions that are defined in the X/Open and POSIX standards.
它告诉您的编译器包含一些在 X/Open 和 POSIX 标准中定义的额外函数的定义。
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.
这将为您提供一些在最新的 UNIX/BSD/Linux 系统上存在的额外功能,但在其他系统(如 Windows)上可能不存在。
The numbers refer to different versions of the standard.
这些数字指的是标准的不同版本。
- 500 - X/Open 5, incorporating POSIX 1995
- 600 - X/Open 6, incorporating POSIX 2004
- 700 - X/Open 7, incorporating POSIX 2008
- 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.
您可以通过查看您调用的每个函数的手册页来判断您需要哪一个(如果有)。
For example, man strdupsays:
例如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
at the top of your source file before doing any #includes if you want to use strdup.
在源文件中做任何之前的顶部#include,如果你想使用s strdup。
Or you could put
或者你可以把
#define _GNU_SOURCE
there instead, which enables all functionality, with the downside that it might not compile on Solaris, FreeBSD, Mac OS X, etc.
相反,它启用了所有功能,但缺点是它可能无法在 Solaris、FreeBSD、Mac OS X 等上编译。
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 #defines you have, for example with basename(3).
在执行 a #include、#define或使用新函数之前检查每个手册页是个好主意,因为有时它们的行为会根据#define您拥有的选项和s 而改变,例如basename(3)。
See also:
也可以看看:
回答by davrieb
-Dis a c compiler option to define a preprocessor variable. In this case _XOPEN_SOURCE.
-D是用于定义预处理器变量的 ac 编译器选项。在这种情况下_XOPEN_SOURCE。
This doesn't actually affect the behavior of the compiler itself, but rather changes how some libraries, e.g. the standard c library, behave. There are several options like this. In most cases they are in relation to some standard document about some UNIX specific programming interface, or some specific library vendor.
这实际上并不影响编译器本身的行为,而是改变了某些库(例如标准 c 库)的行为方式。有几个这样的选择。在大多数情况下,它们与有关某些 UNIX 特定编程接口或某些特定库供应商的某些标准文档有关。
Defining one of them is sometimes necessary, because the behavior of some standard functions or even their signature can differ between standards. So you might have to use -D_XOPEN_SOURCEor something similar to turn on a compatibility mode.
有时需要定义其中之一,因为某些标准函数的行为甚至它们的签名可能因标准而异。所以你可能必须使用-D_XOPEN_SOURCE或类似的东西来打开兼容模式。
Another possible usage of these flags is to make sure your source code stays within the limits of a certain standard, by turning of extensions offered by your C library implementation. This is one of the measures you could use to make sure that your code runs on as many platforms as possible.
这些标志的另一个可能用途是通过转换由 C 库实现提供的扩展来确保您的源代码保持在某个标准的限制内。这是您可以用来确保您的代码在尽可能多的平台上运行的措施之一。

