O_DIRECT 未声明,Eclipse CDT,Ubuntu 20.04
O_DIRECT undeclared, Eclipse CDT, Ubuntu 20.04
我遇到了 O_DIRECT 的问题。
我正在尝试将它与 open()
一起使用,但出现如下错误:
error: O_DIRECT undeclared (first use in this function)
我包括<fcntl.h>
我为 O_DIRECT
搜索了 /usr/include/
目录,它存在于 x86_64-linux-gnu/bits/fcntl-linux.h
中。我尝试改为包含此文件,但随后出现此错误:
error: #error Never use <x86_64-linux-gnu/bits/fcntl-linux.h> directly; include <fcntl.h> instead.
我正在尝试在新安装的 Ubuntu 20.04 系统上的 Eclipse CDT 项目中执行所有这些操作。
您应该在包含 <fcntl.h>
之前定义 _GNU_SOURCE
或将 -D_GNU_SOURCE
添加到您的编译器命令。
请注意,这会降低程序的可移植性。
it exists in x86_64-linux-gnu/bits/fcntl-linux.h. I tried to include this file instead, but then I get this error
如错误所述,您不应直接包含 bits
headers。
O_DIRECT
是 Linux 扩展(即不在 POSIX 中)。您需要定义 _GNU_SOURCE
才能获取它。您可以在源文件的顶部定义它,例如:
#define _GNU_SOURCE
或在编译时用-D_GNU_SOURCE
定义。例如
gcc -D_GNU_SOURCE file.c
您可能也对 What does "#define _GNU_SOURCE" imply? 感兴趣。
我遇到了 O_DIRECT 的问题。
我正在尝试将它与 open()
一起使用,但出现如下错误:
error: O_DIRECT undeclared (first use in this function)
我包括<fcntl.h>
我为 O_DIRECT
搜索了 /usr/include/
目录,它存在于 x86_64-linux-gnu/bits/fcntl-linux.h
中。我尝试改为包含此文件,但随后出现此错误:
error: #error Never use <x86_64-linux-gnu/bits/fcntl-linux.h> directly; include <fcntl.h> instead.
我正在尝试在新安装的 Ubuntu 20.04 系统上的 Eclipse CDT 项目中执行所有这些操作。
您应该在包含 <fcntl.h>
之前定义 _GNU_SOURCE
或将 -D_GNU_SOURCE
添加到您的编译器命令。
请注意,这会降低程序的可移植性。
it exists in x86_64-linux-gnu/bits/fcntl-linux.h. I tried to include this file instead, but then I get this error
如错误所述,您不应直接包含 bits
headers。
O_DIRECT
是 Linux 扩展(即不在 POSIX 中)。您需要定义 _GNU_SOURCE
才能获取它。您可以在源文件的顶部定义它,例如:
#define _GNU_SOURCE
或在编译时用-D_GNU_SOURCE
定义。例如
gcc -D_GNU_SOURCE file.c
您可能也对 What does "#define _GNU_SOURCE" imply? 感兴趣。