LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 04-28-2008, 07:36 AM   #1
bangusilo
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");
          exit(
EXIT_FAILURE);
      }
      
memset(items_to_store'\0'sizeof(items_to_store));
      
strcpy(items_to_store[0].misc_chars"“First!");

      
items_to_store[0].any_integer 47;
      
strcpy(items_to_store[0].more_chars"foo");
      
strcpy(items_to_store[1].misc_chars"bar");
      
items_to_store[1].any_integer 13;
      
strcpy(items_to_store[1].more_chars"unlucky?");
      
strcpy(items_to_store[2].misc_chars"Third");
      
items_to_store[2].any_integer 3;
      
strcpy(items_to_store[2].more_chars"baz");
      for (
0ITEMS_USEDi++) 
      {
        
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);
                exit(
2);
           }
      }
      
sprintf(key_to_use"bu%d"13);
      
key_datum.dptr key_to_use;
      
key_datum.dsize strlen(key_to_use);
      
data_datum dbm_fetch(dbm_ptrkey_datum);
      if (
data_datum.dptr)
      {
           
printf("Data retrieved\n");
           
memcpy(&item_retrieveddata_datum.dptrdata_datum.dsize);
           
printf("Retrieved item - %s %d %s\n",item_retrieved.misc_charsitem_retrieved.any_integer,item_retrieved.more_chars);
      }
      else 
      {
           
printf("No data found for key %s\n"key_to_use);
      }
      
dbm_close(dbm_ptr);
      exit(
EXIT_SUCCESS);
  } 
 
Old 04-28-2008, 10:11 AM   #2
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2016 CentOS6.9 CentOS7.3 + 50+ other Linux OS, for test only.
Posts: 16,657

Rep: Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281Reputation: 3281
Probably #include <gdbm/ndbm.h> (almost as the comment says)
And may be 'gcc dbm1.c -o dbm1 -Wall -pedantic -lgdbm'
ref. this:
http://ubuntuforums.org/showthread.php?t=277163

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

Last edited by knudfl; 04-28-2008 at 10:19 AM. Reason: added 1 line
 
Old 04-29-2008, 01:02 PM   #3
bangusilo
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

http://209.85.171.104/translate_c?hl...%23post1748734

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


Reply


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 04:20 PM
NDBM error lors de la compilation de perl-5.8.6 tifa Linux - Software 2 03-03-2005 10:22 AM
GDBM Need for zinf install... What's GDBM??? Stevetgn Linux - Software 5 07-16-2004 06:00 AM
GDBM Database babukhan Programming 0 04-10-2003 05:43 AM
Gdbm Main Library? elizabetheanera Linux - Software 1 06-13-2001 10:54 AM


All times are GMT -5. The time now is 08:34 PM.

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