17

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:

    #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.

Badshah
  • 391
  • 1
  • 2
  • 12
  • I know I'm late, but I guess the simplest solution would be to take a look at https://gmplib.org/manual/C_002b_002b-Interface-General.html and take a look at the compile-command... – Mr Tsjolder Apr 24 '16 at 16:56

7 Answers7

18

Get the actual version here GNU GMP Library. Make sure you configure it to be installed in /usr/lib (pass --prefix=/usr to configure).

Here you have documentation: GNU GMP Manual.

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:

#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
someoneigna
  • 526
  • 4
  • 8
  • 6
    Your code use the C API and not the C++ API. The link to the libraries you provide are outdated (old versions of GMP). – chmike Mar 12 '14 at 11:04
  • 1
    "pass --prefix=/usr to configure" please don't do that, if you're using a package manager you're going to possibly clobber files. Just use your package manager to install `libgmp-dev` (for Ubuntu, other distros might have different package names), and link with `g++ -lgmp -lgmpxx ...` – Ancurio Jul 28 '18 at 18:00
  • First you output some data with `std::cout` without flushing. Then you directly outputed number with `mpz_out_str` . This would cause the output to become "The absolute value result is:" , instead of "The absolute value result is:". You should flush `std::cout` first. – Akib Azmain Oct 09 '20 at 08:06
7

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!

  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
    

Notes:

  1. 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.

  2. 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".

  3. 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. :-)

e-sushi
  • 12,227
  • 10
  • 35
  • 55
cassius
  • 376
  • 4
  • 10
7

Since there are very small examples in gmp library docs, I'm including exponentiation example for reference:

Program calculates 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

Run :./gmp_pow_test

Install gmp library on Ubuntu with following: sudo apt-get install libgmp-dev libgmpxx4ldbl

Piyush Chauhan
  • 1,364
  • 2
  • 21
  • 34
  • 3
    If you are going to write C code, you might as well include only gmp.h and compile with a C compiler, you don't need to link with gmpxx. Also, mpz_pow_ui doesn't look like an efficient way to compute a power of 2. – Marc Glisse Dec 10 '14 at 20:59
5

You need to tell the compiler what libraries you want to use.

g++ -lgmp -lgmpxx file.cpp -o file
PLampkin
  • 51
  • 1
  • 1
5

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 :).

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
sum of 1234 and -5678 is -4444
absolute value is 4444 
$
Mouse
  • 464
  • 6
  • 9
3

You'll need to tell the compiler where the header file is.

g++ test.cpp -I/path/to/directory/that/contains/the/header -o test.exe
chrisaycock
  • 32,202
  • 12
  • 79
  • 116
0

I have tried so many solutions and all of them has some problems

Here is the best way to install GMP and eclipse

Follow this link http://www.multigesture.net/articles/how-to-install-mingw-msys-and-eclipse-on-windows/

You need to make sure of the following that hasn't been mentioned there:

When installing MinGW choose a path that contains no space like "c:\MinGW"

Once installed, From start open MinGW installation manger

  • choose all the basics -then under MinGW, choose all GMP libraries to be installed
  • Apply changes

After that you will install JDK, then Add "C:\Program Files\Java\jdk1.8.0_121\bin" to PATH system variable

After installing Eclipse go to:

  • GCC C++ compiler --> Miscellaneous add -lgmp -lgmpxx to the END of the line
  • MinGW C++ linker --> Libraries Add to the "Libraries (-l)" both gmp and gmpxx IN THAT ORDER

gooooo ahead.

Temo NB
  • 1
  • 1