You are not logged in.

#1 2009-08-14 19:00:10

tadzik
Member
From: &tadzik
Registered: 2009-07-17
Posts: 91

[C] Compiling program using core/db

Hello there,
I wanted to start using db library. I've created the simple piece of code, everything based on a manpage:

#include <stdio.h>
#include <sys/types.h>
#include <limits.h>
#include <db.h>

int main() {
  DB *base;
  base = dbopen("db", O_CREAT, O_RDWR, DB_HASH, NULL);
  if(!base)
    printf("Failed to open database\n");
  return 0;
}

They I tried compiling it. But then...

└─[%]─> gcc -Wall -pedantic test.c
test.c: In function 'main':
test.c:8: warning: implicit declaration of function 'dbopen'
test.c:8: error: 'O_CREAT' undeclared (first use in this function)
test.c:8: error: (Each undeclared identifier is reported only once
test.c:8: error: for each function it appears in.)
test.c:8: error: 'O_RDWR' undeclared (first use in this function)
test.c:8: warning: assignment makes pointer from integer without a cast

...suprise. Looks like I'm lacking some compiler flags, but I've found nothing like this in the manpage. Could you give me a hand?
Regards,
Ted

Offline

#2 2009-08-14 19:44:41

Hohoho
Member
Registered: 2007-06-23
Posts: 222

Re: [C] Compiling program using core/db

You need to include fcntl.h to get rid of the undeclared O_CREAT O_RDWR errors.

Offline

#3 2009-08-14 19:52:43

tadzik
Member
From: &tadzik
Registered: 2009-07-17
Posts: 91

Re: [C] Compiling program using core/db

Thanks, but I still have the dbopen error

test.c: In function 'main':
test.c:9: warning: implicit declaration of function 'dbopen'
test.c:9: warning: assignment makes pointer from integer without a cast
/tmp/ccEfPHwW.o: In function `main':
test.c:(.text+0x31): undefined reference to `dbopen'
collect2: ld returned 1 exit status

Any ideas?
Regards

Offline

#4 2009-08-14 20:32:44

wu.soldier
Member
From: Poland
Registered: 2006-12-19
Posts: 22

Re: [C] Compiling program using core/db

It seems that dbopen() is not a function from the db library. I don't see it in db.h header. Perhaps you can check that db tutorial: http://www.oracle.com/technology/docume … index.html.

Offline

#5 2009-08-14 20:40:47

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: [C] Compiling program using core/db

If you're sure the function exists, try linking your program with the db library, like so:

gcc something.c -o something -ldb

Take this with a grain of salt, since I don't actually know anything about Berkeley DB.

Last edited by Peasantoid (2009-08-14 20:42:02)

Offline

#6 2009-08-14 21:23:49

Hohoho
Member
Registered: 2007-06-23
Posts: 222

Re: [C] Compiling program using core/db

I got it to work this way:

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <limits.h>
#include <db_185.h>

int main() {
  DB *base;
  base = dbopen("db", O_CREAT, O_RDWR, DB_HASH, NULL);
  if(!base)
    printf("Failed to open database\n");
  return 0;
}

Compiler with -ldb

Seems like dbopen is a legacy function.

Offline

Board footer

Powered by FluxBB