title: GMP with MinGW on Windows
body: <p>I managed to build the GMP library successfully on my Windows 7 (64-bit computer). The steps I followed were:</p>
  
  <pre><code>./configure --enable_cxx --disable-static --enable-shared --prefix="/c/MinGW"
  make
  make install
  make check
  </code></pre>
  
  <p>All of the tests pass successfully. Clearly, I'm at the point where it's possible to compile and run GMP programs. But, when I try to compile the following program:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;gmp.h&gt;
  
  char *progname;
  
  void print_usage_and_exit ()
  {
      fprintf (stderr, "usage: %s -q nnn\n", progname);
      fprintf (stderr, "usage: %s nnn ...\n", progname);
      exit (-1);
  }
  
  int main (int argc, char **argv)
  {
      mpz_t n;
      int i;
  
      progname = argv[0];
  
      if (argc &lt; 2)
          print_usage_and_exit ();
  
      mpz_init (n);
  
      if (argc == 3 &amp;&amp; strcmp (argv[1], "-q") == 0)
      {
          if (mpz_set_str (n, argv[2], 0) != 0)
              print_usage_and_exit ();
          exit (mpz_probab_prime_p (n, 25) == 0);
      }
  
      for (i = 1; i &lt; argc; i++)
      {
          int result;
          if (mpz_set_str (n, argv[i], 0) != 0)
              print_usage_and_exit ();
          result = mpz_probab_prime_p (n, 25);
          mpz_out_str (stdout, 10, n);
          if (result == 0)
              puts (" is composite");
          else if (result == 1)
              puts (" is a probable prime");
          else /* result == 2 */
              puts (" is a prime");
      }
      exit (0);
  }
  </code></pre>
  
  <p>with the command:</p>
  
  <pre><code>gcc -m32 -lgmp main.c
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o:main.c:(.text+0x7f): undefined reference to `_imp____gmpz_init'
  C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o:main.c:(.text+0xc3): undefined reference to `_imp____gmpz_set_str'
  C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o:main.c:(.text+0xe2): undefined reference to `_imp____gmpz_probab_prime_p'
  C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o:main.c:(.text+0x12b): undefined reference to `_imp____gmpz_set_str'
  C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o:main.c:(.text+0x14a): undefined reference to `_imp____gmpz_probab_prime_p'
  C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o:main.c:(.text+0x170): undefined reference to `_imp____gmpz_out_str'
  c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: C:\Users\Alex\AppData\Local\Temp\cc2ZHzEt.o: bad reloc address 0x20 in section `.eh_frame'
  c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
  collect2.exe: error: ld returned 1 exit status
  </code></pre>
  
  <p>Can anyone tell what I'm doing wrong? I've done my Googling, and after a few too many hours, any help you can offer would be appreciated. Thanks!</p>