2010-04-13 75 views
2

當我編譯的C++的示例代碼,我得到了以下信息:的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 

回答

1

這些鏈接錯誤 - 他們的意思是你沒有在一個或多個你需要的庫的鏈接建立這個應用程序。

庫的文檔應該解釋如何使用它建立應用程序。通常,您需要將庫添加到編譯器命令行中:

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

將鏈接到庫搜索路徑中的libsomething.a中。但要知道哪一個鏈接,你必須閱讀文檔。

+0

嗨尼爾,關於如何解決斷鏈的任何提示?我不熟悉這一點,特別是在Mac上。:X – Brian 2010-04-13 09:08:24

+0

嘿尼爾,我花了一些時間弄清楚如何將庫添加到搜索路徑,但沒有找到任何有用的信息。你有什麼建議在Max Os上設置? – Brian 2010-04-13 14:03:18

+0

@Brian你可以用-L標誌指定搜索路徑的添加,就像你用-I指定包含路徑一樣。您也可以簡單地將庫的完整路徑和文件名放在編譯器命令行中。有關g ++的更多信息,請參閱http://gcc.gnu.org/onlinedocs/ – 2010-04-13 16:28:08

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

(我假設這是你已經安裝在圖書館和基礎上,包括(-I)指令的版本。)

編輯:所以,看來你試圖用手來編譯發行版中包含的一個示例(excxx_example_database_read.cpp)......它不僅僅是一個.cpp文件。 MyDB對象不是BerkeleyDB API的一部分,而是一個如何從中進行子類化的例子。

cd到你有BerkeleyDB源代碼,然後cd build_unix。在那裏,做make excxx_example_database_read

+0

@Brian,在應用您的命令後,仍然存在1個錯誤,請參閱上面的代碼 – Brian 2010-04-17 20:22:26