Ubuntu 下用C语言访问gdbm数据库编译注意事项(使..

Ubuntu 下用C语言访问gdbm数据库编译注意事项(使用ndbm方式)

今天看书<Linux程序设计>(第三版)第七章,编译两个dbm1.c和dbm2.c文件时编译错误,提示:

$ gcc -ldbm -o dbm1 dbm1.c
/usr/bin/ld: cannot find -ldbm

collect2: ld returned 1 exit status 

把-ldbm改成-lgdbm仍然报错,查了下资料终于找到解决方案。

 

把dbm1.c文件里的头文件声明#include <ndbm.h>注释掉,然后把#include <gdbm-ndbm.h>头文件的注释去掉,如下:

 

 //#include <ndbm.h>
/* On some systems you need to replace the above with */
#include <gdbm-ndbm.h>
编译文件时需要使用如下命令:
$ gcc -lgdbm_compat -o dbm1 dbm1.c

  Ubuntu中默认安装的是gdbm数据库,应该使用gdbm实现来访问数据库。若还是需要以dbm若ndbm方式来访问数据库的话就需要使用gdbm-ndbm.h头文件,编译链接时应使用-lgdbm_compat链接选项

作者: osdba   发布时间: 2010-09-22