LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2008, 12:48 PM   #1
satyaprakash
LQ Newbie
 
Registered: Jul 2008
Posts: 7

Rep: Reputation: 0
linking db library at compile time


I run the following program:


Code:
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#include<db.h>
#define DATABASE "access.db"

int main()
{

    DB *dbp;
    int ret;
  
    dbp = dbopen(DATABASE,DB_CREATE,0664,DB_HASH,0);

}
I compile it using

cc example.c

and I get this error mostly due to some linker problem

example.c: In function `main':
example.c:17: warning: assignment makes pointer from integer without a cast
/tmp/ccu1Cn3K.o(.text+0x2b): In function `main':
: undefined reference to `dbopen'
collect2: ld returned 1 exit status

Can anyone help me
 
Old 07-02-2008, 01:28 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Yah.

Add 'a -lname_of_lib' to the cc invocation. You didn't mention
which database engine you're trying to use, so I can't be more
specific than that. E.g.

gcc -lpq example.c
if you were using postgres (which you're not - that much I can
tell from the headers you're using ;}). The name of the actual
library is libpq.so.5.1 (in my case).



Cheers,
Tink
 
Old 07-02-2008, 01:40 PM   #3
satyaprakash
LQ Newbie
 
Registered: Jul 2008
Posts: 7

Original Poster
Rep: Reputation: 0
I am trying to read the file which SpamAssassin generates, called "bayes_toks" (a Berkeley DB file). Of the database engine, I dunno what I am using. How do I know. I tried linking with the help of "libdb.a". It didn't work. Can u help me further ?

Last edited by satyaprakash; 07-02-2008 at 01:50 PM.
 
Old 07-02-2008, 02:45 PM   #4
satyaprakash
LQ Newbie
 
Registered: Jul 2008
Posts: 7

Original Poster
Rep: Reputation: 0
I somehow got the program compiled searching and linking the right library.
gcc example.c /usr/lib/libdb1.so.2

now, i have a new problem:

the man page for db looks like this:

Code:
.......
       typedef struct {
              DBTYPE type;
              int (*close)(const DB *db);
              int (*del)(const DB *db, const DBT *key, u_int flags);
              int (*fd)(const DB *db);
              int (*get)(const DB *db, DBT *key, DBT *data, u_int flags);
              int (*put)(const DB *db, DBT *key, const DBT *data,
                   u_int flags);
              int (*sync)(const DB *db, u_int flags);
              int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags);
       } DB;
......

get - A pointer to a routine which is the interface for keyed              retrieval from the database.  The address and length of the data              associated with the specified key are returned in the  structure              referenced  by  data.   Get routines return -1 on error (setting              errno), 0 on success, and 1 if the key was not in the file.
......

i try to use get function in the above structure.

Code:
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#include<db.h>
#define DATABASE "./bayes_toks"

int main()
{

    DB* dbopen();

    DB *dbp;
    DBT key, data;
    int ret;
    char *path = "./bayes_toks";

    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));
    key.data = "Linux";
    key.size = sizeof("Linux");
//    data.data = "apple";
//    data.size = sizeof("apple");   


    if((dbp = dbopen(path,DB_CREATE,0664,DB_HASH,0))!=0)
    {
        printf("error opening the database\n");
        return;
    }

      ret = dbp->get(dbp,&key,&data,0);

      if(ret==0)
        printf("success \n");
      else if (ret ==-1)
        printf("error \n");
      else
        printf("not found");

}
I get an error saying:

Code:
example.c: In function `main':
example.c:32: warning: passing arg 2 of pointer to function from incompatible pointer type
example.c:32: too few arguments to function
Anything wrong with my code ?? I insert a NULL between dbp and &key in the get function. It works, but I get a "Segmentation Fault". Any advice ?

Last edited by satyaprakash; 07-03-2008 at 04:43 AM. Reason: for consistency sake
 
Old 07-02-2008, 03:52 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Not from me ... but I'm now moving this across to our programming forum
for better exposure.


Cheers,
Tink
 
Old 07-02-2008, 06:24 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Code:
example.c: In function `main':
example.c:32: warning: assignment makes pointer from integer without a cast
example.c:38: warning: passing arg 2 of pointer to function from incompatible pointer type
example.c:38: too few arguments to function
Have you edited your file from the one being shown? line 32 for example is a printf function call?

Code:
    key.data = "Linux";
    key.size = sizeof("Linux");
Is this correct? should key.size be five or six ie should it include the null terminator?
 
Old 07-03-2008, 04:45 AM   #7
satyaprakash
LQ Newbie
 
Registered: Jul 2008
Posts: 7

Original Poster
Rep: Reputation: 0
I ve modified the above code and the errors list. Now they are consistent. About the size, the format is as specified by Berkeley DB reference guide. So that must be right. What could be the problem with the dbp->get function. Is it the man page ?
 
Old 07-03-2008, 05:36 AM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
It seems the error is due to the function having another parameter compared to what is produced by your implementation man page.
example:http://docs.huihoo.com/berkeley/db-4...e_tut/get.html
get function definition:http://docs.huihoo.com/berkeley/db-4..._c/db_get.html
 
Old 07-03-2008, 06:50 AM   #9
satyaprakash
LQ Newbie
 
Registered: Jul 2008
Posts: 7

Original Poster
Rep: Reputation: 0
I've used the gdb and found that the problem is with the dbopen function. That isn't returning a proper pointer after opening the database. It's returning a null pointer. As shown in the Berkeley db tutorial, there is nothing such called "db_open" in my library. There is only dbopen. What's wrong with this berkeley db ?

Last edited by satyaprakash; 07-03-2008 at 07:10 AM. Reason: adding more info
 
Old 07-03-2008, 01:02 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Is that tutorial you're following based on the same version
of BDB? There might have conceivably been changes in the API
between major versions.


Cheers,
Tink
 
Old 07-03-2008, 02:47 PM   #11
satyaprakash
LQ Newbie
 
Registered: Jul 2008
Posts: 7

Original Poster
Rep: Reputation: 0
no, it isn't the same version. that's why i amn't copy pasting the tutorial code. i am referring the man page but it isnt working the man pages' way or the tutorial's way either.
 
Old 07-03-2008, 03:06 PM   #12
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I read earlier today (yet forgot to post here about it) that there was major API changes in version 4.1(or somewhere around this version) which explains why you needed the extra parameter in the get function. I did try and have a look for an example snippet of source using the dbopen function yet could not seem to find one(I did not look very hard),yet all the examples use the same method as I linked to earlier of creating and then opening the file, were it seems that creating was just initialising the database instance and not creating in the sense of a file.
 
Old 07-04-2008, 04:54 AM   #13
satyaprakash
LQ Newbie
 
Registered: Jul 2008
Posts: 7

Original Poster
Rep: Reputation: 0
thanx a lot.. i ve linked the right library and everything is working now..
gcc example.c /usr/lib/libdb-4.1.so
 
  


Reply



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
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
Problem in using both load time linking and runtime linking durgaprasad_j Linux - General 0 08-01-2006 03:49 AM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM
linking own library bobby2k3 Programming 2 10-20-2003 10:36 AM
linking library simonissa Linux - Software 2 05-21-2003 09:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:42 AM.

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
Open Source Consulting | Domain Registration