2012-08-27 43 views
1

我想使用NTL庫,但我總是得到有關未定義符號的編譯器錯誤。編譯錯誤使用NTL庫

實施例(從NTL文檔截取):

#include <NTL/ZZ.h> 

NTL_CLIENT 

int main() 
{ 
    ZZ a, b, c; 

    cin >> a; 
    cin >> b; 
    c = (a+1)*(b+1); 
    cout << c << "\n"; 
} 

結果:

$ g++ -lntl simple.cpp 
/tmp/ccGwxURb.o: In function `main': 
simple.cpp:(.text+0x3a): undefined reference to `NTL::operator>>(std::basic_istream<char, std::char_traits<char> >&, NTL::ZZ&)' 
simple.cpp:(.text+0x4b): undefined reference to `NTL::operator>>(std::basic_istream<char, std::char_traits<char> >&, NTL::ZZ&)' 
simple.cpp:(.text+0xda): undefined reference to `NTL::operator<<(std::basic_ostream<char, std::char_traits<char> >&, NTL::ZZ const&)' 
/tmp/ccGwxURb.o: In function `NTL::ZZ::operator=(NTL::ZZ const&)': 
simple.cpp:(.text._ZN3NTL2ZZaSERKS0_[NTL::ZZ::operator=(NTL::ZZ const&)]+0x22): undefined reference to `_ntl_gcopy' 
/tmp/ccGwxURb.o: In function `NTL::ZZ::~ZZ()': 
simple.cpp:(.text._ZN3NTL2ZZD2Ev[_ZN3NTL2ZZD5Ev]+0x14): undefined reference to `_ntl_gfree' 
/tmp/ccGwxURb.o: In function `NTL::add(NTL::ZZ&, NTL::ZZ const&, long)': 
simple.cpp:(.text._ZN3NTL3addERNS_2ZZERKS0_l[NTL::add(NTL::ZZ&, NTL::ZZ const&, long)]+0x2a): undefined reference to `_ntl_gsadd' 
/tmp/ccGwxURb.o: In function `NTL::mul(NTL::ZZ&, NTL::ZZ const&, NTL::ZZ const&)': 
simple.cpp:(.text._ZN3NTL3mulERNS_2ZZERKS0_S3_[NTL::mul(NTL::ZZ&, NTL::ZZ const&, NTL::ZZ const&)]+0x2d): undefined reference to `_ntl_gmul' 
collect2: ld returned 1 exit status 

的NTL頭是在/usr/include/NTL,所以它們應該包括在內。

有什麼問題?我是否以錯誤的方式編譯?如果是這樣,我在哪裏可以找到正確的方法,因爲在文檔中似乎沒有像「如何用ntl編譯」之類的東西?

如果我使用的using namespace NTL代替NTL_CLIENT沒有什麼變化:

$ ls /usr/lib | grep libntl 
libntl-5.4.2.so 
libntl.a 
libntl.so 
$ ls /usr/include | grep NTL 
NTL 
$ g++ -L/usr/lib -lntl -lgmp -lm simple.cpp 
/tmp/ccwdQkr4.o: In function `main': 
simple.cpp:(.text+0x3a): undefined reference to `NTL::operator>>(std::basic_istream<char, std::char_traits<char> >&, NTL::ZZ&)' 
simple.cpp:(.text+0x4b): undefined reference to `NTL::operator>>(std::basic_istream<char, std::char_traits<char> >&, NTL::ZZ&)' 
simple.cpp:(.text+0xda): undefined reference to `NTL::operator<<(std::basic_ostream<char, std::char_traits<char> >&, NTL::ZZ const&)' 
/tmp/ccwdQkr4.o: In function `NTL::ZZ::operator=(NTL::ZZ const&)': 
simple.cpp:(.text._ZN3NTL2ZZaSERKS0_[NTL::ZZ::operator=(NTL::ZZ const&)]+0x22): undefined reference to `_ntl_gcopy' 
/tmp/ccwdQkr4.o: In function `NTL::ZZ::~ZZ()': 
simple.cpp:(.text._ZN3NTL2ZZD2Ev[_ZN3NTL2ZZD5Ev]+0x14): undefined reference to `_ntl_gfree' 
/tmp/ccwdQkr4.o: In function `NTL::add(NTL::ZZ&, NTL::ZZ const&, long)': 
simple.cpp:(.text._ZN3NTL3addERNS_2ZZERKS0_l[NTL::add(NTL::ZZ&, NTL::ZZ const&, long)]+0x2a): undefined reference to `_ntl_gsadd' 
/tmp/ccwdQkr4.o: In function `NTL::mul(NTL::ZZ&, NTL::ZZ const&, NTL::ZZ const&)': 
simple.cpp:(.text._ZN3NTL3mulERNS_2ZZERKS0_S3_[NTL::mul(NTL::ZZ&, NTL::ZZ const&, NTL::ZZ const&)]+0x2d): undefined reference to `_ntl_gmul' 
collect2: ld returned 1 exit status 
$ cat simple.cpp 
#include <NTL/ZZ.h> 

using namespace NTL; 

int main() 
{ 
    ZZ a, b, c; 

    std::cin >> a; 
    std::cin >> b; 
    c = (a+1)*(b+1); 
    std::cout << c << "\n"; 
} 

回答

2

這些都不是編譯器錯誤。這些是鏈接器錯誤。在編譯期間包含標題是不夠的。您還需要指定鏈接期間使用的庫。我不知道NTL,因此我不知道需要包含哪個庫,但我希望在任何可用的文檔中提到它。使用Google進行快速搜索似乎表明您需要使用-lntl(假定該庫安裝在標準目錄中)。

+0

這個答案忽略了一點:1)如果你看過我的問題,你可以看到我*做*使用'-lntl',並且我還提到了NTL安裝正確的事實[是的,我剛剛說過這些頭文件位於正確的位置,但我再次檢查並且有一個libntl.so在/ usr/lib中]。 2)NTL沒有「文檔」這樣的東西。我發現了關於編譯的簡要說明,但它不能解決問題。 – Bakuriu

+1

那麼,你不使用'-lntl'來解析符號。這是正確的,我沒有看到它,但提到它不是我在尋找的地方:對象文件,包括庫,從左到右進行處理。如果您想要解決符號,請在源代碼後面提及庫。 –

+0

好的,在參數後面加上「-lX」。但我不明白爲什麼會發生這種情況。爲什麼訂單很重要?而且,我一直認爲調用一個程序的「正確」方法是'$ name [options] arguments ...',那麼爲什麼'g ++'在選項之前需要參數? – Bakuriu

0

當我第一次開始使用庫時,我遇到了同樣的問題。編譯完庫,每次使用庫項目時,你必須去 項目>添加現有項(快捷鍵:按Ctrl + + 一個),然後選擇編譯庫時生成的.lib文件。

如果這不起作用,請隨時向我發送收件箱,我可以向您發送關於如何編譯庫和使用它的詳細說明。

+0

歡迎來到StackOverflow。你的答案似乎是使用IDE(可能是Visual Studio)的框架,但是不清楚提問者是否在使用。爲了讓您的答案對他人更有價值,最好包含您對該主題的所有信息。請考慮至少添加一個編譯步驟的概述。 –

+0

@ user3727590:我不認爲在StackOverflow上邀請某人就某個問題的解決方案進行私人郵件交換是正確的......而是請幫助社區發佈一個完整的答案,闡明您想通過郵件寫入的所有細節:) –

0

喜來解決這個問題,你應該通過終端例如運行程序

g++ simple.cpp -o simple -lntl 

然後顯示結果寫

./simple