使用未声明的标识符“ O_DIRECT”

时间:2019-03-21 13:18:39

标签: c macos

Apple LLVM版本10.0.0(clang-1000.10.44.4)
目标:x86_64-apple-darwin18.0.0

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#define _GNU_SOURCE
#define __USE_GNU 1
#include <fcntl.h>


int main()
{
    int fd = open("./test.txt", O_WRONLY|O_DIRECT);

    close(fd);

    return 0;
}

我使用clang -o out test.c并得到以下结果:

test.c:14:39: error: use of undeclared identifier 'O_DIRECT'
    int fd = open("./test.txt", O_WRONLY|O_DIRECT);

我该如何解决问题?

谢谢。

2 个答案:

答案 0 :(得分:0)

使用此代码段无法说出您要做什么,但通常不要在旨在可移植的应用程序中使用非标准的内容。

完成任务的便携式方法是大概 new

答案 1 :(得分:0)

总结

对于LINUX,必须包括O_DIRECT标志。对于Mac OSX,O_DIRECT不可用。相反,fcntl(fd, F_NOCACHE, 1)似乎是规范的解决方案,其中fd是文件的文件描述符。对于Windows,有一个名为FILE_FLAG_NO_BUFFERING的标志是Windows中O_DIRECT

的对应标志

Reference here