添加标头如何提高可移植性? (系统/时间.h)
我刚刚注意到 getrusage 手册页中的这一行:
如今不再需要包含
,但可以提高可移植性。 (事实上,struct timeval 是在
中定义的)
什么?由于 struct rusage 包含 struct timeval 作为成员,那么 sys/resource.h 肯定必须包含 sys/time.h,否则该类型将不完整且无法使用?
这个评论怎么可能有意义呢?怎么可能没有必要呢?可移植性如何得到帮助?
I just noticed this line in the getrusage man page:
Including
<sys/time.h>
is not required these days, but increases portability. (Indeed, struct timeval is defined in<sys/time.h>
)
What? Since struct rusage
contains struct timeval
as a member, surely sys/resource.h must include sys/time.h or the type would be incomplete and unusable?
How could this comment ever have made sense? How could it ever have not been necessary? How could portability have ever been helped?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,在 C 的早期,您必须在头文件 B 之前手动包含头文件 A 的情况并不少见。也许早期版本的预处理器无法执行嵌套包含,或者可能只是风格上的问题(联机帮助页)通常会直接包含相关结构的头文件)。
“现在”,sys/resource.h 必须包含 sys/time.h 或重复 struct timeval 的定义,但并非每个系统都完全遵循标准。
In general, it was not uncommon in the early days of C for you to have to manually include header file A before header file B. Maybe an early version of the preprocessor couldn't do nested includes, or maybe it was just stylistic (manpages would often directly include the header files for relevant structures).
"These days", sys/resource.h must either include sys/time.h or repeat the definition of struct timeval, but not every system follows the standard completely.
这是因为 BSD 要求它:
有关详细信息,请参阅有关 UNIX 可移植性的页面。
It's because the BSD's require it:
See this page on UNIX Portability for details.