我正在使用带有 gcc 的 x86_64 GNU/Linux。
man -s2 open
的 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);
现在,当我尝试编译以下代码片段时,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;
}
那么 types.h
和 stat.h
是可选的吗?它们在 open()
联机帮助页中的作用是什么?
最佳答案
手册页作为对程序员和编译器制造商的指导。
您可能不需要将它们包含在您的系统中。但是,手册页描述了一种使用这些方法的可移植方式,因此您无论如何都应该包括它们。
关于c - open()系统调用头文件要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28608860/