Question

For example, take open(2), which has the following synopsis:

#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);

Should I include all those header files or is any one of those fine? In the former case, how do I know which header files contain which functions, macros and typedefs?

Was it helpful?

Solution

You should include all of them. The POSIX specification will tell you what's in each (for instance, this is the POSIX specification for fcntl.h), or at least what's guaranteed to be in each.

OTHER TIPS

You should include all these header files. The headers mentioned in the synopsis are all supposed to be included.

AFAIK, you should include all those header files. Why do you need to know which header file contains macros, functions or typedefs, if you eventually include all of them?

Only <fcntl.h> is required. There are two man pages for open. http://linux.die.net/man/2/open and http://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);

If you use the overload with mode_t, you need <sys/types.h>.

#include <sys/stat.h>
#include <fcntl.h>

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

For the latter overload, opengroup.org considers <sys/stat.h> optional. Also, <sys/types.h> is not required either.

The requirement to include <sys/types.h> has been removed. Although was required for conforming implementations of previous POSIX specifications, it was not required for UNIX applications.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top