我应该包括手册页面概要中列出的所有头文件吗?

7
例如,我们来看一下open(2)函数,它的摘要如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);

我应该包含所有这些头文件还是其中任何一个都可以?在前一种情况下,我如何知道哪个头文件包含哪些函数、宏和typedefs?

4个回答

7
你应该包含它们中的所有内容。 POSIX规范会告诉你每个文件中包含了哪些内容(例如,这篇文章是fcntl.h的POSIX规范),或者至少保证每个文件中包含了哪些内容。

最好按照手册给出的顺序包含文件(最好是Linux上的Linux,即open(2) ...)。 - Basile Starynkevitch

1
只需要 <fcntl.h>open 有两个手册页面。http://linux.die.net/man/2/openhttp://linux.die.net/man/3/open
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);

如果您使用带有mode_t的重载函数,您需要包含<sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int open(const char *path, int oflag, ... );

对于后一种重载,opengroup.org 认为 <sys/stat.h> 是可选的。此外,<sys/types.h> 也不是必需的。
引用:

需要包含 <sys/types.h> 的要求已被删除。虽然过去的 POSIX 规范的符合实现需要它,但 UNIX 应用程序并不需要。


1
你应该包含所有这些头文件。概要中提到的头文件都应该被包含。

1
据我所知,您应该包含所有这些头文件。如果您最终包含了所有头文件,那么为什么需要知道哪个头文件包含宏、函数或typedefs?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接