1. 下载gdbm安装包:
http://ubuntu.cn99.com/ubuntu/pool/main/g/gdbm/
http://download.chinaunix.net/download/0004000/3484.shtml
我下载的包名为:gdbm_1.8.3.orig.tar.bz2,gdbm-1.8.3.tar.gz,gdbm-1.9.1.tar.gz
2. 使用上面其中一个包应该都行,我使用gdbm_1.8.3.orig.tar.bz2,解压缩:tar jxvf gdbm_1.8.3.orig.tar.bz2,并把解压缩后的文件夹重命名为gdbm,进入gdbm目录,依次执行:./configure,make,make install命令,编译安装后,gdbm目录下为dbm库的相关头文件目录,gdbm/.libs目录为相关库文件目录;
注:如果编译安装时有错误发生,可以试试其他gdbm包。
3. 测试代码如下:
#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
/* A struct to use to test dbm */
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 i, result;
datum key_datum;
datum data_datum;
DBM *dbm_ptr;
dbm_ptr = dbm_open(TEST_DB_FILE, O_RDWR | O_CREAT, 0666);
if (!dbm_ptr) {
fprintf(stderr, "Failed to open database\n");
exit(EXIT_FAILURE);
}
/* put some data in the structures */
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 (i = 0; i < ITEMS_USED; i++) {
/* build a key to use */
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);
/* build the key datum strcture */
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_ptr, key_datum, data_datum, DBM_REPLACE);
if (result != 0) {
fprintf(stderr, "dbm_store failed on key %s\n", key_to_use);
exit(2);
}
} /* for */
/* now try and retrieve some data */
sprintf(key_to_use, "bu%d", 13); /* this is the key for the second item */
key_datum.dptr = key_to_use;
key_datum.dsize = strlen(key_to_use);
data_datum = dbm_fetch(dbm_ptr, key_datum);
if (data_datum.dptr) {
printf("Data retrieved\n");
memcpy(&item_retrieved, data_datum.dptr, data_datum.dsize);
printf("Retrieved item - %s %d %s\n",
item_retrieved.misc_chars,
item_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);
}
4. 编译运行:
执行:gcc -o dbm11 -I/root/Desktop/1/gdbm /root/Desktop/dbm1.c -L/root/Desktop/1/gdbm/.libs -lgdbm_compat -lgdbm
执行:/root/Desktop/dbm11,此时可能会出现如下错误:
dbm11: error while loading shared libraries: libgdbm_compat.so.3: cannot open shared object file: No such file or directory
因为用到了动态库文件,解放方案如下:
find / -name "libgdbm_compat.so.3"
/root/Desktop/1/gdbm/.libs/libgdbm_compat.so.3
执行:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/Desktop/1/gdbm/.libs:. 来指定动态库文件目录
再次执行:dbm11,输出如下:
Data retrieved
Retrieved item - bar 13 unlucky?
5. 错误情况:
1. 错误如下:
dbm1.c:(.text+0x2d): undefined reference to `dbm_open'
dbm1.c:(.text+0x250): undefined reference to `dbm_store'
dbm1.c:(.text+0x2fb): undefined reference to `dbm_fetch'
dbm1.c:(.text+0x38a): undefined reference to `dbm_close'
解决方案:需要链接一个gdbm_compat库才可以
参考网站:
http://blog.csdn.net/huiguixian/article/details/6447096
http://blog.csdn.net/hai__feng/article/details/9613849
http://www.linuxidc.com/Linux/2010-08/27780.htm