2

当我编译 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
4

2 回答 2

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

于 2010-04-16T21:34:31.613 回答
1

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

该库的文档应说明如何使用它构建应用程序。一般情况下,您需要将库添加到编译器命令行:

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

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

于 2010-04-13T09:01:43.120 回答