20

When I try to compile my program I get these errors:

btio.c:19: error: ‘O_RDWR’ was not declared in this scope
btio.c:19: error: ‘open’ was not declared in this scope
btio.c: In function ‘short int create_tree()’:
btio.c:56: error: ‘creat’ was not declared in this scope
btio.c: In function ‘short int create_tree(int, int)’:
btio.c:71: error: ‘creat’ was not declared in this scope

what library do I need to include to fix these errors?

2
  • 6
    You are missing a necessary header/include file, not a library. May 10, 2010 at 0:57
  • 1
    Doesn't #include work with libraries? May 10, 2010 at 1:44

2 Answers 2

50

You want:

#include <fcntl.h>    /* For O_RDWR */
#include <unistd.h>   /* For open(), creat() */

Also, note that, as @R Samuel Klatchko writes, these are not "libraries". What #include does is inserts a file into your code verbatim. It just so happens that the standard header fcntl.h will have a line like:

#define O_RDWR    <some value here>

And unistd.h will have lines like:

int open(const char *, int, ...);

int creat(const char *, mode_t);

In other words, function prototypes, which informs the compiler that this function exists somewhere and optionally what its parameters look like.

The later linking step will then look for these functions in libraries; that is where the term "library" comes in. Most typically these functions will exist in a library called libc.so. You can think of your compiler inserting the flag -lc (link to libc) on your behalf.

Also, these are not "C++" but rather POSIX.

3
  • 1
    What's the difference between a library file and a header file? May 10, 2010 at 1:11
  • 3
    @Phenom - Please read my answer again. The library has the actual code. The header is un-compiled C that has declarations without implementation.
    – asveikau
    May 10, 2010 at 3:24
  • #include <fcntl.h> doesn't work for me. I am using g++ compiler in Linux. I get the error "unable to find numeric literal operator..."
    – Kyle Sweet
    Apr 13, 2021 at 19:39
7

Have you tried <fcntl.h>? A search for any combination of those symbols would have yielded that...

2
  • 1
    I googled them but didn't find it. This was one of those rare moments when google doesn't make the answer obvious. May 10, 2010 at 1:41
  • 4
    @Phenom: The first result for creat or O_RDWR is from the OpenGroup POSIX documentation... May 10, 2010 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.