6 These instructions are only for the impatient. Others should read the install
7 instructions in the manual, gmp.info. Use
16 Here are some brief instructions on how to install GMP, and some examples to
17 help you get started using GMP.
19 First, you need to compile, and optionally install, GMP. Since you're
24 If that fails, or you care about the performance of GMP, you need to read the
25 full instructions in the chapter "Installing GMP", in the manual.
27 Next, try some small test programs, for example the ones below.
29 In GMP programs, all variables need to be initialized before they are
30 assigned, and cleared out before program flow leaves the scope in which they
31 were declared. Here is an example program that reads two numbers from the
32 command line, multiplies them, and prints the result to stdout.
36 #include <gmp.h> /* All GMP programs need to include gmp.h */
38 main (int argc, char **argv)
43 { printf ("Usage: %s <number> <number>\n", argv[0]); exit (1); }
45 /* Initialize variables */
50 /* Assign a and b from base 10 strings in argv */
51 mpz_set_str (a, argv[1], 10);
52 mpz_set_str (b, argv[2], 10);
54 /* Multiply a and b and put the result in p */
57 /* Print p in base 10 */
58 mpz_out_str (stdout, 10, p);
61 /* Clear out variables */
69 This might look tedious, with all the initializing and clearing. Fortunately
70 some of these operations can be combined, and other operations can often be
71 avoided. An experienced GMP user might write:
77 main (int argc, char **argv)
82 { printf ("Usage: %s <number> <number>\n", argv[0]); exit (1); }
84 /* Initialize and assign a and b from base 10 strings in argv */
85 mpz_init_set_str (a, argv[1], 10);
86 mpz_init_set_str (b, argv[2], 10);
90 /* Multiply a and b and put the result in p */
93 /* Print p in base 10 */
94 mpz_out_str (stdout, 10, p);
97 /* Since we're about to exit, no need to clear out variables */
102 Now you have to compile your test program, and link it with the GMP library.
103 Assuming your working directory is still the gmp source directory, and your
104 source file is called example.c, enter:
106 gcc -g -I. example.c .libs/libgmp.a
108 After installing, the command becomes: "gcc -g example.c -lgmp". Also, GMP is
109 libtool based so you can use that to link if you want.
111 Now try to run the example:
113 ./a.out 98365871231256752134 319378318340103345227
114 31415926535897932384618573336104570964418
116 The functions used here all operate on signed integers, and have names
117 starting with "mpz_". There are many more such functions than used in these
118 examples. See the chapter "Integer Functions" in the manual, for a complete
121 There are two other main classes of functions in GMP. They operate on
122 rational numbers and floating-point numbers, respectively. The chapters
123 "Rational Number Functions", and "Floating-point Functions" document these
126 To run a set of tests, do "make check". This will take a while.
128 To create the printable documentation from the texinfo source, type "make
129 gmp.dvi" or "make gmp.ps". This requires various "tex" commands.
131 To install the library, do "make install" (then you can use -lgmp instead of
134 If you decide to use GMP, it is a good idea you at least read the chapter "GMP
135 Basics" in the manual.
137 Some known build problems are noted in the "Installing GMP" chapter of
138 the manual. Please report other problems to bug-gmp@gnu.org.