gpt4 book ai didi

c++ - 使用 NTL 库的编译器错误

转载 作者:搜寻专家 更新时间:2023-10-31 01:52:46 25 4
gpt4 key购买 nike

我正在尝试使用 NTL 库,但我总是遇到有关 undefined symbol 的编译器错误。

示例(取自 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 header 位于 /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";
}

最佳答案

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

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

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

关于c++ - 使用 NTL 库的编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12139285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com