user1804599
user1804599

Reputation:

Should I include any or all of the headers listed in the synopsis of a man page?

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?

Upvotes: 7

Views: 252

Answers (4)

user1508519
user1508519

Reputation:

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.

Upvotes: 1

mcleod_ideafix
mcleod_ideafix

Reputation: 11438

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?

Upvotes: 1

tabstop
tabstop

Reputation: 1761

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.

Upvotes: 7

A. Lefebvre
A. Lefebvre

Reputation: 139

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

Upvotes: 1

Related Questions