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:
Windows | Dev-C++ | MinGW | GMP |
---|---|---|---|
XP | 4.9.9.2 | 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
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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
- Go to the GMP download page. Download the archive of the latest release, a ".tar.bz2" or ".tar.gz" file - which one is irrelevant.
- 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).
- 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.
- Assuming you have MinGW installed (it comes with Dev-C++), you only now need MSYS to run the "configure" script. To install this
- Go to the SourceForge MinGW homepage.
- Click on "Download MinGW - Minimalist GNU for Windows" - the big green button!
- 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!
- 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.)
- Click the '+' symbol by the Current Release, which is "Current Release: msys-1.0.10" at the moment.
- Click on "MSYS-1.0.10.exe" (or whatever the latest version is - what is important is the ".exe" extension).
- Run this file - the default options should be fine.
- 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.
- 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
- Open the MSYS prompt (by default, there is a link in the Start Menu - Start->MinGW->MSYS->msys).
- 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.
- Update fix: You must fix an error which currently exists in the Makefiles. You must do one of the following:
- Before running ./configure (step 4), go to C:\c++\GMP\GMP-4.2.1\mpn\Makeasm.am 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,
- 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).
- 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.
- 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.
- 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.
- Update: type "make install" and press return.
- (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
- Now, open Dev-C++, and create a new C++ project (a Console Application).
- 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;
}
- 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!
- 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:
- #include <gmpxx.h>
- libgmpxx.a
- libgmp.a
For the C version, it's:
- #include <GMP.h>
- 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
Enjoy!
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.
Comments
Commenting is closed.
There are 54 comments.
c:\dev-cpp\bin\make.exe[2]: Entering directory `c:/c++/gmp/gmp-4.2.3/mpn'
C:/msys/1.0/bin/sh.exe ../libtool --mode=compile --tag=CC ../mpn/m4-ccas --m4="m 4" gcc -c -DHAVE_CONFIG_H -I. -I. -I.. -D__GMP_WITHIN_GMP -I.. -DOPERATION_`echo add_n | sed 's/_$//'` -m32 -O2 -fomit-frame-pointer -mtune=pentium3 -march=p entium3 -mno-cygwin `test -f 'add_n.asm' || echo './'`add_n.asm
../mpn/m4-ccas "--m4=m4 gcc" -c -DHAVE_CONFIG_H -I. -I. -I.. -D__GMP_WITHIN_GMP -I.. -DOPERATION_add_n -m32 -O2 -fomit-frame-pointer -mtune=pentium3 -march=pentium3 -mno-cygwin add_n.asm -o add_n.o
m4 gcc -DHAVE_CONFIG_H -D__GMP_WITHIN_GMP -DOPERATION_add_n add_n.asm >tmp-add_ n.s
m4: gcc: No such file or directory
-c -DHAVE_CONFIG_H -I. -I. -I.. -D__GMP_WITHIN_GMP -I.. -DOPERATION_add_n -m32 -O2 -fomit-frame-pointer -mtune=pentium3 -march=pentium3 -mno-cygwin tmp-add_n.s -o add_n.o
../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
Great Thanks,
Robin
Thanks for the feedback, but I'm a bit puzzled as to what you mean by putting the "other Makefile" "into the tutorial" since the only fixes needed are clearly indicated as Compilation & Installation step 3, alternatives i) and ii).
:S
./configure
make
make check (optional)
make install
A few things I found from installing GPM on different systems:
- In Step 3 of "Compilation & Installation", BOTH fixes need to be performed, and they must be done before running 'configure'.
- Also in Step 3, the two files you had named as "Makefile" are actually named "Makefile.in".
- For 64-bit WinXP and 64-bit WinVista systems, the MSYS command-prompt will just flash open and close immediately. A new "msys.bat" fixes this beautifully:
http://mingw.cvs.sourceforge.net/*checkout*/mingw/msys/dvlpr/bin/msys.bat
The other option is to download the latest version of MSYS (currently 1.0.11, but there's no installer for it yet), which contains this fix.
However, I have not run this very recently, and bow to your more contemporary knowledge :)
Cheers again.
Thank you very much for the guide, but I ran into an error I can't solve. I made the fixes you described, both in "makefile" and in "makefile.in". Here's the error message:
./gen-fac_ui 0 >mpz/fac_ui.h || (rm -f mpz/fac_ui.h; exit 1)
Usage: gen-fac_ui limbbits nailbits
make: *** [mpz/fac_ui.h] Error 1
Thanks in advance!
In file included from ../gmp-impl.h:102,
from fib_table.c:4:
../fib_table.h:4: error: parse error before "data"
In file included from fib_table.c:4:
../gmp-impl.h:2547:64: operator '&&' has no right operand
../gmp-impl.h:2735:64: operator '&&' has no right operand
../gmp-impl.h:2751:44: operator '&&' has no right operand
../gmp-impl.h:2762:44: operator '&&' has no right operand
../gmp-impl.h:2775:44: operator '&&' has no right operand
../gmp-impl.h:2790:44: operator '&&' has no right operand
fib_table.c:7: error: parse error before "data"
make[2]: *** [fib_table.lo] Error 1
make[2]: Leaving directory `/gmp/gmp-4.2.4/mpn'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/gmp/gmp-4.2.4'
make: *** [all] Error 2
Found this page discussing a related problem, but I can't solve it. This has to do with GMP_LIMB_BITS in some way.
http://swox.com/list-archives/gmp-bugs/2006-December/000655.html
Well, the double ampersand error appears to be associated with "&& GMP_LIMB_BITS == 64". The first seems related to "GMP_NUMB_BITS != 32". I'm not really up to manually parsing the configure.in file.
Feel free to try re-defining these for your own build.
If while doing the 'make install' ,u get errors related to permission access then run Msys in admin mode. But for doing that u will have to turn off the user account in the user accounts(control panel).
And the rest should all work fine.
I tried including the following header files:
#include <GMP.h> /*again as stated by you */
and in the linker the following lib : libgmp.a
I was getting the following errors in compiling the program :
In GMP.h
syntax error at '##' token
syntax error before "cmp"
syntax error at '##' token
syntax error before "add"
and so forth
Do you know if I am missing any header files for these ?
Well, the double ampersand error appears to be associated with "&& GMP_LIMB_BITS == 64". The first seems related to "GMP_NUMB_BITS != 32". I'm not really up to manually parsing the configure.in file.
It does seem to be a problem with how GMP_LIMB_BITS is defined in gmp.h. In my case it ended up blank, and after I put in 32, I was able to get past the errors that you had seen.
i need to install the GMP file on windows vista in Visual 6.0... plzz help me
i need it by thursday... plzzz...
error===
Executing g++.exe...
g++.exe "D:\my program\tt1.cpp" -o "D:\my program\tt1.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
D:\my program\tt1.cpp:2:20: gmpxx.h: No such file or directory
D:\my program\tt1.cpp: In function `int main()':
D:\my program\tt1.cpp:5: error: `mpz_class' undeclared (first use this function)
D:\my program\tt1.cpp:5: error: (Each undeclared identifier is reported only once for each function it appears in.)
D:\my program\tt1.cpp:5: error: expected `;' before "a"
....and so on...
program===
#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";
and so on.....
[BTW, I strongly recommend using Cygwin or other cross-compilers, since this method of compilation is more a hack than a wise method.]
Thank you very very much.(But why isn't devpack working?)
>Compiler: Default compiler
>Building Makefile: "F:\Dev-Cpp\Makefile.win"
>Executing make...
>make.exe -f "F:\Dev-Cpp\Makefile.win" all
>g++.exe main.o -o "Project2.exe" -L"F:/Dev-Cpp/lib" >lib/libgmpxx.a lib/libgmp.a
>
>/mingw/lib/crt2.o(.text+0x167):crt1.c: undefined >reference to `__cpu_features_init'
>collect2: ld returned 1 exit status
>make.exe: *** [Project2.exe] Error 1
>Execution terminated
Any ideas?
All went very smoothly.
God bless you.
[Linker error] undefined reference to `__cpu_features_init'
ld returned 1 exit status
C:\Documents and Settings\pintu\Desktop\How To Install GMP in Dev-Cpp\Makefile.win [Build Error] [gmp_sum.exe] Error 1
help me pls. I am using Dev-C++ with gmp 4.3.2
Pls help me ...
Really, I mean, even though we want fastness, it's a hell to install this thing!
Can't they just provide the files?
I used MSYS-1.0.11 (http://downloads.sourceforge.net/mingw/MSYS-1.0.11.exe).
I had to add a line in the linker settings in Dev-C++ to enable GMP.
Originally, I was getting errors such as [Linker error] undefined reference to `__gmpz_init'
For future reference, to fix this in Dev-C++, go to Tools - Compiler Options - Compiler
Click 'Add these commands to the linker command line', and in the box below it, type:
-lgmp
Click OK, and then compile your program (ensuring that you have put #include <gmp.h> at the top for C programs).
"
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."
I cannot find the folder or equivalent. I download gmp--5.2.0 in my windows 7 64 bit laptop. How to fix it or is it possible to install this fantastic library on Windows 7 platform?
Thanks!
[Linker error] undefined reference to `__dyn_tls_init_callback'
[Linker error] undefined reference to `__cpu_features_init'
ld returned 1 exit status
C:\Dev-Cpp\Makefile.win [Build Error] [main.exe] Error 1
Any idea what might be causing this? Also, once this works in Dev-C++, will it also work in other IDEs, such as Code::Blocks? If not, is there an easy way to get it to work?
Thanks,
Vince
By the way the path where my compiler is:
c: \dev-cpp
that's all thanks in advance.
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/../../../../include/string.h:40:39: error: expected identifier or '(' before 'do'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/../../../../include/string.h:40:39: error: expected identifier or '(' before 'while'
nextprime.c: In function '__gmp_nextprime':
nextprime.c:86:3: warning: pointer targets in initialization differ in signednes s [-Wpointer-sign]
make[2]: *** [nextprime.lo] Error 1
make[2]: Leaving directory `/c/c++/GMP/gmp-5.0.5'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/c/c++/GMP/gmp-5.0.5'
make: *** [all] Error 2
[Linker error] undefined reference to `__gmpz_clear'
[Linker error] undefined reference to `__gmpz_set_str'
[Linker error] undefined reference to `__gmpz_set_si'
[Linker error] undefined reference to `__gmpz_init'
[Linker error] undefined reference to `operator<<(std::ostream&, __mpz_struct const*)'
[Linker error] undefined reference to `operator<<(std::ostream&, __mpz_struct const*)'
[Linker error] undefined reference to `operator>>(std::istream&, __mpz_struct*)'
[Linker error] undefined reference to `__gmpz_init'
[Linker error] undefined reference to `__gmpz_add'
[Linker error] undefined reference to `__gmpz_set'
ld returned 1 exit status
C:\Dev-Cpp\work\Makefile.win [Build Error] [main.exe] Error 1
Do respond. Urgent need.
(Sorry to everyone I don't respond to, but this guide is rather outdated and I have a degree etc. to worry about!)
It worked like a charm for me. Was very useful and one of the best tutorials on the web. I liked the way you explained everything.
make
I received the following error
./gen-fac_ui 32 0 >mpz/fac_ui.h || (rm -f mpz/fac_ui.h; exit 1)
./gen-fac_ui: ./gen-fac_ui: cannot execute binary file
c:\dev-cpp\bin\make.exe: *** [mpz/fac_ui.h] Error 1
Plzzz help
" .drectve `-aligncomm:"___gmp_junk",2' unrecognized
[Linker error] undefined reference to `_Unwind_Resume'
[Linker error] undefined reference to `__gxx_personality_v0'
[Linker error] undefined reference to `std::ctype<char>::_M_widen_init() const'
[Linker error] undefined reference to `__chkstk_ms' "
Any ideas?