简体   繁体   English

使用 NTL 库的编译器错误

[英]Compiler errors using NTL library

I'm trying to use NTL library, but I always get compiler errors about undefined symbols.我正在尝试使用 NTL 库,但我总是收到有关未定义符号的编译器错误。

Example (taken from the NTL documentation):示例(取自 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";
}

Result:结果:

$ 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

The NTL headers are in /usr/include/NTL , so they should be included. NTL 标头位于/usr/include/NTL ,因此应包含它们。

What's the problem?有什么问题? Am I compiling in the wrong way?我是否以错误的方式编译? If so, where can I find the right way, since it seems like there is not such a thing like "how to compile with ntl" in the documentation?如果是这样,我在哪里可以找到正确的方法,因为文档中似乎没有“如何使用 ntl 编译”这样的内容?

If I use using namespace NTL instead of NTL_CLIENT nothing changes:如果我使用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";
}

These are not compiler errors.这些不是编译器错误。 These are linker errors.这些是链接器错误。 It isn't sufficient to include the header during compilation.在编译期间包含头文件是不够的。 You also need to specify the libraries being used during linking.您还需要指定链接期间使用的库。

I don't know about NTL so I don't know which library needs to be included but I would expect that it is mentioned somewhere in whatever documentation is available.我不知道 NTL,所以我不知道需要包含哪个库,但我希望在任何可用的文档中都会提到它。 A quick search with Google seems to indicate that you need to use -lntl (assuming the library is installed in a standard directory).使用 Google 快速搜索似乎表明您需要使用-lntl (假设该库安装在标准目录中)。

Note that traditionally C compilers process the command line from left to right, so you should add -lntl before the source file that uses the NTL library.请注意,传统 C 编译器从左到右处理命令行,因此您应该使用 NTL 库的源文件之前添加-lntl

I had the same issue and after reading the NTL docs, I used the following:我遇到了同样的问题,在阅读了 NTL 文档后,我使用了以下内容:

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

I think the gmp library is also required alongside with ntl.我认为 gmp 库与 ntl 一起也需要。

I had somewhat of the same problem when I first started using the library.当我第一次开始使用这个库时,我遇到了一些同样的问题。 After having compiled the library, every time you use the library for a project, you have to go to Project > Add Existing Item (shortcut key: Ctrl + Shift + A ) and select the .lib file you produced when compiling the library.编译库后,每次将库用于项目时,都必须转到“项目” >“添加现有项” (快捷键: Ctrl + Shift + A )并选择编译库时生成的.lib文件。

If this doesn't work, feel free to Inbox me and I can send you detailed instructions I have documented on how to Compile the library and utilize it.如果这不起作用,请随时收件箱我,我可以向您发送我记录的有关如何编译库和利用它的详细说明。

Hi to solve this problem you should run program by terminal for example嗨,要解决这个问题,您应该通过终端运行程序,例如

 g++ simple.cpp -o simple -lntl

then to show result write然后显示结果写

./simple

I had a similar issue.我有一个类似的问题。 I was using Codelite IDE on ubuntu.我在 ubuntu 上使用 Codelite IDE。 I solved the issue by changing the order in Setting --> Linker --> Libraries as follows:我通过更改设置 --> 链接器 --> 库中的顺序解决了这个问题,如下所示:

ntl gmp ntl GMP

When the order is different (gmp;ntl) then I was getting the error.当顺序不同 (gmp;ntl) 时,我收到错误消息。

So please check the order and move ntl first before gmp所以请检查顺序并在gmp之前先移动ntl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM