-D_XOPEN_SOURCE 的作用/含义是什么?

发布于 2024-10-25 07:02:14 字数 205 浏览 1 评论 0原文

我最近遇到一些代码,如果没有这个参数,gcc 将无法编译。我检查了 gcc 手册页,但没有找到这个特定选项。我确实找到了 XOPEN_SOURCE,但几乎没有解释它的作用。

有人可以详细说明一下吗?我知道 -D_XOPEN_SOURCE 可以设置为不同的值,例如 400600,但是它们有什么作用呢?

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.

Can someone please elaborate? I know -D_XOPEN_SOURCE can be set to different values, such 400, 600, but what do those do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Bonjour°[大白 2024-11-01 07:02:15

当您

#define _XOPEN_SOURCE <some number>

这样做时,

cc -D_XOPEN_SOURCE=<some number>

它会告诉您的编译器包含 X/Open 和 POSIX 标准中定义的一些额外函数的定义。

这将为您提供一些在最新的 UNIX/BSD/Linux 系统上存在的额外功能,但在其他系统(例如 Windows)上可能不存在。

这些数字指的是该标准的不同版本。

您可以通过查看您调用的每个函数的手册页来判断您需要哪一个(如果有)。

例如,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

这意味着

#define _SVID_SOURCE
#define _BSD_SOURCE
#define _XOPEN_SOURCE 500
#define _XOPEN_SOURCE 600
#define _XOPEN_SOURCE 700

如果您想使用 <,您应​​该在执行任何 #include 之前将以下内容之一放在源文件的顶部 :代码>strdup。

或者您也可以放在

#define _GNU_SOURCE

那里,这样可以启用所有功能,但缺点是它可能无法在 Solaris、FreeBSD、Mac OS X 等上编译。

最好在执行 #include、#define 或使用新函数,因为有时它们的行为会根据您拥有的选项和 #define 发生变化,例如使用 basename(3)

另请参阅:

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.

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.

You can tell which one you need (if any) by looking at the man page for each function you call.

For example, man strdup says:

   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.

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.

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).

See also:

蓝天 2024-11-01 07:02:15

-D 是用于定义预处理器变量的编译器选项。在本例中_XOPEN_SOURCE

这实际上并不影响编译器本身的行为,而是改变了一些库(例如标准 c 库)的行为方式。像这样的选项有好几种。在大多数情况下,它们与有关某些 UNIX 特定编程接口或某些特定库供应商的标准文档相关。

有时需要定义其中之一,因为某些标准函数的行为甚至它们的签名在不同标准之间可能有所不同。因此,您可能必须使用 -D_XOPEN_SOURCE 或类似的东西来打开兼容模式。

这些标志的另一种可能用途是通过打开 C 库实现提供的扩展来确保您的源代码保持在特定标准的限制内。这是您可以用来确保您的代码在尽可能多的平台上运行的措施之一。

-D is a c compiler option to define a preprocessor variable. In this case _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.

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_SOURCE or something similar to turn on a compatibility mode.

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.

亚希 2024-11-01 07:02:15

这将标头公开为属于给定规范的定义,例如 posix。
它所属的实际范数由值定义(例如此处为 400 或 600)。
请参阅此参考了解范数/值绑定。

This exposes the header to belong to a definition of a given norm, such as posix.
The actual norm it belongs to is defined by the value (here 400 or 600 for instance).
See this Reference for the norm/value binding.

比忠 2024-11-01 07:02:15

由于某些未知的原因,Mac OS/X (XCode) 需要 600 来定义 strdup(),即使它是在 1995 年规范中的。 Mozilla 和 其他 遇到了这个问题...

For some unknown reason, Mac OS/X (XCode) requires 600 to define strdup(), even though it's in the 1995 specification. Mozilla and others have run into this...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文