3

I am experiencing an issue with O_DIRECT. I am trying to use it with open(), but I get an error like:

error: O_DIRECT undeclared (first use in this function)

I am including <fcntl.h>

I grepped /usr/include/ directory for O_DIRECT and it exists in x86_64-linux-gnu/bits/fcntl-linux.h. I tried to include this file instead, but then I get this error:

error: #error Never use <x86_64-linux-gnu/bits/fcntl-linux.h> directly; include <fcntl.h> instead.

I am trying to all of this in Eclipse CDT project on newly installed Ubuntu 20.04 system.

P.P
  • 117,907
  • 20
  • 175
  • 238
RustyChicken
  • 133
  • 1
  • 7

2 Answers2

6

You should define _GNU_SOURCE before including <fcntl.h> or add -D_GNU_SOURCE to your compiler command.

Note that this reduces portability of your program.

1

it exists in x86_64-linux-gnu/bits/fcntl-linux.h. I tried to include this file instead, but then I get this error

As the error says, you shouldn't include bits headers directly.

O_DIRECT is a Linux extension (i.e. not in POSIX). You need to define _GNU_SOURCE to get it. You can either define it at the top of source file, like:

#define _GNU_SOURCE

or define while compiling with -D_GNU_SOURCE. e.g.

gcc -D_GNU_SOURCE file.c

You may interested in What does "#define _GNU_SOURCE" imply? too.

P.P
  • 117,907
  • 20
  • 175
  • 238