【发布时间】:2019-03-21 13:18:39
【问题描述】:
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);
我该如何解决这个问题?
谢谢。
【问题讨论】:
-
MacOS 没有实现
O_DIRECT。您将需要对更大的程序进行重大更改。 This old answer 表明存在一个名为F_NOCACHE的fcntl操作码,这或多或少是等价的,但它所引用的文档已从 Apple 的网站上删除,所以我可以给你的详细信息。 -
我还可以告诉你
#define _GNU_SOURCE对 MacOS 没有影响,而且即使在 Linux 上只有在 之前定义它才会有影响i> 包括任何系统头文件,并且应用程序代码中的#define __USE_GNU会引发未定义的行为。 -
谢谢zwol。你解决了我所有的困惑!