C++ berkeley db安装后编译链接的奇怪错误

officercat 2015-11-14 08:36:26


我在centos6.5上安装了berkeley db 6.1.6,是下源代码在本系统上重新编译安装的。

库如下:

ll -rt /usr/local/lib
-rwxr-xr-x 1 root root 2141065 11月 14 19:46 libdb-6.1.so
-rw-r--r-- 1 root root 929 11月 14 19:46 libdb-6.1.la
-rw-r--r-- 1 root root 3250156 11月 14 19:50 libdb-6.1.a
-rw-r--r-- 1 root root 3250156 11月 14 19:50 libdb.a
lrwxrwxrwx 1 root root 12 11月 14 19:50 libdb.so -> libdb-6.1.so
lrwxrwxrwx 1 root root 12 11月 14 19:50 libdb-6.so -> libdb-6.1.so

也运行了 ldconfig

用了document上的示例代码

#include <string.h>
#include <db_cxx.h>
using namespace std;
#define DESCRIPTION_SIZE 199

int main(int argc,char ** argv)
{
char *description = "Grocery bill.";
float money = 122.45;
Db my_database(NULL, 0);

Dbt key(&money, sizeof(float));
Dbt data(description, strlen(description) + 1);
int ret = my_database.put(NULL, &key, &data, DB_NOOVERWRITE);
if (ret == DB_KEYEXIST) {
my_database.err(ret, "Put failed because key %f already exists",money);
}
}

然后编译
gcc般般是4.9.2
clang++ main.cc -ldb -lpthread -o test_d -DHAVE_CXX_STDHEADERS
编译成功, 但链接错误:
[root@10-180-92-207 bigdata]# clang++ main.cc -ldb -lpthread -o test_d -DHAVE_CXX_STDHEADERS
/tmp/main-660df3.o: In function main':
main.cc:(.text+0x87): undefined reference toDb::Db(DbEnv*, unsigned int)'
main.cc:(.text+0x9f): undefined reference to Dbt::Dbt(void*, unsigned int)'
main.cc:(.text+0xd0): undefined reference toDbt::Dbt(void*, unsigned int)'
main.cc:(.text+0xf9): undefined reference to Db::put(DbTxn*, Dbt*, Dbt*, unsigned int)'
main.cc:(.text+0x147): undefined reference toDb::err(int, char const*, ...)'
main.cc:(.text+0x199): undefined reference to Dbt::Dbt()'
main.cc:(.text+0x1aa): undefined reference toDbt::Dbt()'
main.cc:(.text+0x242): undefined reference to Db::get(DbTxn*, Dbt*, Dbt*, unsigned int)'
main.cc:(.text+0x2ed): undefined reference toDbt::~Dbt()'
main.cc:(.text+0x2fe): undefined reference to Dbt::~Dbt()'
main.cc:(.text+0x30f): undefined reference toDbt::~Dbt()'
main.cc:(.text+0x320): undefined reference to Dbt::~Dbt()'
main.cc:(.text+0x331): undefined reference toDb::~Db()'
main.cc:(.text+0x36c): undefined reference to Dbt::~Dbt()'
main.cc:(.text+0x382): undefined reference toDbt::~Dbt()'
main.cc:(.text+0x398): undefined reference to Dbt::~Dbt()'
main.cc:(.text+0x3ae): undefined reference toDbt::~Dbt()'
main.cc:(.text+0x3c4): undefined reference to `Db::~Db()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这就和完全啊没装berkeleydb的库一样的报错信息。
但问题是就libdb一个库文件,我也装了,就在系统默认目录下,也ldconfig了,-ldb也用了,就算用-L再明确指一遍也不行。。。

我在另一台ubuntu14.04的电脑上重复了以上操作还是一样的问题。

这是什么鬼?装多了开源库,第一次件berkeleydb这样让我摸不着头脑的。
...全文
461 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
officercat 2016-01-29
  • 打赏
  • 举报
回复
引用 2 楼 pww71 的回复:
太落伍了,什么年代了。用这个看看。 http://blog.csdn.net/pww71/article/details/25113303
有空会学习学习
officercat 2016-01-29
  • 打赏
  • 举报
回复
引用 1 楼 lingtian725 的回复:
gcc xxx.c -ldb g++ xxx.cpp -ldb_cxx
我是用错了库,没注意berkeleydb的安装库里有C 和C++两个不同的库文件,我在网上看的说明都是C的配置。
pww71 2016-01-20
  • 打赏
  • 举报
回复
太落伍了,什么年代了。用这个看看。 http://blog.csdn.net/pww71/article/details/25113303
BigCat_X 2016-01-20
  • 打赏
  • 举报
回复
gcc xxx.c -ldb g++ xxx.cpp -ldb_cxx

8,028

社区成员

发帖
与我相关
我的任务
社区描述
高性能数据库开发
社区管理员
  • 高性能数据库开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧