使用NTL库时出现编译器错误

Compiler errors using NTL library

本文关键字:编译器 错误 NTL 使用      更新时间:2023-10-16

我试图使用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";
}

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

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

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

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

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

我认为gmp库与ntl一起也是必需的。

我刚开始使用这个库时也遇到了同样的问题。编译完库后,每次将库用于项目时,都必须转到项目添加现有项(快捷键:Ctrl+Shift+A

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

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

 g++ simple.cpp -o simple -lntl

然后显示结果写入

./simple

我也遇到了类似的问题。我在ubuntu上使用Codelite IDE。我通过在Setting->链接器-->库如下:

ntlgmp

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

因此,请检查订单并在gmp 之前先移动ntl