-D_DEFAULT_SOURCE是做什么的? [英] What does -D_DEFAULT_SOURCE do?

查看:943
本文介绍了-D_DEFAULT_SOURCE是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,我从gcc -std=c99收到警告,提示usleep()被隐式声明.然后我偶然发现了这篇stackoverflow帖子,这使我不得不使用-D_BSD_SOURCE.但是,现在gcc告诉我-D_BSD_SOURCE已被弃用,我应该改用-D_DEFAULT_SOURCE.

Previously I was receiving warnings from gcc -std=c99 that usleep() was implicitly declared. Then I stumbled across this stackoverflow post, which led me to use -D_BSD_SOURCE. However, now gcc tells me that -D_BSD_SOURCE has been deprecated and I should use -D_DEFAULT_SOURCE instead.

#warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE"

为什么不推荐使用-D_BSD_SOURCE?为什么使用-D_DEFAULT_SOURCE代替?它是做什么的?

Why is -D_BSD_SOURCE deprecated? Why is -D_DEFAULT_SOURCE used instead? And what does it do?

我确实做了一些谷歌搜索,结果只是充满了人们使用它来关闭gcc的结果.我找不到为什么 -D_BSD_SOURCE已被弃用,就是这样.

I did some googling, and the results are just filled with people using it to shut gcc up. I couldn't find out why -D_BSD_SOURCE has been deprecated, just that it is.

推荐答案

glibc手册描述了包括_DEFAULT_SOURCE的每个功能测试宏(FTM):

The glibc manual describes each feature test macro (FTM) including _DEFAULT_SOURCE:

如果您定义此宏,除包含大多数功能外 X/Open,LFS和GNU扩展:效果是启用以下功能 POSIX 2008版,以及某些BSD和SVID功能 没有单独的功能测试宏来控制它们.定义这个 宏,无需使用编译器选项(例如-ansi-std=c99,与未定义任何功能测试宏具有相同的作用;与其他功能测试宏一起定义它,或在何时使用选项 例如-ansi,即使在其他情况下也可以启用这些功能 选项会导致它们被禁用.

If you define this macro, most features are included apart from X/Open, LFS and GNU extensions: the effect is to enable features from the 2008 edition of POSIX, as well as certain BSD and SVID features without a separate feature test macro to control them. Defining this macro, on its own and without using compiler options such as -ansi or -std=c99, has the same effect as not defining any feature test macros; defining it together with other feature test macros, or when options such as -ansi are used, enables those features even when the other options would otherwise cause them to be disabled.

有关LTM的LWN.net这篇文章为我们提供了基本原理(在其他可能有趣的信息中) :

This LWN.net article about FTMs provides us with a rationale (among other perhaps interesting info):

最初的意图似乎是在每个glibc中 使用FTM的头文件,仅__USE_*内部文件之一 宏应控制任何特定定义的公开范围. 此外,不应在嵌套的#ifdef中使用宏 指令.对glibc头文件的检查迅速显示出 现实远非意图,导致罗兰(Roland)陷入困境的情况 McGrath 提出建议,该是时候了进行大清理 事情又回到了预期的情况.罗兰(Roland)认为这项任务可以 通过消除_BSD_SOURCE_SVID_SOURCE FTM来简化, 尽管它们在历史上有其目的,但已经不再是 这些天有用.他说,不再需要唯一的宏 用于现代源代码的是那些与正式标准有关的代码,以及 _GNU_SOURCE.

The original intent seems to have been that, within each of the glibc header files that employs FTMs, only one of the __USE_* internal macros should govern the exposure of any particular definition. Additionally, the macros should not be used in nested #ifdef directives. An inspection of the glibc header files quickly shows that the reality is far from the intent, a situation that led Roland McGrath to suggest that it was time for a major cleanup to bring things back to the intended situation. Roland thought that task could be simplified by eliminating the _BSD_SOURCE and _SVID_SOURCE FTMs, which, although they had a purpose historically, have ceased to be useful these days. Anymore, he said, the only macros that are needed for modern source code are those that relate to formal standards plus _GNU_SOURCE.

Joseph Myers 已正式提供了一系列补丁实施 这项工作的第一步.保守的做法受到鼓舞 罗兰(Roland)表示_BSD_SOURCE_SVID_SOURCE FTM正在两个glibc版本中进行.版本 glibc 2.19添加了新的FTM _DEFAULT_SOURCE.定义此宏会导致默认定义被公开,即使显式 其他宏的定义将导致这种情况不会发生.效果 定义此宏等效于显式 在早期的glibc版本中定义了三个宏:

Joseph Myers duly obliged with a series of patches to implement the first steps in this work. The conservative approach encouraged by Roland meant that the deprecation of the _BSD_SOURCE and _SVID_SOURCE FTMs is taking place across two glibc versions. Version 2.19 of glibc added a new FTM, _DEFAULT_SOURCE. Defining this macro causes the default definitions to be exposed even when the explicit definition of other macros would cause that not to happen. The effect of defining this macro is equivalent to the effect of explicitly defining three macros in earlier glibc versions:

cc -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809C

因此,如果您需要定义_BSD_SOURCE_SVID_SOURCE,也只需定义_DEFAULT_SOURCE.如果glibc版本< = 2.18不在乎,则版本== 2.19则不会警告是否同时定义了这三个或全部.

So if you need to define _BSD_SOURCE or _SVID_SOURCE, simply define _DEFAULT_SOURCE too. glibc versions <= 2.18 don't care about it and versions >= 2.19 don't warn if both or all three are defined.

这篇关于-D_DEFAULT_SOURCE是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆