ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#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
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).
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.
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.
......
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
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?
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 ?
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.