How can adding a header increase portability? (sys

2019-07-15 09:04发布

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?

2条回答
迷人小祖宗
2楼-- · 2019-07-15 09:32

It's because the BSD's require it:

FreeBSD and OpenBSD need sys/time.h, not just time.h, before sys/resource.h.

See this page on UNIX Portability for details.

查看更多
聊天终结者
3楼-- · 2019-07-15 09:49

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.

查看更多
登录 后发表回答