berkeley DB:无法编译c ++代码

时间:2010-04-13 09:00:17

标签: berkeley-db

当我编译C ++的示例代码时,我得到以下信息:

c++ excxx_example_database_read.cpp -o dbApp -I /usr/local/BerkeleyDB.5.0/include/
Undefined symbols:
  "Dbt::Dbt(void*, unsigned int)", referenced from:
      show_vendor(MyDb&, char const*)in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
  "Dbc::get(Dbt*, Dbt*, unsigned int)", referenced from:
      show_all_records(MyDb&, MyDb&) in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
  "Dbc::close()", referenced from:
      show_all_records(MyDb&, MyDb&) in ccnaWItX.o
      show_all_records(MyDb&, MyDb&) in ccnaWItX.o
      show_all_records(MyDb&, MyDb&) in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
  "Dbt::~Dbt()", referenced from:
      show_vendor(MyDb&, char const*)in ccnaWItX.o
      show_vendor(MyDb&, char const*)in ccnaWItX.o
      show_vendor(MyDb&, char const*)in ccnaWItX.o
      show_vendor(MyDb&, char const*)in ccnaWItX.o
      show_all_records(MyDb&, MyDb&) in ccnaWItX.o
      show_all_records(MyDb&, MyDb&) in ccnaWItX.o
      show_all_records(MyDb&, MyDb&) in ccnaWItX.o
      show_all_records(MyDb&, MyDb&) in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
  "Db::~Db()", referenced from:
      MyDb::~MyDb() in ccnaWItX.o
      MyDb::~MyDb() in ccnaWItX.o
  "typeinfo for DbException", referenced from:
      GCC_except_table3 in ccnaWItX.o
      GCC_except_table4 in ccnaWItX.o
      GCC_except_table5 in ccnaWItX.o
      GCC_except_table6 in ccnaWItX.o
      __ZTI11DbException$non_lazy_ptr in ccnaWItX.o
  "DbException::~DbException()", referenced from:
      __ZN11DbExceptionD1Ev$non_lazy_ptr in ccnaWItX.o
  "MyDb::close()", referenced from:
      MyDb::~MyDb() in ccnaWItX.o
  "MyDb::MyDb(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, bool)", referenced from:
      _main in ccnaWItX.o
      _main in ccnaWItX.o
      _main in ccnaWItX.o
  "Dbt::Dbt()", referenced from:
      show_vendor(MyDb&, char const*)in ccnaWItX.o
      show_all_records(MyDb&, MyDb&) in ccnaWItX.o
      show_all_records(MyDb&, MyDb&) in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
  "DbException::get_errno() const", referenced from:
      show_vendor(MyDb&, char const*)in ccnaWItX.o
      show_all_records(MyDb&, MyDb&) in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
      _main in ccnaWItX.o
  "DbException::DbException(DbException const&)", referenced from:
      show_vendor(MyDb&, char const*)in ccnaWItX.o
      show_all_records(MyDb&, MyDb&) in ccnaWItX.o
      show_item(MyDb&, MyDb&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccnaWItX.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

我不知道是什么问题。请帮忙!

更新: 现在我用:

getting_started baxxu$ c++ excxx_example_database_r
ead.cpp -o dbApp -I /usr/local/BerkeleyDB.5.0/include/ -L/usr/local/
BerkeleyDB.5.0/lib -ldb_cxx-5.0

仍然出错:

Undefined symbols:
  "MyDb::close()", referenced from:
      MyDb::~MyDb() in ccYCyhIg.o
  "MyDb::MyDb(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, bool)", referenced from:
      _main in ccYCyhIg.o
      _main in ccYCyhIg.o
      _main in ccYCyhIg.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

2 个答案:

答案 0 :(得分:4)

您需要链接到BerkeleyDB c ++库。

c++ excxx_example_database_read.cpp -o dbApp -I /usr/local/BerkeleyDB.5.0/include/ -L/usr/local/BerkeleyDB.5.0/lib -ldb_cxx-5.0

(我假设您已经安装了库,而且版本基于include(-I)指令。)

编辑:因此,您似乎正在尝试手动编译分发中包含的一个示例(excxx_example_database_read.cpp)...它不仅仅是一个.cpp文件。 MyDB对象不是BerkeleyDB API的一部分,而是如何从中进行子类化的示例。

cd您拥有BerkeleyDB源代码,然后cd build_unix。在那里,做make excxx_example_database_read

答案 1 :(得分:1)

这些是链接器错误 - 它们意味着您没有链接到构建此应用程序所需的一个或多个库中。

库的文档应该解释如何使用它构建应用程序。通常,您需要将库添加到编译器命令行:

c++ excxx_example_database_read.cpp -o dbApp -I /usr/local/BerkeleyDB.5.0/include/  -lsomething

将链接到libsomething.a,在库搜索路径中找到。但是要知道要链接哪一个,你必须阅读文档。