2

我正在尝试使用 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

5 回答 5

3

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

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

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

于 2012-08-27T09:29:02.983 回答
1

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

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

我认为 gmp 库也需要与 ntl 一起使用。

于 2018-07-12T03:47:06.377 回答
0

当我第一次开始使用该库时,我遇到了同样的问题。编译完库后,每次在项目中使用该库时,都必须进入 项目>添加现有项(快捷键:Ctrl++ ShiftA并选择.lib编译库时生成的文件。

如果这不起作用,请随时收件箱给我,我可以向您发送我记录的关于如何编译和使用库的详细说明。

于 2014-07-09T20:11:19.633 回答
0

嗨,要解决这个问题,您应该通过终端运行程序,例如

 g++ simple.cpp -o simple -lntl

然后显示结果写入

./simple
于 2017-01-22T20:44:54.593 回答
0

我有一个类似的问题。我在 ubuntu 上使用 Codelite IDE。我通过更改 Setting --> Linker --> Libraries 中的顺序解决了这个问题,如下所示:

ntl gmp

当订单不同(gmp;ntl)时,我得到了错误。

所以请在gmp之前检查顺序并先移动ntl

于 2021-12-05T09:13:23.460 回答