Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-28-2008, 08:36 AM   #1
LQ Newbie
Registered: Apr 2008
Posts: 2

Rep: Reputation: 0
Flag this message I have a problem with gdbm and ndbm

am using the book titled "Beginning Linux Programming 4th Edition by Neil Matthew
Richard Stones".
In chapter 7 of this book, i come across one example, and it is about dbm, i ran this example and i got the following error

In function `main':
ddbm1.c.text+0x2d): undefined reference to `dbm_open'
ddbm1.c.text+0x244): undefined reference to `dbm_store'
ddbm1.c.text+0x2ee): undefined reference to `dbm_fetch'
ddbm1.c.text+0x379): undefined reference to `dbm_close'
collect2: ld returned 1 exit status

I have compiled using the following commands,

1.gcc -o dbm1 -I/usr/include/ndbm dbm1.c -lgdbm

also i tried with

gcc -o dbm1 –I/usr/include/gdbm dbm1.c -lgdbm_compat –lgdbm

I am using Open Susse Linux 10.3.
I failed to find the reason behind this error, and i couldn't find the answer any where.
I hope you will assist me on this.

PHP Code:
 #include  <unistd.h>
 #include  <stdlib.h>
 #include  <stdio.h>
 #include  <fcntl.h>
 #include <ndbm.h>
 /* On some systems you need to replace the above with
 #include <gdbm-ndbm.h>
 #include <string.h>
 #define TEST_DB_FILE “/tmp/dbm1_test”
 #define ITEMS_USED 3
struct test_data {
char misc_chars[15];
int any_integer;
char more_chars[21];
int main()

struct test_data items_to_store[ITEMS_USED];
struct test_data item_retrieved;
char key_to_use[20];
int iresult;
datum key_datum;
datum data_datum;
DBM *dbm_ptr;
dbm_ptr dbm_open(TEST_DB_FILEO_RDWR O_CREAT0666);
      if (!
dbm_ptr) {
fprintf(stderr"Failed to open database\n");

items_to_store[0].any_integer 47;
items_to_store[1].any_integer 13;
items_to_store[2].any_integer 3;
      for (
sprintf(key_to_use"%c%c%d",items_to_store[i].misc_chars[0], items_to_store[i].more_chars[0],items_to_store[i].any_integer);
key_datum.dptr = (void *)key_to_use;
key_datum.dsize strlen(key_to_use);
data_datum.dptr = (void *)&items_to_store[i];
data_datum.dsize sizeof(struct test_data);
result dbm_store(dbm_ptrkey_datumdata_datumDBM_REPLACE);
           if (
result != 0
fprintf(stderr"dbm_store failed on key %s\n"key_to_use);
key_datum.dptr key_to_use;
key_datum.dsize strlen(key_to_use);
data_datum dbm_fetch(dbm_ptrkey_datum);
      if (
printf("Data retrieved\n");
printf("Retrieved item - %s %d %s\n",item_retrieved.misc_charsitem_retrieved.any_integer,item_retrieved.more_chars);
printf("No data found for key %s\n"key_to_use);
Old 04-28-2008, 11:11 AM   #2
LQ 5k Club
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2020 CentOS6.10 CentOS7.7 + 50+ other Linux OS, for test only.
Posts: 17,322

Rep: Reputation: 3581Reputation: 3581Reputation: 3581Reputation: 3581Reputation: 3581Reputation: 3581Reputation: 3581Reputation: 3581Reputation: 3581Reputation: 3581Reputation: 3581
Probably #include <gdbm/ndbm.h> (almost as the comment says)
And may be 'gcc dbm1.c -o dbm1 -Wall -pedantic -lgdbm'
ref. this:

Edit: Sorry, looks like what you are already doing.

Last edited by knudfl; 04-28-2008 at 11:19 AM. Reason: added 1 line
Old 04-29-2008, 02:02 PM   #3
LQ Newbie
Registered: Apr 2008
Posts: 2

Original Poster
Rep: Reputation: 0
hey guy, i got the soln,
with this command

gcc dbm1.c -o dbm1 -lgdbm -lgdbm_compat

I got soln from this site, you need to translate it from chinese to ur lang

Last edited by bangusilo; 04-30-2008 at 01:32 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
cyrus-imap: per-message \Seen flag Berhanie Linux - General 0 10-17-2005 05:20 PM
NDBM error lors de la compilation de perl-5.8.6 tifa Linux - Software 2 03-03-2005 11:22 AM
GDBM Need for zinf install... What's GDBM??? Stevetgn Linux - Software 5 07-16-2004 07:00 AM
GDBM Database babukhan Programming 0 04-10-2003 06:43 AM
Gdbm Main Library? elizabetheanera Linux - Software 1 06-13-2001 11:54 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:04 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration