Question

I'm attempting to write a small program in C that will open and read from a Berkeley 4.2 hash DB on a FreeBSD 9.1 system for testing, but I can't get it to compile. This is the first time I've written anything in C and compiled from a command line so I'm probably missing one thing that'll get it working, I don't know.

After searching all over and looking at documentation and source code on github, this is what I've got so far:

#include <sys/types.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <unistd.h>

#include <db.h>

int main()
{                                                                 
  DB * mydb;                                                                
  u_int32_t open_flags = DB_RDONLY;
  int ret;

  ret = db_create(&mydb, NULL, 0);
  if (ret != 0) {
    printf("Error creating DB structure!");
    return 1;
  }

  ret = mydb->open(mydb, NULL, "bsddb-py", NULL, DB_HASH, open_flags, 0);
  if (ret != 0) {
    printf("Error opening DB file!");
    return 2;
  }

  mydb->close(mydb, 0);
}

I compile with this:

cc -ldb-4.2 db_test.c

And get this:

db_test.c: In function 'main':
db_test.c:20: error: 'DB_RDONLY' undeclared (first use in this function)
db_test.c:20: error: (Each undeclared identifier is reported only once
db_test.c:20: error: for each function it appears in.)
db_test.c:29: error: 'DB' has no member named 'open'
db_test.c:35: error: too many arguments to function 'mydb->close'

Apparently the compiler is hung up on using Berkeley 1.85 (dbopen and such) and it won't budge?

Was it helpful?

Solution

It looks like #include <db.h> will provide an interface to Berkeley 1.85 because that's what's installed by default on FreeBSD. We have Berkeley 4.2 installed via ports, and to avoid conflicts, the header that interfaces with 4.2 is put elsewhere - I was referencing the right library but not the right header.

So, I changed the include to:

#include <db42/db.h>

...and compiled with...

cc -I/usr/local/include/ -L/usr/local/lib/ -ldb-4.2 db_test.c -o db_test

Running the above source with that modification produced no visible output, which means it worked!

As a newbie to it, BSD is weird.

OTHER TIPS

DB_RDONLY is contained in some header file that you are not #including. That should take care of all the line 20 errors.

Line 29: a DB struct apparently doesn't have a member named open. Recheck the struct/maybe you forgot to include the file that that struct is declared in.

35: Seems like the function close doesn't take 2 arguments. Recheck this in the header file/make sure you included the header file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top