我想使用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";
}
這個答案忽略了一點:1)如果你看過我的問題,你可以看到我*做*使用'-lntl',並且我還提到了NTL安裝正確的事實[是的,我剛剛說過這些頭文件位於正確的位置,但我再次檢查並且有一個libntl.so在/ usr/lib中]。 2)NTL沒有「文檔」這樣的東西。我發現了關於編譯的簡要說明,但它不能解決問題。 – Bakuriu
那麼,你不使用'-lntl'來解析符號。這是正確的,我沒有看到它,但提到它不是我在尋找的地方:對象文件,包括庫,從左到右進行處理。如果您想要解決符號,請在源代碼後面提及庫。 –
好的,在參數後面加上「-lX」。但我不明白爲什麼會發生這種情況。爲什麼訂單很重要?而且,我一直認爲調用一個程序的「正確」方法是'$ name [options] arguments ...',那麼爲什麼'g ++'在選項之前需要參數? – Bakuriu