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"

int
main()
{
      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));
      key.data = "fruit";
      key.size = sizeof("fruit");
      data.data = "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 *)key.data);
      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 *)key.data, (char *)data.data);
      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 *)key.data);
      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 *)key.data, (char *)data.data);
      else
            dbp->err(dbp, ret, "DB->get");

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

      exit(ret);
}
-------------------

However, I get an error :

error while loading shared libraries: libdb-4.2.so: cannot open shared object file: No such file or directory

However, the libdb-4.2.so 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???
kwseowAsked:
Who is Participating?
 
fridomCEO/ProgrammerCommented:
You have to include the path to the library e.g into ld.so.conf, 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.

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

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

And what could be the possible effect of changing this?
 
0
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.