2012-08-27 40 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