You are currently only viewing entries from July 2007.

To go back to view all items, click here. Also, see August 2007 and June 2007
Tuesday, 10 July 2007

Installing GMP on Windows

A guide to installing GMP and GMPXX on Windows # Permalink

What is GMP?

The GNU Multi-Precision library, or GMP, is an essential component of the programmer who works with mathematics on a regular basis. The problem is, C and C++ only come with data types with fixed sizes (int, long, double, etc.), which severely limits the size of number which can be kept in memory.

The GMP library allows you to store arbitrary precision numbers (both integers and floats) with ease - indeed, with the C++ extension, GMPXX, allows you to use these data types in pretty much exactly the same way as you use, say long.

What is this guide for?

If you're working on Windows (instead of Ubuntu :)), and using Dev-C++ (a fantastic open source C/C++ compiler available under the GPL, which makes it free), then this guide should be straight forward, and works just great with these software versions:

XP 3.4.2 (version of GCC & G++) 4.2.1 (GMPXX)

If there are no errors, then this guide should work straightforwardly. If there are errors, ask about them below. Whenever you want, even if this entry is really old. We'll try, but no guarantees.

The Instructions

Starting Point

You should have Dev-C++ installed. No? Then click here. The assumed installation directory is C:\Dev-Cpp\. Be aware of this when adapting the commands below.

The installation of Dev-C++ should have put "C:\Dev-Cpp\bin" in your PATH environment variable. To check this, go to Start->Run, type "cmd", and press return. Now type "gcc --version". You should get a message like this:

gcc (GCC) 3.4.2 (mingw-special)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO

If you get a message like "'gcc' is not recognized as an internal or external command, operable program or batch file.", and you are sure you have installed Dev-C++ on Windows XP: right click on My Computer and select Properties. Click on the Advanced tab, and then click on the Environment Variables button. Look for "Path" in the bottom white box, scrolling down if necessary. Select it, click on "Edit", and go to the end of the box which appears. Add ";C:\Dev-Cpp\bin", including the semi-colon, and adjusting the "C:\Dev-Cpp" bit to whatever directory you installed Dev-C++ into.

The Preparation

  1. Go to the GMP download page. Download the archive of the latest release, a ".tar.bz2" or ".tar.gz" file - which one is irrelevant.
  2. Extract this archive to a folder like "c:\c++\GMP" or something - try to avoid spaces and capital letters. To do this, you need a good archive program - if you haven't got one that can read the file (i.e. you double-click on it and it says "Windows cannot open this file"), download and install 7-Zip (from here).
  3. Check that you have a folder like "C:\c++\GMP\GMP-4.2.1", or equivalent. This folder should contain several source files and folders.
  4. Assuming you have MinGW installed (it comes with Dev-C++), you only now need MSYS to run the "configure" script. To install this
    1. Go to the SourceForge MinGW homepage.
    2. Click on "Download MinGW - Minimalist GNU for Windows" - the big green button!
    3. Look through the "Latest File Releases" section until you see "MSYS Base System". Click on the text saying "MSYS Base System" - not the "Download" link!
    4. Find the first "Current Release" in the list in the big table, ignoring "Technology Previews", by preference. (NB: You can close any "Technology Preview" sections by clicking on the '-' symbol to the left of the heading.)
    5. Click the '+' symbol by the Current Release, which is "Current Release: msys-1.0.10" at the moment.
    6. Click on "MSYS-1.0.10.exe" (or whatever the latest version is - what is important is the ".exe" extension).
  5. Run this file - the default options should be fine.
  6. When it finishes the installation, a black prompt window appears. Confirm that you do want to normalize, and that you have a MinGW installation (type 'y' and press return), and then type in the folder where you have Dev-C++ installed, using forward slashes and lowercase: for me, that is "c:/dev-cpp". Press return.
  7. Go to "C:\Dev-Cpp\bin" (or equivalent) and make a copy of "mingw32-make.exe". Rename this copy "make.exe". This is because the MSYS installation removes "make.exe" without the prefix, causing Dev-C++ to become confused when trying to compile using a makefile.

You are now ready to compile! :D

Compilation & Installation

  1. Open the MSYS prompt (by default, there is a link in the Start Menu - Start->MinGW->MSYS->msys).
  2. Update: "cd" to the directory with your GMP files using a command like "cd /c/c++/GMP/GMP-4.2.1" (yes, of course, without the quotes, as always!) - be sure to adjust "/c/..." to your own directory - this one is equivalent to C:\c++\GMP\GMP-4.2.1. Press return.
  3. Update fix: You must fix an error which currently exists in the Makefiles. You must do one of the following:
    1. Before running ./configure (step 4), go to C:\c++\GMP\GMP-4.2.1\mpn\ and go to the last line of the file. If you find --m4="$(M4)" in the middle of it, change it to --m4=$(M4). That is, remove the double quote marks,
    2. After running ./configure (step 4), go to C:\c++\Includes\GMP\GMP-4.2.1\mpn\Makefile and also C:\c++\Includes\GMP\GMP-4.2.1\tests\Makefile and make the same replacements described just above, although not quite at the last line. Change --m4="$(M4)" to --m4=$(M4).
  4. Type in "./configure --prefix=/c/dev-cpp --enable-cxx" (yes, no quotes, and change the path as in 2. above) - and press return. Let it run - it is analyzing your system, and building the Makefiles necessary.
  5. Providing there are no errors (if there are, ask someone about them, or try to use your common sense to fix them - most errors are misspelt or incorrectly written directory names) type in "make" and press return. Let this run also - it's now compiling and linking all the code, which may take quite a while. The process compiles each source file in each category (integers, floats, etc. - as in the folder structure), producing .o and .lo files, and then links them each folder together into individual .la files.
  6. When this had finished, in my paranoia, I also ran "make check", which compiles various tests and runs them using your new (static) library. They all passed first time :P. It is recommended that you run "make check" though, since, as the GMP developers say, GMP tends to explore interesting corners of compilers, and many have fallen foul of compiler errors.
  7. Update: type "make install" and press return.
  8. (Update: you should check to ensure the ".a" libraries have been installed to your Dev-C++ directory, as in "C:\Dev-Cpp\lib". If they haven't, look for a ".libs" folder in the directory you extracted everything to.)

Now you should be ready to use the static library!

Using the Library

  1. Now, open Dev-C++, and create a new C++ project (a Console Application).
  2. Type in this code into "main.cpp" and save it:
    	#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;
  3. Go to Project->Project Options->Parameters->Linker, click "Add Library or Object", and navigate to "C:\Dev-Cpp\lib". Select the "libgmpxx.a" file, and click "Open". A new entry has appeared. Do the same, but selecting "libgmp.a". The order is important!
  4. Click OK, and compile!

A Brief Explanation

MSYS is necessary to run the "configure" shell script, and to provide the ideal environment for building. It is easier to use than Cygwin for this purpose, and quicker to install.

The "--enable-cxx" flag for the "configure" script is to allow the linking of the "libgmpxx" library, and to cause the copying of "gmpxx.h" to the installation directory.

When using GMPXX in C++, the following course of action is required:

  1. #include <gmpxx.h>
  2. libgmpxx.a
  3. libgmp.a

For the C version, it's:

  1. #include <GMP.h>
  2. libgmp.a

The C++ version overloads the operators (+, -, /, *, =, ...) to make using the library as easy as using built in operators, as the above example demonstrates. The data types in the C++ wrapper as as follows:

mpz_class: Integers, or whole numbers

mpf_class: Floats, or decimal numbers

mpq_class: Rational numbers, or fractions


Useful? Problems?

Please leave a comment (it's really easy) if this was useful, if you had problems, or especially if you have something to add!

Recent Fix No. 1:

If you get an error like

m4: gcc: No such file or directory
../mpn/m4-ccas: -c: command not found
c:\dev-cpp\bin\make.exe[2]: *** [add_n.lo] Error 1
c:\dev-cpp\bin\make.exe[2]: Leaving directory `c:/c++/GMP/GMP-4.2.3/mpn\'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `c:/c++/GMP/GMP-4.2.3\'
c:\dev-cpp\bin\make.exe: *** [all] Error 2

then go to Step 3 under Compilation & Installation above.

(See this forum thread for information.)

Recent Fix No. 2

If you're getting errors like these:

../fib_table.h:4: error: parse error before "data"
../gmp-impl.h:2547:64: operator '&&' has no right operand

Check the definition of things like GMP_LIMB_BITS in gmp.h - if they're blank, give them a nice value, such as 32 (no guarantees.) Also, read the comments below.

Posted by carl at 04:00

Filed under: Computing

Journal from July 2007

All entries from month July 2007

Simple diagrams for LaTeX with Inkscape • 1.7.2013 Yesterday I discovered how to make nice, simple, elegant diagrams for fairly painless inclusion… [read more - comment]No Data Connection (Android) • 4.6.2013 Just spent an age dealing with a phone (Samsung Galaxy S2, I9100, on the UK network 3) running a… [read more - comment]Android/BusyBox Segmentation Faults • 30.9.2012 Just had a terrifying moment when, after attempting to install BusyBox on an Android device,… [read more - comment]Temporarily Redefining In-built Mathematica Functions • 16.7.2012 Suppose a package you're using is, say, zealously Simplifying lots of Mathematica expressions… [read more - comment]Nuclear Power • 24.7.2011 I've never known the answers to the big questions about energy. I do know that with … [read more - comment]
top / xhtml / css
© Carl Turner 2008-2017
design & engine by suchideas / hosted by xenSmart