【问题标题】:berkeley DB: can't compile c++ codesberkeley DB:无法编译 C++ 代码
【发布时间】:2010-04-13 09:00:17
【问题描述】:

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

【问题讨论】:

    标签: berkeley-db


    【解决方案1】:

    您需要链接到 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

    【讨论】:

    • @Brian,我应用你的命令后,还是有1个错误,看上面的代码
    【解决方案2】:

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

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

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

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

    【讨论】:

    • 嗨 Neil,关于如何修复损坏的链接的任何提示?我对此并不熟悉,尤其是在 Mac 上。:X
    • 嘿,尼尔,我花了一些时间弄清楚如何将库添加到搜索路径,但没有找到任何有用的信息。你对在 Max Os 上设置这个有什么建议吗?
    • @Brian 您使用 -L 标志指定搜索路径的添加,与使用 -I 指定包含路径的方式大致相同。您也可以简单地将库的完整路径和文件名放在编译器命令行上。有关 g++ 的更多信息,请参阅gcc.gnu.org/onlinedocs
    猜你喜欢
    • 2011-08-08
    • 2015-07-23
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 2017-08-25
    相关资源
    最近更新 更多