C++ 使用GMP库的c++程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14126393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ program using GMP library
提问by Badshah
I have installed GMP using the instruction on this website: http://www.cs.nyu.edu/exact/core/gmp/Then I looked for an example program using the library:
我已经使用本网站上的说明安装了 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;
}
But if I compile this using the command: g++ test.cpp -o test.exe, it says gmpxx.h: no such file or directory. How can I fix this? I am kind of new to this. And I am using MinGW.
但是,如果我使用以下命令编译它:g++ test.cpp -o test.exe,它会说 gmpxx.h:没有这样的文件或目录。我怎样才能解决这个问题?我对此有点陌生。我正在使用 MinGW。
回答by someoneigna
Get the actual version here GNU GMP Library. Make sure you configure it to be installed in /usr/lib (pass --prefix=/usr to configure).
在此处获取GNU GMP 库的实际版本。确保将其配置为安装在 /usr/lib 中(通过 --prefix=/usr 进行配置)。
Here you have documentation: GNU GMP Manual.
这里有文档:GNU GMP 手册。
You are not using the lib correctly. I don't know if you can directly access mpx values with C++ functions but, here you have a working example of what you wanted to achieve:
您没有正确使用该库。我不知道您是否可以使用 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;
}
Compile with:
编译:
g++ -lgmp file.cpp -o file
回答by cassius
Here is the correct procedure for setting up the current (as of 7/2/13) GNU bignum libraries with Eclipse CDT, MinGW, and msys for C++. To get through this, you should have used Unix or Linux before, as well as Windows, and you should have a vague recollection of programming and compiling programs. This is the culmination of over a week of research and hardcore frustration, so if I messed something up note it politely or I will blow you up with the power of my mind!
以下是使用 Eclipse CDT、MinGW 和 msys for C++ 设置当前(截至 2013 年 7 月 2 日)GNU bignum 库的正确过程。要解决这个问题,您之前应该使用过 Unix 或 Linux,以及 Windows,并且您应该对编程和编译程序有一个模糊的回忆。这是一个多星期的研究和铁杆挫折的高潮,所以如果我把某些东西搞砸了,请礼貌地记下,否则我会用我的思想力量炸毁你!
I assume you have already downloaded and installed Eclipse and MinGW and have installed msys into MinGW. You must install MinGW before msys!
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.
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.
cd gmp-x.x.x and run ./config.guess. Write down the output. You will need it next.
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.
make
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.
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.
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
我假设您已经下载并安装了 Eclipse 和 MinGW,并且已经将 msys 安装到 MinGW 中。你必须在 msys 之前安装 MinGW!
将 GMP 库的 tarball 从 gmplib.org 下载到 ${gmp_download}。我下载了 gmp-5.1.2.tar.xz,因为我从来没有用过 lzip,也不知道 msys 里有没有。
打开一个 msys 窗口(本质上是一个 bash shell)。cd ${gmp_buid} 和 tar -Jxvf ${gmp_download}/gmp-xxxtar.xz
这些 tar 选项与您可能在网络上的其他地方找到的不同!-Jxvf 适用于 xz(我认为是 lzip),但对于 gzip,您使用 -xzvf。
cd gmp-xxx 并运行 ./config.guess。写下输出。接下来你会需要它。
运行 ./configure --prefix=${gmp_build} --build= --enable-cxx --with-gnu-ld
显然,如果您没有明确告诉 GMP 为您的平台构建,它会构建所有内容,这很糟糕。cxx 选项构建 C++ 库,--with-gnu-ld 允许它与 ld 一起使用。很简单。
制作
进行安装
例如:假设您安装到 C:/gmp。你应该有 gmp/include/gmp.h 和 gmpxx.h。你还应该有 gmp/lib/libgmp.a、libgmp.la、libgmpxx.a、libgmpxx.la。您还应该有一个包含内容的共享目录。
设置日食:
- 转到项目--> 属性
- 在 C/C++ build --> Environment 下编辑 PATH 变量并添加 ${gmp_build}/include;${gmp_build}/lib
- 在 C/C++ build --> settings --> tool settings --> GCC Assembler --> general 添加 ${gmp_build}/include 作为包含路径。
- 相同的地方,但 --> GCC C++ 编译器 --> 包含添加 ${gmp_build}/include 作为包含路径。
- 相同的地方 --> GCC C++ 编译器 --> 杂项将 -lgmp -lgmpxx 添加到行尾。这条线的尽头!
- 相同的地方 --> GCC C 编译器添加与以前相同的包含路径和杂项选项。
- 相同的地方 --> MinGW C++ 链接器 --> 库 按该顺序将 gmp 和 gmpxx 添加到“库 (-l)”中!现在将 ${gmp_build}/lib 添加到“图书馆搜索路径 (-L)”
- 在 C/C++ General --> Paths & Symbols --> Incudes Tab 下检查您的包含目录中是否有 ${gmp_build}/include 用于汇编、C 和 C++。如果它们不存在,则您可能搞砸了前面的步骤。它们应该由 Eclipse 自动填充。
- 相同的地方 --> 库选项卡检查您是否按该顺序有 gmp 和 gmpxx。它应该已经被填充了。
- 相同的地方 --> 库路径选项卡检查 ${gmp_build}/lib 应该已经存在。点击“应用”并确保重建索引,否则更改不会生效。点击确定关闭。
运行这个简短的程序来验证您的设置:
#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; }
您的编译命令应该类似于:
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
Notes:
笔记:
The order of the options is important. I don't know all of the whys, but if the second command line (which links the program) has the -lgmp -lgmpxx flags before the -o option, the linking will fail miserably.
The -l flag is a tricky one. It actually says "Go look in -L for liblibrary.a". In this case "Go look in C:\gmp\lib for libgmp.a and libgmpxx.a".
I have heard of bugs involving cout and the 64 bit version of eclipse, so I am using the 32 bit version, where I am seeing the same bug. :-)
选项的顺序很重要。我不知道所有的原因,但是如果第二个命令行(链接程序)在 -o 选项之前有 -lgmp -lgmpxx 标志,链接将失败。
-l 标志是一个棘手的标志。它实际上是说“去 -L 中查找 liblibrary.a”。在这种情况下,“在 C:\gmp\lib 中查找 libgmp.a 和 libgmpxx.a”。
我听说过涉及 cout 和 64 位版本的 eclipse 的错误,所以我使用的是 32 位版本,在那里我看到了相同的错误。:-)
回答by Piyush Chauhan
Since there are very small examples in gmp library docs, I'm including exponentiation example for reference:
由于 gmp 库文档中有非常小的示例,因此我包含了求幂示例以供参考:
Program calculates 2 ^ 20000
程序计算 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;
}
Compile:g++ -o gmp_pow_test gmp_pow_test.cpp -lgmp
编译:g++ -o gmp_pow_test gmp_pow_test.cpp -lgmp
Run :./gmp_pow_test
跑 :./gmp_pow_test
Install gmp library on Ubuntu with following: sudo apt-get install libgmp-dev libgmpxx4ldbl
使用以下命令在 Ubuntu 上安装 gmp 库: sudo apt-get install libgmp-dev libgmpxx4ldbl
回答by Mouse
It is probably too late to be useful, but...
现在有用可能为时已晚,但是......
First, your program works just fine. As others pointed out, you need to (a) ensure that GMP library is installed (including its gmpxx extension, and all the relevant files), and (b) that you're telling the compiler where to find both the include files, and the libraries to link with. In my case include files are in /opt/local/include, and the libraries are in /opt/local/lib (where Macports placed them :).
首先,您的程序运行良好。正如其他人指出的那样,您需要 (a) 确保安装了 GMP 库(包括其 gmpxx 扩展名和所有相关文件),以及 (b) 您告诉编译器在哪里可以找到包含文件,以及要链接的库。在我的例子中,包含文件在 /opt/local/include 中,而库在 /opt/local/lib (Macports 放置它们的地方:)。
Here's the code:
这是代码:
#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;
}
Here's the compile/link command:
这是编译/链接命令:
clang++ -o gmpxx-tst -I/opt/local/include gmpxx-tst.cpp -L/opt/local/lib -lgmpxx -lgmp
Here's what invocation of gmpxx-tst produces:
以下是 gmpxx-tst 的调用产生的结果:
$ ./gmpxx-tst
sum of 1234 and -5678 is -4444
absolute value is 4444
$
回答by PLampkin
You need to tell the compiler whatlibraries you want to use.
您需要告诉编译器您要使用哪些库。
g++ -lgmp -lgmpxx file.cpp -o file
回答by chrisaycock
You'll need to tell the compiler wherethe header file is.
您需要告诉编译器头文件在哪里。
g++ test.cpp -I/path/to/directory/that/contains/the/header -o test.exe