使用NTL库时出现编译器错误
Compiler errors using NTL library
我试图使用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
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 编译器错误:destuctor 的更宽松的抛出说明符
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- C++ G++ 编译器 - 错误:隐式声明的定义
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 模板模板参数和模板别名:编译器错误?
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- C1001内部编译器错误是由于矢量初始化(如数组)引起的
- 编译器错误(英特尔并行工作室 2019 与 Visual Studio 社区 2019)
- 如何在C++中克服此 C4430 编译器错误?
- 尝试使用 WinBGI 显示文本时出现编译器错误
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- C++自定义分配器大小参数作为模板参数会引发编译器错误