使用NTL库编译器错误

时间:2012-08-27 09:23:22

标签: c++ compiler-errors 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";
}

4 个答案:

答案 0 :(得分:3)

这些不是编译器错误。这些是链接器错误。在编译期间包含头是不够的。您还需要指定链接期间使用的库。

我不知道NTL所以我不知道需要包含哪个库但是我希望在任何可用的文档中都提到它。使用Google快速搜索似乎表明您需要使用-lntl(假设该库安装在标准目录中)。

请注意,传统上C编译器从左到右处理命令行,因此您应该在使用NTL库的源文件之前添加-lntl

答案 1 :(得分:0)

当我第一次开始使用该库时,我遇到了同样的问题。编译完库后,每次将库用于项目时,都必须转到 项目&gt; 添加现有项(快捷键: Ctrl + Shift + A )并选择.lib文件你在编译库时产生的。

如果这不起作用,请随意收件箱,我可以向您发送详细说明,记录如何编译库并使用它。

答案 2 :(得分:0)

您好解决此问题,您应该按终端运行程序,例如

 g++ simple.cpp -o simple -lntl

然后显示结果写

./simple

答案 3 :(得分:0)

我遇到了同样的问题,在阅读NTL文档后,我使用了以下内容:

g++ -g -O2 -std=c++11 -pthread -march=native foo.cpp -o foo -lntl -lgmp -lm

我认为与ntl一起还需要gmp库。