Error running the example code for Berkeley DB

Hi there,

I've compiled succesfully the sample code for testing Berkeley DB as follow:

#include <sys/types.h>

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

#include "db.h"

#define      DATABASE "access.db"

      DB *dbp;
      DBT key, data;
      int ret, t_ret;
      /* Create the database handle and open the underlying database. */
      printf ("test 1");
      if ((ret = db_create(&dbp, NULL, 0)) != 0) {
            fprintf(stderr, "db_create: %s\n", db_strerror(ret));
            exit (1);
      printf("test 2");
      if ((ret = dbp->open(dbp,NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
            dbp->err(dbp, ret, "%s", DATABASE);
            goto err;
      printf("test 3");
      /* Initialize key/data structures. */
      memset(&key, 0, sizeof(key));
      memset(&data, 0, sizeof(data)); = "fruit";
      key.size = sizeof("fruit"); = "apple";
      data.size = sizeof("apple");

      /* Store a key/data pair. */
      if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) == 0)
            printf("db: %s: key stored.\n", (char *);
      else {
            dbp->err(dbp, ret, "DB->put");
            goto err;

      /* Retrieve a key/data pair. */
      if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0)
            printf("db: %s: key retrieved: data was %s.\n",
                (char *), (char *);
      else {
            dbp->err(dbp, ret, "DB->get");
            goto err;

      /* Delete a key/data pair. */
      if ((ret = dbp->del(dbp, NULL, &key, 0)) == 0)
            printf("db: %s: key was deleted.\n", (char *);
      else {
            dbp->err(dbp, ret, "DB->del");
            goto err;

      /* Retrieve a key/data pair. */
      if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0)
            printf("db: %s: key retrieved: data was %s.\n",
                (char *), (char *);
            dbp->err(dbp, ret, "DB->get");

err:      if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
            ret = t_ret;


However, I get an error :

error while loading shared libraries: cannot open shared object file: No such file or directory

However, the file is found in /usr/local/BerkeleyDB4.2/lib

I've also included   /usr/local/BerkeleyDB4.2/include in the include path and  /usr/local/BerkeleyDB4.2/lib in the library path.

Any idea what's wrong???
Who is Participating?
You have to include the path to the library e.g into, don't forget to run ldconfig after adding it.

Or use the environement variable LD_LIBRARY_PATH or do make symbolic link from directories which are searched e.g /usr/lib
If the libraries are symbolic links then you have to check if the files the point to are exitent. If not you can unlink and relink them.

Or you may want to install Berkeley DB from your Linux installation media or Server.

kwseowAuthor Commented:
Guess I found the answer... just ln this .so file into /usr/lib

However, do I need to link and to /usr/local/Berkeley.4.2/lib/ as well? Currently, both are linked to

And what could be the possible effect of changing this?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.