C++程序使用GMP库

20

我使用这个网站上的指南安装了GMP:http://www.cs.nyu.edu/exact/core/gmp/,然后我寻找了一个使用该库的示例程序:

    #include <iostream>
#include <gmpxx.h>
using namespace std;
int main (void) {
mpz_class a, b, c;
a = 1234;
b = "-5678";
c = a+b;
cout << "sum is " << c << "\n";
cout << "absolute value is " << abs(c) << "\n";
cin >> a;
return 0;
}

但是,如果我使用命令g++ test.cpp -o test.exe进行编译时,它会显示:gmpxx.h:没有那个文件或目录。我该如何解决这个问题?我对这方面还比较新手。而且我正在使用MinGW。


我知道我来晚了,但我想最简单的解决方案就是查看https://gmplib.org/manual/C_002b_002b-Interface-General.html并查看编译命令... - Mr Tsjolder
7个回答

19

在这里获取实际版本 GNU GMP Library。确保您将其配置为安装在 /usr/lib 中(通过传递 --prefix=/usr 给 configure 命令)。

这里提供文档:GNU GMP Manual

您没有正确使用该库。我不知道您是否可以直接使用 C++ 函数访问 mpx 值,但是这里有一个可行的示例,可以实现您想要的目标:

#include<iostream>
#include<gmp.h>

using namespace std;

int main (int argc, char **argv) {

    mpz_t a,b,c;
    mpz_inits(a,b,c,NULL);

    mpz_set_str(a, "1234", 10);
    mpz_set_str(b,"-5678", 10); //Decimal base

    mpz_add(c,a,b);

    cout<<"\nThe exact result is:";
    mpz_out_str(stdout, 10, c); //Stream, numerical base, var
    cout<<endl;

    mpz_abs(c, c);
    cout<<"The absolute value result is:";
    mpz_out_str(stdout, 10, c);
    cout<<endl;

    cin.get();

    return 0;
}

使用以下方式进行编译:

g++ -lgmp file.cpp -o file

6
你的代码使用了C API而非C++ API。你提供的库链接已经过时(是旧版本的GMP)。 - chmike
1
请不要这样做,如果您使用软件包管理器,可能会覆盖文件。只需使用软件包管理器安装libgmp-dev(对于Ubuntu,其他发行版可能具有不同的软件包名称),并使用g++ -lgmp -lgmpxx ...进行链接即可。记得在链接时加上库参数。 - Ancurio
首先,您使用std::cout输出一些数据而不刷新。然后,您直接使用mpz_out_str输出数字。这将导致输出变为“<number>绝对值结果是:”,而不是“绝对值结果是:<number>”。您应该先刷新std::cout - Akib Azmain Turja
为什么它默认安装到“/usr/local”,但你建议安装到“/usr/lib”? - intrigued_66

9
以下是使用Eclipse CDT、MinGW和msys为C++设置当前(截至2013年7月2日)GNU bignum库的正确步骤。在完成此步骤之前,您应该先使用过Unix或Linux以及Windows,并且应该对编程和编译程序有一个模糊的记忆。这是一周多的研究和极度沮丧的结果,所以如果我出错了,请礼貌地指出,否则我会用我的思维力量炸掉你!
  1. I assume you have already downloaded and installed Eclipse and MinGW and have installed msys into MinGW. You must install MinGW before msys!

  2. Download the tarball for the GMP libraries from gmplib.org to ${gmp_download}. I downloaded the gmp-5.1.2.tar.xz because I have never used lzip and didn't know if it was available in msys.

  3. Open up an msys window (essentially a bash shell). cd ${gmp_buid} and tar -Jxvf ${gmp_download}/gmp-x.x.x.tar.xz

    Those tar options are different from what you may find elsewhere on the web! -Jxvf is right for xz (and I think lzip), but for gzip you use -xzvf.

  4. cd gmp-x.x.x and run ./config.guess. Write down the output. You will need it next.

  5. Run ./configure --prefix=${gmp_build} --build= --enable-cxx --with-gnu-ld

    Apparently if you don't explicitly tell GMP to build for your platform it builds everything, which is bad. The cxx option builds the C++ libraries and --with-gnu-ld allows it to work with ld. Pretty straightforward.

  6. make

  7. make install

    EX: suppose you installed to C:/gmp. You should have gmp/include/gmp.h and gmpxx.h. You should also have gmp/lib/libgmp.a, libgmp.la, libgmpxx.a, libgmpxx.la. You should also have a share directory with stuff in it.

  8. Set up eclipse:

    • Go to project --> properties
    • Under C/C++ build --> Environment edit the PATH variable and add ${gmp_build}/include;${gmp_build}/lib
    • Under C/C++ build --> settings --> tool settings --> GCC Assembler --> general add ${gmp_build}/include as an include path.
    • Same place but --> GCC C++ compiler --> Includes add ${gmp_build}/include as an include path.
    • Same place --> GCC C++ compiler --> Miscellaneous add -lgmp -lgmpxx to the END of the line. THE END OF THE LINE!
    • Same place --> GCC C compiler Add the same include paths and miscellaneous options as before.
    • Same place --> MinGW C++ linker --> Libraries Add to the "Libraries (-l)" both gmp and gmpxx IN THAT ORDER! Now add ${gmp_build}/lib to "LIbrary Search Path (-L)"
    • Under C/C++ General --> Paths & Symbols --> Incudes Tab check that you have ${gmp_build}/include in your include directories for Assembly, C, and C++. If they aren't there you may have messed up an earlier step. They should be auto populated by Eclipse.
    • Same place --> Libraries Tab check that you have gmp and gmpxx IN THAT ORDER. It should already be populated.
    • Same Place --> Library Paths Tab Check for ${gmp_build}/lib which should already be there. Hit "Apply" and make sure you rebuild the index or the changes won't take. Hit OK to close out.
  9. Run this short program to verify your setup:

    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <gmp.h>
    #include <gmpxx.h>
    
    using namespace std;
    
    int main ()
    {
        mpz_t p;
        mpz_init_set_ui (p,3);
    
        return 0;
    }
    

    Your compile commands should look similar to this:

     g++ "-IC:\gmp\include" -O0 -g3 -Wall -c -fmessage-length=0 -lgmp -lgmpxx -o main.o "..\main.cpp" g++ "-LC:\gmp\lib" -o GMPDebug.exe main.o -lgmp -lgmpxx
    
注释:
  1. 选项的顺序很重要。我不知道所有的原因,但如果第二个命令行(链接程序)在-o选项之前有-lgmp -lgmpxx标志,链接将失败。

  2. -l标志是一个棘手的标志。它实际上是说“去-L中查找liblibrary.a”。在这种情况下,“去C:\ gmp \ lib中查找libgmp.a和libgmpxx.a”。

  3. 我听说过关于cout和64位版本的eclipse的错误,所以我正在使用32位版本,在那里我看到了同样的错误。:-)


7

由于gmp库文档中只有很少的示例,因此我提供了一个指数运算的示例作为参考:

该程序计算2 ^ 20000

#include <iostream>
#include <gmp.h>

using namespace std;
int main(void) {
  mpz_t result, base;
  mpz_inits(result,base,NULL);
  mpz_set_str(base, "2", 10);
  mpz_pow_ui(result, base, 20000);
  mpz_out_str(stdout, 10, result);
  return 0;
}

编译:g++ -o gmp_pow_test gmp_pow_test.cpp -lgmp

运行:./gmp_pow_test

在Ubuntu上安装gmp库,执行以下命令:sudo apt-get install libgmp-dev libgmpxx4ldbl


4
如果你打算编写C代码,最好只包含gmp.h并使用C编译器进行编译,不需要链接gmpxx。另外,mpz_pow_ui看起来不是计算2的幂的有效方法。 - Marc Glisse

5
可能已经太晚了不会有用,但是...
首先,你的程序运行得非常好。正如其他人指出的那样,你需要 (a) 确保 GMP 库已经安装(包括它的 gmpxx 扩展和所有相关文件),以及 (b) 告诉编译器在哪里找到包含文件和需要链接的库。
在我的情况下,包含文件在 /opt/local/include 中,而库在 /opt/local/lib 中(这是 Macports 放置它们的位置 :)。
以下是代码:
#include <iostream>
#include <gmpxx.h>

using namespace std;

int main (void) {
    mpz_class a, b, c;

    a = 1234;
    b = "-5678";
    c = a+b;

    cout << "sum of " << a << " and " << b << " is " << c << "\n";
    cout << "absolute value is " << abs(c) << "\n";
    // cin >> a;
    return 0;
}

这是编译/链接命令:

clang++ -o gmpxx-tst -I/opt/local/include gmpxx-tst.cpp -L/opt/local/lib -lgmpxx -lgmp

下面是调用gmpxx-tst的��果:

$ ./gmpxx-tst
sum of 1234 and -5678 is -4444
absolute value is 4444 
$

这在处理大数时是行不通的,这也是整个问题的关键所在。当用一个大于长整型 0xffff'ffff'ffff'ffff 的数初始化变量 "a" 时,任何更大的数都会导致编译器警告:"警告:整数常量超出其类型的范围"。 - undefined

5
你需要告诉编译器你想要使用哪些库。
g++ -lgmp -lgmpxx file.cpp -o file

3

您需要告诉编译器头文件在哪里。

g++ test.cpp -I/path/to/directory/that/contains/the/header -o test.exe

它仍然不起作用。我是否还应该做些什么? - Badshah
1
@llonesmiz,现在它显示“找不到 -lgmpxx”和“找不到 -lgmp”。为什么会这样?我该如何修复它? - Badshah
我说在哪里找到 #includes,-L 表示在哪里找到 -l 参数(希望你不需要 -R)。man ld。 - Marc Glisse

0

我尝试了很多解决方案,但它们都有一些问题。

以下是安装 GMP 和 Eclipse 的最佳方法

请按照此链接进行操作: http://www.multigesture.net/articles/how-to-install-mingw-msys-and-eclipse-on-windows/

您需要确保以下内容未在链接中提到:

在安装MinGW时选择不含空格的路径,例如“c:\MinGW”

安装完成后,从开始菜单打开MinGW安装管理器

  • 选择所有基本组件 -然后,在MinGW下,选择要安装的所有GMP库
  • 应用更改

之后,您将安装JDK,然后将“C:\ Program Files \ Java \ jdk1.8.0_121 \ bin”添加到PATH系统变量中

安装Eclipse后,转到:

  • GCC C++编译器 --> 杂项添加 -lgmp -lgmpxx 到行末
  • MinGW C++链接器 --> 库添加到 "Libraries (-l)" 中,按照 gmp 和 gmpxx 的顺序添加

请继续。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接