How to Install and Run GMP on Windows Using MPIR

http://www.exploringbinary.com/how-to-install-and-run-gmp-on-windows-using-mpir/


To perform arbitrary-precision arithmetic in C and C++ programs on Windows, I use GMP. In particular, I use MPIR, a Windows port of GMP. MPIR is a simple alternative to using GMP under Cygwin or MinGW.

I will show you how to install MPIR in Microsoft Visual C++ as a static, 32-bit library. I will also show you how to install the optional C++ interface — also as a static, 32-bit library. I will provide two example C programs that call the GMP integer and floating-point functions, and two equivalent C++ programs — programs that use the same GMP functions, only indirectly through the C++ interface.

1. Download What You Need

To install MPIR and run it under Visual C++ as I have, you’ll need four things:

(I won’t describe how to install Visual C++ or 7-zip.)

2. Prepare to Build MPIR

MPIR must be built with Visual C++; to prepare for that, do the following:

  1. From mpir-1.3.1.tar.gz, use your file archiver to extract mpir-1.3.1.tar; from mpir-1.3.1.tar, extract folder mpir-1.3.1.
  2. Copy \mpir-1.3.1\build.vc9\yasm.rules to the Visual C++ project defaults directory (mine is C:\Program Files\Microsoft Visual Studio 9.0\VC\VCProjectDefaults\).
  3. Rename file yasm-0.8.0-win32.exe to yasm.exe.
  4. Move yasm.exe to the Visual C++ binary directory (mine is C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\).

3. Build MPIR

Here’s how to build MPIR as a static Win32 library:

  1. Open the MPIR Visual C++ solution \mpir-1.3.1\build.vc9\mpir.sln.

    You get the following error message if you are using Visual Studio Express:

    The project consists entirely of configurations that require support for platforms which are not installed on this machine. The project cannot be loaded.

    64-bit projects error message

    This message is referring to the 64-bit projects, which we won’t be using anyway. Hit ‘Enter’ to ignore (do this eight times — one for each 64-bit project in the solution.)

  2. Right click on “Solution ‘mpir’ (14 projects)”, select “Properties” and then click on “Configuration Properties” and then click on “Configuration Manager”. In the box labeled “Active solution configuration” select “Release” and then click “Close” and then click “OK”.

    Change to Release

  3. Build the version of the library you want by right clicking on the project name and selecting “Build” (I built lib_mpir_p4, labeled in the readme file as the “MPIR library using Pentium IV assembler”).

    Build lib_mpir_p4

    A successful build of lib_mpir_p4, which should take less than two minutes, gives this message:

    Build: 6 succeeded, 0 failed, 0 up-to-date, 0 skipped

    (You can ignore the compiler warning messages.)

  4. Optional: for C++ interface only: Build the C++ interface by right clicking on project lib_mpir_cxx and selecting “Build” (again, ignore the compiler warning messages).

4. Install the MPIR Library in Visual C++

To install MPIR in Visual C++, you need to copy three files from the MPIR build directory into corresponding Visual C++ directories:

  • Copy files mpir.lib and mpir.pdb from

    \mpir-1.3.1\build.vc9\lib_mpir_p4\Win32\Release\

    to the Visual C++ library directory

    C:\Program Files\Microsoft Visual Studio 9.0\VC\lib\.

  • Copy \mpir-1.3.1\build.vc9\lib_mpir_p4\Win32\Release\mpir.h

    to the Visual C++ include directory

    C:\Program Files\Microsoft Visual Studio 9.0\VC\include\.

Optional: for C++ interface only: To install the library for the C++ interface, you need to copy three files from the MPIR build directory into corresponding Visual C++ directories:

  • Copy files mpirxx.lib and mpirxx.pdb from

    \mpir-1.3.1\build.vc9\lib_mpir_cxx\Win32\Release\

    to the Visual C++ library directory

    C:\Program Files\Microsoft Visual Studio 9.0\VC\lib\.

  • Copy \mpir-1.3.1\build.vc9\lib_mpir_cxx\Win32\Release\mpirxx.h

    to the Visual C++ include directory

    C:\Program Files\Microsoft Visual Studio 9.0\VC\include\.

(Unless you want to build another library, you can delete the folder mpir-1.3.1 at this point, and perhaps file mpir-1.3.1.tar.gz too. You might also want to wait until you run the test programs below.)

5. Configure Your Visual C++ Project to Use MPIR

  • Open an existing Visual C++ solution or create a new one.
  • Right click on the project name and select “Properties”. Then click “Configuration Properties”, then click “Linker”, and then click “Command Line”. In the “Additional options” box, enter “mpir.lib” and click OK.

    Add library to command line

    Optional: for C++ interface only: Also add “mpirxx.lib” to the command line:

    Add C++ library to command line

  • Right click on the project name and select “Properties”. Then click “Configuration Properties”, then click “C/C++”, and then click “Code Generation”. In the “Runtime Library” pulldown menu, select “Multi-threaded (/MT)” and click OK. This takes care of the linker warning message you would otherwise get:

    LINK : warning LNK4098: defaultlib ‘LIBCMT’ conflicts with use of other libs; use /NODEFAULTLIB:library

Writing a C/C++ Program that Uses the Standard GMP Interface

To access the GMP functions through their standard interface from your C/C++ source code, you must include the file “mpir.h”, as follows:

#include <mpir.h>

(A vanilla GMP program would have the line “#include <gmp.h>” instead; other than this, there are no source code differences.)

The MPIR GMP functions are documented in the MPIR manual, which is essentially the same document as the GMP manual.

Here are two C programs I wrote that you can use to test out the standard GMP interface: one uses some integer functions, and one uses some floating-point functions:

C Program Using GMP Integers Through the Standard Interface

#include <stdio.h>
#include <mpir.h>

int main (int argc, char *argv[])
{
 mpz_t aBigPO2;

 mpz_init(aBigPO2);

 mpz_set_ui(aBigPO2, 1073741824); //2^30
 mpz_mul(aBigPO2,aBigPO2,aBigPO2); //2^60
 mpz_mul(aBigPO2,aBigPO2,aBigPO2); //2^120
 mpz_mul(aBigPO2,aBigPO2,aBigPO2); //2^240
 mpz_mul(aBigPO2,aBigPO2,aBigPO2); //2^480
 mpz_mul(aBigPO2,aBigPO2,aBigPO2); //2^960
 mpz_mul(aBigPO2,aBigPO2,aBigPO2); //2^1920

 mpz_out_str(stdout,10,aBigPO2);
 printf ("\n");

 mpz_clear(aBigPO2);
}

This prints out 21920:

949711451807891414054698636958849699906924706346851167428009563305851
662866960338751057874083211050161729488483879798993810787765480587192
741530384819193300769874625884321977783469748956377553448566093328992
717820774610081821193616932757859144579109671494034728110890670954570
186561270637912025593911079819522904974136715161890547150302121514577
299257466073410681074505560366912534455201581754427662731068044605805
987604257959314070588213630129796572870132647963130222671409082294912
848599974253399700073940596408585364978789157781640247045138282505908
97948604589281308443672576

(I verified this with PARI/GP).

C Program Using GMP Floating-Point Numbers Through the Standard Interface

#include <stdio.h>
#include <mpir.h>

int main (int argc, char *argv[])
{
 mpf_t aSmallPO2;

 mpf_init2(aSmallPO2,4449); //Set precision to 4449 bits

 mpf_set_d (aSmallPO2,0.000000000931322574615478515625); //2^-30
 mpf_mul(aSmallPO2,aSmallPO2,aSmallPO2); //2^-60
 mpf_mul(aSmallPO2,aSmallPO2,aSmallPO2); //2^-120
 mpf_mul(aSmallPO2,aSmallPO2,aSmallPO2); //2^-240
 mpf_mul(aSmallPO2,aSmallPO2,aSmallPO2); //2^-480
 mpf_mul(aSmallPO2,aSmallPO2,aSmallPO2); //2^-960
 mpf_mul(aSmallPO2,aSmallPO2,aSmallPO2); //2^-1920

 mpf_out_str (stdout,10,0,aSmallPO2);
 printf ("\n");

 mpf_clear(aSmallPO2);
}

In case you are wondering, 4449 bits of precision is the minimum needed to print all the digits of 2-1920, which is:

0.10529513970757941370610747782095090628278032322332406685248932058897
4872190707039601594120403123249585501787935873212314961661043417067942
8278180446530326422046737097860094653249850843254396524641486587776526
8438334467392843186701182578047022952222095279213542847547625013806063
5939813263124389547821599195698069457805581599831394945162148920227813
5870110642683804987806409062559492005002245124038200955046244238057665
8723954500997837640072880232154236046343680085877727078528072722123195
3662151119904110195371481974425580486352182740921723969072266817056082
2079226689557687055189455076290288097855736909213224995450090355633626
2479360506670537902070512869428970881638135729845035256195565406246915
5908284846389916152190520040559448133294815988479693870277001617833335
8035796655478612315835426745796222381146595433091161056422873002023624
5605524128583533967685249775398427450933606825973311143113749366383842
6445031772775456966272872821230559421629513369329254203751128364614621
2033824114891508165551708120643223406118819602960923135752402206346995
9597949051896076865883172740955991670305216873490456538967283758977304
0869715545540580641662604687640672610266478149937775606620121327969356
0952412990814675362368055269333370619407233848969981813715084977716048
9153948872988676360548263714707910434915092735830288717124858521856367
588043212890625e-577

(I verified this with PARI/GP).

Writing a C++ Program that Uses the GMP C++ Interface

(To use the C++ interface you will have had to have followed the build steps labeled ‘optional’ above.)

To access the GMP functions through their C++ interface, you must include the file “mpirxx.h”, as follows:

#include <mpirxx.h>

(A vanilla GMP program would have the line “#include <gmpxx.h>” instead.)

Also, your source code will require this additional line, if you want to remove the compiler warnings due to mpirxx.h:

#pragma warning(disable: 4800)

Without this, you will get about a dozen warning messages like this:

c:\program files\microsoft visual studio 9.0\vc\include\mpirxx.h(1610) : warning C4800: ‘int’ : forcing value to bool ‘true’ or ‘false’ (performance warning)

The MPIR GMP C++ interface is documented in the MPIR manual (and the GMP manual).

Here are two C++ programs I wrote that you can use to test out the GMP C++ interface: they are the equivalent of the two C programs above, except they’re expressed much more naturally (as to be expected in a C++ program):

C++ Program Using GMP Integers Through the C++ Interface

#include <iostream>
using namespace std;

#pragma warning(disable: 4800)
#include <mpirxx.h>
#pragma warning(default: 4800)

int main (int argc, char *argv[])
{   
  mpz_class aBigPO2;

  aBigPO2 = 1073741824; //2^30
  aBigPO2*=aBigPO2; //2^60
  aBigPO2*=aBigPO2; //2^120
  aBigPO2*=aBigPO2; //2^240
  aBigPO2*=aBigPO2; //2^480
  aBigPO2*=aBigPO2; //2^960
  aBigPO2*=aBigPO2; //2^1920

  cout << aBigPO2 << endl;
}

(This gives the same output as the C program above.)

C++ Program Using GMP Floating-Point Numbers Through the C++ Interface

#include <iostream>
#include <iomanip>
using namespace std;

#pragma warning(disable: 4800)
#include <mpirxx.h>
#pragma warning(default: 4800)

int main (int argc, char *argv[])
{
  mpf_class aSmallPO2(0,4449); //Init to 0, precision 4449 bits

  aSmallPO2 = 0.000000000931322574615478515625; //2^-30
  aSmallPO2*=aSmallPO2; //2^-60
  aSmallPO2*=aSmallPO2; //2^-120
  aSmallPO2*=aSmallPO2; //2^-240
  aSmallPO2*=aSmallPO2; //2^-480
  aSmallPO2*=aSmallPO2; //2^-960
  aSmallPO2*=aSmallPO2; //2^-1920

  cout << setprecision (1343) << aSmallPO2 << endl;
}

The value of 1343 to setprecision is the precision in decimal digits.

The output differs slightly from it’s corresponding C program (the number is shifted one place and has the corresponding exponent to match):

1.05295139707579413706107477820950906282780323223324066852489320588974
8721907070396015941204031232495855017879358732123149616610434170679428
2781804465303264220467370978600946532498508432543965246414865877765268
4383344673928431867011825780470229522220952792135428475476250138060635
9398132631243895478215991956980694578055815998313949451621489202278135
8701106426838049878064090625594920050022451240382009550462442380576658
7239545009978376400728802321542360463436800858777270785280727221231953
6621511199041101953714819744255804863521827409217239690722668170560822
0792266895576870551894550762902880978557369092132249954500903556336262
4793605066705379020705128694289708816381357298450352561955654062469155
9082848463899161521905200405594481332948159884796938702770016178333358
0357966554786123158354267457962223811465954330911610564228730020236245
6055241285835339676852497753984274509336068259733111431137493663838426
4450317727754569662728728212305594216295133693292542037511283646146212
0338241148915081655517081206432234061188196029609231357524022063469959
5979490518960768658831727409559916703052168734904565389672837589773040
8697155455405806416626046876406726102664781499377756066201213279693560
9524129908146753623680552693333706194072338489699818137150849777160489
1539488729886763605482637147079104349150927358302887171248585218563675
88043212890625e-578

Besides the much cleaner syntax of the C++ programs, the underlying GMP functions — “*_init”, “*_clear”, “*_mul”, etc. — are hidden in the C++ classes.

For Further Information

This article is my distillation of the MPIR readme file for a specific build scenario — please refer to \mpir-1.3.1\build.vc9\readme.txt for details on other builds.

Acknowledgement

Thanks to Brian Gladman for answering my questions and for suggesting I explore the C++ interface.

Dingbat

88 Responses to “How to Install and Run GMP on Windows Using MPIR”

  1. Rick Regan Says:

    @Chan,

    I am still using exactly the setup I describe in this article (MPIR 1.3.1, Visual Studio 2008, etc.) (If it ain’t broke, don’t fix it ūüôā .) If I ever upgrade I’ll be sure to update my instructions. In the meantime, maybe you can find some help at mpir.org. Thanks and good luck.

  2. Chan Says:

    @Rick Regan: Thanks again for your quick response. It’s kinda odd when the build process failed only on C++. But I really don’t know how were you be able to use the same “exact” set up though. Because the mpir-2.40 that I downloaded doesn’t have the “\mpir-1.3.1\build.vc9\yasm.rules” but “vsyasm.props, vsyasm.targets, and vsyasm.xml”. Anyhow, I’m look forward to seeing your new tutorial on a newer version MPIR 2.40 ;). And of-course thanks for the link to further help.

  3. Rick Regan Says:

    @Chan,

    I haven’t rebuilt since I wrote this article (March 2010), so I am in fact using the versions of everything as listed.

  4. Chan Says:

    @Rick: Would you mind rebuilding the 2.4.0 version to help me address this issue? Thanks in advance ūüėČ

  5. Rick Regan Says:

    @Chan,

    Perhaps you can get help at the mpir-devel group. Please let us know if you resolve your issue.

  6. Rick Regan Says:

    Regarding @Chan’s problem: he was building the 32-bit library for a 64-bit system (see his question on stackoverflow.)

  7. boredchimp Says:

    Trying to compile mpir-3.0.0 using the tutorial here but there isn’t \build.vc10\yasm.rules

    What should I do?

  8. Rick Regan Says:

    @boredchimp,

    I’m still using my old build of MPIR — perhaps you can get help at the mpir-devel group? Sorry.

  9. Rick Regan Says:

    This is a comment I received by email from Anil Philip:

    For those wanting to build a newer version of mpir, just go to mpir-2.5.1\win and run the two batch files in a command window. You will need to download yasm, rename and copy over yasm.exe to C:\Program Files\Microsoft Visual Studio 9.0\VC\bin.

  10. Maxvell Says:

    Nice job Rick ! thanks a lot, It is very helpful

  11. Roberto Says:

    Hi, one question, would you mind telling me how to install everything but for visual 2010?.

    Thanks

  12. Rick Regan Says:

    @Roberto,

    Sorry, I have not upgraded to VS 2010 yet.

  13. Ahmet Says:

    Thank you for your tutorial, I followed it and after some struggle I managed to work out your c++ example. However when I try to run this code:

    mpq_class x(1), y(1), z(1), a(93, 10), b(98, 10);

    for (int i = 0; i < 15; i++)
    {
    x *= a;
    y *= b;
    z *= (a * b);
    }
    //works good so far
    //std::cout << z << "\n" << (x*y) << std::endl;
    //gives error:
    Error 26 error LNK1169: one or more multiply defined symbols found
    Error 13 error LNK2005: "public: __int64 __thiscall std::basic_streambuf<char,struct std::char_traits >::sputn(char const *,__int64)” (?sputn@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QAE_JPBD_J@Z) already defined in mpirxx.lib(osdoprnti.obj)

    and some more errors. This works when I write the following though:
    cout << z.get_d() << endl;

    However, I want to be able to display the numbers in a ratio format. Any ideas?

  14. Ahmet Says:

    By the way, I am using 64 bit Visual Studio 2010. But I built it in Win32, and your example works fine.

  15. Rick Regan Says:

    Ahmet,

    Sorry, I have not tried it on 64-bit.

  16. Dejan Says:

    Thank you for the tutorial. All your examples work for me, except last line in floating point c++ interface: cout << setprecision (1343) << aSmallPO2 << endl; Even without setprecision is not building. I download mpir-2.2.1 as the last version supporting VS2008, and build lib_mpir_gc instead lib_mpir_p4. I build in release mode – 32bit version on XP. The error is same as kusha reported above. Do you know how to resolve this problem?

  17. Rick Regan Says:

    @Dejan,

    Sorry, I’m still on my original build, even on my 64-bit machine.

  18. Sparse Matrices and Large Integers in Visual Studio 2012 | BlogoSfera Says:

    […] with Visual Studio 2010?, Can not find mpirxx.h after building the mpir-2.6.0 library in vc++ and http://www.exploringbinary.com/how-to-install-and-run-gmp-on-windows-using-mpir/, I have finally managed to get MPIR to run in Visual Studio 2012. But I also need to do calcuations […]

  19. JD Says:

    On step 4, files mpir.lib and mpir.pdb from mpir version 2.6 aren’t present, searched the entire directory tree, can get everything else to work though.

  20. JD Says:

    never mind, figured it out, thanks

  21. Geisi Says:

    the file is not yasm-0.8.0-win32.exe

  22. Rick Regan Says:

    @Geisi,

    I don’t know what you mean. The link in my article points to yasm-0.8.0-win32.exe.

  23. Ella Says:

    Thanks alot!
    If you’re trying to build and it fails, try downloading yasm from here http://yasm.tortall.net/Download.html and download vsyasm, not yasm! It worked for me.

    Also check out the readme.txt at build.vc10, it might help.

  24. Adam Says:

    What is a licence of your code ? ( I would like to put it in wikiboks , like here :
    https://pl.wikibooks.org/wiki/Programowanie_w_systemie_UNIX/GMP
    With info/link to your page )

  25. Rick Regan Says:

    @Adam,

    It’d be best if you just linked to my site instead of copying the code. It’s not that I want to protect the code per se, but my articles are copyrighted. I appreciate the interest though. Thanks for asking.

  26. William Boyd Says:

    Any chance of updating this for Visual Studio 10 and vsyasm @ http://yasm.tortall.net/Download.html .

    I had no luck trying following your instructions with these (mpir.lib just isn’tt there).

    Thanks.

    William Boyd

  27. Rick Regan Says:

    @William,

    Sorry, I still use this old setup. I don’t know how to set it up with the new releases.

  28. anny Says:

    lib_mpir_p4 built is failed
    giving error
    yasm’ is not recognized as an internal or external command,
    6>operable program or batch file.
    6>Project : error PRJ0019: A tool returned an error code from “Assembling submul_1.asm”

  29. Brian Gladman Says:

    @Anny

    It is likely that you have not installed vsyasm.exe as set out in the readme.txt file in the Visual Studio build directory of the MPIR distribution. I am the Windows developer of MPIR and will be happy to give any advice you need in getting this to work. However if you have further questions, you are likely to get a faster answer if you ask on the MPIR developer list at mpir-devel@googlegroups.com

    best regards,
    Brian Gladman

  30. david edwards Says:

    I did all that you suggested but when I went to install
    1.Open the MPIR Visual C++ solution \mpir-1.3.1\build.vc9\mpir.sln.
    it brought up visual c++ but required updates for all and you couldn’t ignore that. each project had 2 errors and when it was done the solution had 0 projects rather than 14.

    what am I doing wrong?

    thank you

    david

  31. Rick Regan Says:

    @David,

    I’m sorry that I can’t help you (these build instructions are over four years old and I haven’t built a new version). Maybe you should try mpir-devel@googlegroups.com as indicated in comment #79 above.

  32. How to install mpir in Visual Studio 2012 | Understanding the Art of Programming Says:

    […] example I was unable to build the mpir library with my VS 2013..all kinds of errors). This is the article I mentioned. The solution is to download a pre built library, so click here to download it. Extract […]

  33. Robert Steed Says:

    I’m trying to get this to work for mpir-2.6.0 on MsVC 2013 Express. The build works (I chose lib_mpir_gc). I copied mpir.lib and mpir.pdb from “mpir-2.6.0\build.vc10\Win32\Release” to “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib”, and mpir.h from “mpir-2.6.0\lib\Win32\Release” or “mpir-2.6.0” (the two copies are identical) to “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include”. When I try to build the first test program above (prints out 2^1920) in a new (empty) project, I get this error message:
    error C1083: Cannot open source file: ‘mpir.lib’: No such file or directory
    Any ideas?

  34. Robert Steed Says:

    Forgot to say, for the new empty project:
    …followed step 5. – project ‚ÄúProperties‚ÄĚ > ‚ÄúConfiguration Properties‚ÄĚ > ‚ÄúLinker‚ÄĚ > ‚ÄúCommand Line‚ÄĚ > ‚ÄúAdditional options‚ÄĚ :- mpir.lib.
    Also did this project ‚ÄúProperties‚ÄĚ > ‚ÄúConfiguration Properties‚ÄĚ > ‚ÄúC/C++‚ÄĚ > ‚ÄúCode Generation‚ÄĚ > ‚ÄúRuntime Library‚ÄĚ :- ‚ÄúMulti-threaded (/MT)‚ÄĚ.

  35. Rick Regan Says:

    @Robert,

    Sorry, I have not tried the build on newer versions. Perhaps try the MPIR Google group (see comments above).

  36. gmp.h /mpir.h linker in visual studio 2012 professional Says:

    […] I am currently working on visual studio 2012 professional. I followed the instructions from the link. But still I got error […]

  37. Anil Says:

    I followed your steps. It all gone well. I successfully Built the project. But when I run the program it said that …

    “The program can’t start because mpir.dll is missing from your computer”

  38. Rick Regan Says:

    (This article refers to old versions and is thus out-of-date — I have closed comments.)


css.php