[22253] | 1 | Copyright 1996, 1997, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
---|
[15293] | 2 | |
---|
[18190] | 3 | This file is part of the GNU MP Library. |
---|
| 4 | |
---|
| 5 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|
| 6 | it under the terms of the GNU Lesser General Public License as published by |
---|
| 7 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
| 8 | option) any later version. |
---|
| 9 | |
---|
| 10 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
| 11 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
| 12 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
| 13 | License for more details. |
---|
| 14 | |
---|
| 15 | You should have received a copy of the GNU Lesser General Public License |
---|
| 16 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
| 17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
| 18 | 02111-1307, USA. |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | |
---|
[15293] | 24 | INSTALLING GNU MP |
---|
| 25 | ================= |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | These instructions are only for the impatient. Others should read the install |
---|
[18190] | 29 | instructions in gmp.info. Use |
---|
[15293] | 30 | |
---|
| 31 | info -f ./gmp.info |
---|
| 32 | |
---|
| 33 | or in emacs |
---|
| 34 | |
---|
| 35 | C-u C-h i gmp.info |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | Here are some brief instructions on how to install GMP, and some examples to |
---|
[18190] | 39 | help you get started using it. First you need to compile. Since you're |
---|
| 40 | impatient, try this |
---|
[15293] | 41 | |
---|
[18190] | 42 | ./configure |
---|
| 43 | make |
---|
[15293] | 44 | |
---|
| 45 | If that fails, or you care about the performance of GMP, you need to read the |
---|
[18190] | 46 | full instructions in the chapter "Installing GMP" in the manual. |
---|
[15293] | 47 | |
---|
[18190] | 48 | Optionally, you can install with the following. This will be to /usr/local by |
---|
| 49 | default, and you'll probably need to be "root" to be able to write there. |
---|
| 50 | |
---|
| 51 | make install |
---|
| 52 | |
---|
[15293] | 53 | Next, try some small test programs, for example the ones below. |
---|
| 54 | |
---|
| 55 | In GMP programs, all variables need to be initialized before they are |
---|
| 56 | assigned, and cleared out before program flow leaves the scope in which they |
---|
| 57 | were declared. Here is an example program that reads two numbers from the |
---|
| 58 | command line, multiplies them, and prints the result to stdout. |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | #include <stdio.h> |
---|
| 62 | #include <gmp.h> /* All GMP programs need to include gmp.h */ |
---|
| 63 | |
---|
| 64 | main (int argc, char **argv) |
---|
| 65 | { |
---|
| 66 | mpz_t a, b, p; |
---|
| 67 | |
---|
| 68 | if (argc != 3) |
---|
[18190] | 69 | { |
---|
| 70 | printf ("Usage: %s <number> <number>\n", argv[0]); |
---|
[22253] | 71 | return 1; |
---|
[18190] | 72 | } |
---|
[15293] | 73 | |
---|
| 74 | /* Initialize variables */ |
---|
| 75 | mpz_init (a); |
---|
| 76 | mpz_init (b); |
---|
| 77 | mpz_init (p); |
---|
| 78 | |
---|
| 79 | /* Assign a and b from base 10 strings in argv */ |
---|
| 80 | mpz_set_str (a, argv[1], 10); |
---|
| 81 | mpz_set_str (b, argv[2], 10); |
---|
| 82 | |
---|
| 83 | /* Multiply a and b and put the result in p */ |
---|
| 84 | mpz_mul (p, a, b); |
---|
| 85 | |
---|
[18190] | 86 | /* Print p in decimal */ |
---|
| 87 | gmp_printf ("%Zd\n", p); |
---|
[15293] | 88 | |
---|
| 89 | /* Clear out variables */ |
---|
| 90 | mpz_clear (a); |
---|
| 91 | mpz_clear (b); |
---|
| 92 | mpz_clear (p); |
---|
[22253] | 93 | return 0; |
---|
[15293] | 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | This might look tedious, with all the initializing and clearing. Fortunately |
---|
| 98 | some of these operations can be combined, and other operations can often be |
---|
| 99 | avoided. An experienced GMP user might write: |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | #include <stdio.h> |
---|
| 103 | #include <gmp.h> |
---|
| 104 | |
---|
| 105 | main (int argc, char **argv) |
---|
| 106 | { |
---|
| 107 | mpz_t a, b, p; |
---|
| 108 | |
---|
| 109 | if (argc != 3) |
---|
[18190] | 110 | { |
---|
| 111 | printf ("Usage: %s <number> <number>\n", argv[0]); |
---|
[22253] | 112 | return 1; |
---|
[18190] | 113 | } |
---|
[15293] | 114 | |
---|
| 115 | /* Initialize and assign a and b from base 10 strings in argv */ |
---|
| 116 | mpz_init_set_str (a, argv[1], 10); |
---|
| 117 | mpz_init_set_str (b, argv[2], 10); |
---|
| 118 | /* Initialize p */ |
---|
| 119 | mpz_init (p); |
---|
| 120 | |
---|
| 121 | /* Multiply a and b and put the result in p */ |
---|
| 122 | mpz_mul (p, a, b); |
---|
| 123 | |
---|
[18190] | 124 | /* Print p in decimal */ |
---|
| 125 | gmp_printf ("%Zd\n", p); |
---|
[15293] | 126 | |
---|
| 127 | /* Since we're about to exit, no need to clear out variables */ |
---|
[22253] | 128 | return 0; |
---|
[15293] | 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | Now you have to compile your test program, and link it with the GMP library. |
---|
[18190] | 133 | Assuming your working directory is still the gmp build directory, and your |
---|
[15293] | 134 | source file is called example.c, enter: |
---|
| 135 | |
---|
| 136 | gcc -g -I. example.c .libs/libgmp.a |
---|
| 137 | |
---|
| 138 | After installing, the command becomes: "gcc -g example.c -lgmp". Also, GMP is |
---|
| 139 | libtool based so you can use that to link if you want. |
---|
| 140 | |
---|
| 141 | Now try to run the example: |
---|
| 142 | |
---|
| 143 | ./a.out 98365871231256752134 319378318340103345227 |
---|
| 144 | 31415926535897932384618573336104570964418 |
---|
| 145 | |
---|
| 146 | The functions used here all operate on signed integers, and have names |
---|
| 147 | starting with "mpz_". There are many more such functions than used in these |
---|
[18190] | 148 | examples. See the chapter "Integer Functions" in the manual for a complete |
---|
[15293] | 149 | list. |
---|
| 150 | |
---|
| 151 | There are two other main classes of functions in GMP. They operate on |
---|
| 152 | rational numbers and floating-point numbers, respectively. The chapters |
---|
| 153 | "Rational Number Functions", and "Floating-point Functions" document these |
---|
| 154 | classes. |
---|
| 155 | |
---|
| 156 | To run a set of tests, do "make check". This will take a while. |
---|
| 157 | |
---|
| 158 | To create the printable documentation from the texinfo source, type "make |
---|
| 159 | gmp.dvi" or "make gmp.ps". This requires various "tex" commands. |
---|
| 160 | |
---|
| 161 | If you decide to use GMP, it is a good idea you at least read the chapter "GMP |
---|
| 162 | Basics" in the manual. |
---|
| 163 | |
---|
| 164 | Some known build problems are noted in the "Installing GMP" chapter of |
---|
| 165 | the manual. Please report other problems to bug-gmp@gnu.org. |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | ---------------- |
---|
| 170 | Local variables: |
---|
| 171 | mode: text |
---|
| 172 | fill-column: 78 |
---|
| 173 | End: |
---|