LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1538|回复: 0

不能编译gdbm程序?(解决)

[复制链接]
发表于 2007-8-15 22:59:38 | 显示全部楼层 |阅读模式
下面是一个从《Beginning Linux Programming 3rd》中的一个例子程序,但是在Archlinux下编译不了,我不知道具体什么原因。而gdbm是在base中的,所以完全安装了base中的包的系统都已经安装好了gdbm。希望大家帮忙一下!


  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <ndbm.h>
  6. #include <string.h>

  7. #define TEST_DB_FILE "/tmp/dbm1_test"
  8. #define ITEMS_USED 3

  9. /* A struct to use to test dbm */
  10. struct test_data {
  11.     char misc_chars[15];
  12.     int  any_integer;
  13.     char more_chars[21];
  14. };

  15. int main() {

  16.     struct test_data items_to_store[ITEMS_USED];
  17.     struct test_data item_retrieved;

  18.     char key_to_use[20];
  19.     int i, result;

  20.     datum key_datum;
  21.     datum data_datum;

  22.     DBM *dbm_ptr;

  23.     dbm_ptr = dbm_open(TEST_DB_FILE, O_RDWR | O_CREAT, 0666);
  24.     if (!dbm_ptr) {
  25.         fprintf(stderr, "Failed to open database\n");
  26.         exit(EXIT_FAILURE);
  27.     }

  28.         /* put some data in the structures */
  29.     memset(items_to_store, '\0', sizeof(items_to_store));
  30.     strcpy(items_to_store[0].misc_chars, "First!");
  31.     items_to_store[0].any_integer = 47;
  32.     strcpy(items_to_store[0].more_chars, "foo");
  33.     strcpy(items_to_store[1].misc_chars, "bar");
  34.     items_to_store[1].any_integer = 13;
  35.     strcpy(items_to_store[1].more_chars, "unlucky?");
  36.     strcpy(items_to_store[2].misc_chars, "Third");
  37.     items_to_store[2].any_integer = 3;
  38.     strcpy(items_to_store[2].more_chars, "baz");

  39.     for (i = 0; i < ITEMS_USED; i++) {
  40.             /* build a key to use */
  41.         sprintf(key_to_use, "%c%c%d",
  42.             items_to_store[i].misc_chars[0],
  43.             items_to_store[i].more_chars[0],
  44.             items_to_store[i].any_integer);

  45.             /* build the key datum strcture */
  46.         key_datum.dptr = (void *)key_to_use;
  47.         key_datum.dsize = strlen(key_to_use);
  48.         data_datum.dptr = (void *)&items_to_store[i];
  49.         data_datum.dsize = sizeof(struct test_data);

  50.         result = dbm_store(dbm_ptr, key_datum, data_datum, DBM_REPLACE);
  51.         if (result != 0) {
  52.             fprintf(stderr, "dbm_store failed on key %s\n", key_to_use);
  53.             exit(2);
  54.         }
  55.     } /* for */

  56.         /* now try and retrieve some data */
  57.     sprintf(key_to_use, "bu%d", 13); /* this is the key for the second item */
  58.     key_datum.dptr = key_to_use;
  59.     key_datum.dsize = strlen(key_to_use);

  60.     data_datum = dbm_fetch(dbm_ptr, key_datum);
  61.     if (data_datum.dptr) {
  62.         printf("Data retrieved\n");
  63.         memcpy(&item_retrieved, data_datum.dptr, data_datum.dsize);
  64.         printf("Retrieved item - %s %d %s\n",
  65.                item_retrieved.misc_chars,
  66.                item_retrieved.any_integer,
  67.                item_retrieved.more_chars);
  68.     }
  69.     else {
  70.         printf("No data found for key %s\n", key_to_use);
  71.     }

  72.     dbm_close(dbm_ptr);

  73.     exit(EXIT_SUCCESS);
  74. }

复制代码


尝试编译1:
$gcc dbm1.c -o gdbm1 -lgdbm
错误信息:
/tmp/cc2vMknU.o: In function `main':
dbm1.c: (.text+0x2d): undefined reference to `dbm_open'
dbm1.c: (.text+0x244): undefined reference to `dbm_store'
dbm1.c: (.text+0x2ee): undefined reference to `dbm_fetch'
dbm1.c: (.text+0x379): undefined reference to `dbm_close'
collect2: ld returned 1 exit status

尝试编译2:
$ gcc dbm1.c -o gdbm1 -lndbm -L/usr/lib
错误信息:
/usr/bin/ld: cannot find -lndbm
collect2: ld returned 1 exit status

解决了:
如果用gdbm,还要兼容dbm或ndmb,编译的时候要连接上gdbm_compat:
$ gcc dbm1.c -o dbm1 -lgdbm -lgdbm_compat
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表