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

标签 c header-files system-calls manpage

我正在使用带有 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.hstat.h 是可选的吗?它们在 open() 联机帮助页中的作用是什么?

最佳答案

手册页作为对程序员和编译器制造商的指导。

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

关于c - open()系统调用头文件要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28608860/

相关文章:

c - 如何将值传递给 XV6 中的系统调用函数?

c++ - Xcode 5 在 usr/include 中找不到头文件

c - 如何将 double 传递给在 C 中采用空终止字符串的函数?

python - 如何使用 exec 系统调用使用 C++ 的参数调用 Python 脚本

ios - 更新到 Xcode 10.2、Swift 5 后 `' NBPhoneNumberDesc.h ' file not found`

system-calls - 了解标准库函数是否正在执行系统调用的重要性

c - 更好的链表编程实践

c - 两个不同文件中的代码非常相似,当它们应该给出相同的结果时却给出了两个不同的结果

c - fork() - 如何让最后一个子进程杀死父进程/原始进程?

c++ - 在头文件中定义类是否正确?