open()系统调用头文件要求

roo*_*kea 6 c manpage system-calls header-files

我正在使用x86_64 GNU/Linux与gcc.
大纲部分man -s2 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);
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试编译以下代码片段时,gcc不会抛出警告/错误.

#include <stdio.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
    int fd;

    fd = open("foo.txt", O_RDWR, 0777);
    if(fd == -1)
        perror("open");

    fd = creat("foo.txt", 0777);
    if(fd == -1)
        perror("creat");

    close(fd);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

那么,types.hstat.h可选的?他们在manpage中的目的是什么open()

Kla*_*äck 5

手册页用作程序员和编译器制造商的指令.

您可能不需要将它们包含在系统中.但是,手册页描述了使用这些方法的可移植方式,因此无论如何都应该包含它们.

  • @rootkea:一般情况下**会有所不同.正如KlasLindbäck所说,该联机帮助页指定了如何*可移植*,你应该坚持它(因为毕竟,如果不需要那些额外的包含它不需要任何费用;但是你的代码将无法编译,或者更糟糕的是,如果*需要*并且你没有包含,将调用UB). (3认同)