zzhouyy
zzhouyy

Reputation: 21

undefined reference to 'gdbm_open'

I have installed gdbm, command "man gdbm" is ok. Now I try to write a simple gdbm program.

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>

#include <gdbm.h>

#define TEST_DB_FILE "/tmp/gdbm1_test.db"

int main()
{
    GDBM_FILE gdbm_ptr;
    gdbm_ptr = gdbm_open(TEST_DB_FILE, 0, GDBM_WRCREAT, 0666, NULL );

    if ( !gdbm_ptr )
    {
         fprintf(stderr, "Failed to open database\n");
         exit(EXIT_FAILURE);
    }
}

But when it compiles, "undefined reference to 'gdbm_open" error occurs. I have include gdbm.h, why such error occurs?

Upvotes: 2

Views: 1209

Answers (1)

user626998
user626998

Reputation:

You need to link with libgdbm:

gcc foo.c -lgdbm

Upvotes: 2

Related Questions