在Ubuntu上安装NTL

来源:互联网 发布:2017大数据产业峰会 编辑:程序博客网 时间:2024/06/05 14:19

因为项目需要,准备在Ubuntu上使用NTL,在官网上http://shoup.net/ntl/下载

Unix:    ntl-9.7.0.tar.gz下载好后,进入该包所在目录,解压该压缩包,

$tar -zxvf ntl-9.7.0.tar.gz

进入解压后的文件夹中,$cd ntl-9.7.0/

查看有哪些文件夹或文件$ ls

doc  include README  src

打开README  $ catREADME

提示:

For a detailed guideto installation, please see the appropriate

documentation:

   * doc/tour-unix.html for unix systems

   * doc/tour-win.html for Windows and othersystems

因为是在Linux上使用,所以打开doc/tour-unix.html

官网文档中也有介绍http://shoup.net/ntl/doc/tour-unix.html

按照该步骤一步一步来

因为已经解压出来了,则前面的步骤就跳过,直接

$ cd src

$ ./configure

$ make

在这里给了错误提示

gen_gmp_aux.c:26:17:fatal error: gmp.h: No such file or directory

compilationterminated.

make[1]: ***[setup3] Error 1

make[1]: Leavingdirectory `/home/insider/test/ntl-9.7.0/src'

make: *** [all]Error 2

提示说我没有gmp.h,所以先看了下安装文档后面的,其中有一句

For this to work, GMP must already be installed (most Unixdistributions already come with GMP installed, but seethis page formore details). If you really do not want to use GMP, you can pass theoption NTL_GMP_LIP=off to configure.

所以我还得先装GMPhttps://gmplib.org/ 从官网中下载gmp-6.1.0.tar.lz,然后解压

$ lunzipgmp-6.1.0.tar.lz

接着给了提示,说我还没安装lunzip

The program 'lunzip'is currently not installed.  You caninstall it by typing:

sudo apt-get installlunzip

那就安装个lunzip

$ sudo apt-getinstall lunzip

安装完后继续解压

$ lunzipgmp-6.1.0.tar.lz

生成了个gmp-6.1.0.tar文件

$tar -xvfgmp-6.1.0.tar

进入解压后的文件夹中

$ cd gmp-6.1.0/

GMP的官方文档中也写了如何安装GMPhttps://gmplib.org/manual/Installing-GMP.html#Installing-GMP

$ ./configure

这里就出现了错误

checking forsuitable m4... configure: error: No usable m4 in $PATH or /usr/5bin (seeconfig.log for reasons).

我猜测估计就是没装m4吧,直接去安装

$ sudo apt-getinstall m4

安装完后继续上述步骤

$ ./configure

这次没问题了,

$ make

$ makecheck

$ makeinstall

看到最后一行make: *** [install] Error 2

再往上看一下,/bin/mkdir: cannot create directory`/usr/local/share/info': Permission denied

原来是权限不够,还以为又哪里出错了呢

$ sudo make install

这次就没问题了,因为设置都是默认,所以应该在/usr/local/include/usr/local/lib里增加了GMP相关的头文件以及库

$ ls/usr/local/include/

gmp.h

$ ls /usr/local/lib/

libgmp.a  libgmp.la libgmp.so  libgmp.so.10  libgmp.so.10.3.0

至此,GMP是安装完了

回到NTL的安装,进入src文件夹中,重新来过一遍吧,以防万一

$ ./configure

$ make

这个过程比较久,需要多等待下,运行好几个测试

$ makecheck

$ sudomake install

检查一下

$ ls/usr/local/include/

gmp.h  NTL

$ ls /usr/local/lib/

libgmp.a   libgmp.so    libgmp.so.10.3.0  libgmp.la  libgmp.so.10 libntl.a

至此NTL也安装好了,现在来做个简单测试

官方文档给出一些例子,这里我主要使用到里边的大整数功能,主要涉及了ZZ类,相关例子的网址:http://shoup.net/ntl/doc/tour-ex1.html

#include <NTL/ZZ.h>

 

using namespace std;

using namespace NTL;

 

int main()

{

  ZZ a, b, c; 

 

  cin >> a; 

  cin >> b; 

  c = (a+1)*(b+1);

  cout << c << "\n";

}

$vim ntl_test.cc

复制上述的代码,保存后,进行编译运行

$ g++ ntl_test.cc -otest

出现以下错误

/tmp/cctWcbOG.o: Infunction `main':

ntl_test.cc:(.text+0x3a):undefined reference to `NTL::operator>>(std::basic_istream<char,std::char_traits<char> >&, NTL::ZZ&)'

ntl_test.cc:(.text+0x4b):undefined reference to `NTL::operator>>(std::basic_istream<char,std::char_traits<char> >&, NTL::ZZ&)'

ntl_test.cc:(.text+0xda):undefined reference to `NTL::operator<<(std::basic_ostream<char,std::char_traits<char> >&, NTL::ZZ const&)'

/tmp/cctWcbOG.o: Infunction `NTL::ZZ::Deleter::apply(void*&)':

ntl_test.cc:(.text._ZN3NTL2ZZ7Deleter5applyERPv[NTL::ZZ::Deleter::apply(void*&)]+0x14):undefined reference to `_ntl_gfree(void**)'

/tmp/cctWcbOG.o: Infunction `NTL::ZZ::operator=(NTL::ZZ const&)':

ntl_test.cc:(.text._ZN3NTL2ZZaSERKS0_[NTL::ZZ::operator=(NTL::ZZconst&)]+0x36): undefined reference to `_ntl_gcopy(void*, void**)'

/tmp/cctWcbOG.o: Infunction `NTL::add(NTL::ZZ&, NTL::ZZ const&, long)':

ntl_test.cc:(.text._ZN3NTL3addERNS_2ZZERKS0_l[NTL::add(NTL::ZZ&,NTL::ZZ const&, long)]+0x41): undefined reference to `_ntl_gsadd(void*,long, void**)'

/tmp/cctWcbOG.o: Infunction `NTL::mul(NTL::ZZ&, NTL::ZZ const&, NTL::ZZ const&)':

ntl_test.cc:(.text._ZN3NTL3mulERNS_2ZZERKS0_S3_[NTL::mul(NTL::ZZ&,NTL::ZZ const&, NTL::ZZ const&)]+0x4e): undefined reference to`_ntl_gmul(void*, void*, void**)'

collect2: ldreturned 1 exit status

我想是它不认识NTL的库文件在哪,所以

$ g++ ntl_test.cc -o test -L /usr/local/lib/,还是出现类似的错误,所以我想肯定是我忘了加入什么参数,但是不太记得需要加什么了,后面在http://shoup.net/ntl/doc/tour-unix.html看到了需要的参数

$ g++ -g -O2ntl_test.cc -o test -lntl -lgmp -lm

这下没问题了,如果还出错,在加入-I-L手动指定头文件以及库文件的位置

$ ./test

2

5

18

结果是对的,至此就搞定了,但对于我这种菜鸟来说,花了很长世间,中间出现了很多个问题,都花了很长时间才解决,现在回过头开看,官方文档很重要,记得先看看文档,再下手!!

1 0
原创粉丝点击