title: GNU GMP with Matlab and mex - Library Problems
body: <p>I am using Matlab (R2015a) together with Windows SDK 7.1 to compile a single C++ source file with Matlab's "mex" functionality. This works all fine. 
  However, in this C++-file, I'd like to use the GNU GMP library (arbitrary precision arithmetic/bigint), since I would appreciate some big integer calculations.
  Therefore, i downloaded gmp-6.0.0 and set up the Cygwin toolchain to compile my GMP libraries for my Win-7 (64-bit) machine. </p>
  
  <p>I compile/configure GMP this way in Cygwin:</p>
  
  <pre><code>$ configure --host=none-pc-cygwin --enable-alloca=malloc-notreentrant  --enable-cxx --enable-static --disable-shared
  $ make
  $ make check
  $ make install
  </code></pre>
  
  <p>... which gives me the header- and library files without errors or problems (gmp.h, gmpxx.h) as well as the libraries (libgmp.a, libgmpxx.a). </p>
  
  <p>In my C++-program, I include the header:</p>
  
  <pre><code>#include "gmpxx.h"
  </code></pre>
  
  <p>... and i copy the libraries/headers to the same matlab working directory. 
  In my C++-code, I now try to use the GMP library, e.g. like this:</p>
  
  <pre><code>mpz_t val1;
  mpz_init(val1);
  </code></pre>
  
  <p>I then call the matlab/mex/Windows SDK compiler (in Matlab) like so:</p>
  
  <pre><code>mex TestProgram.cpp -v libgmp.a libgmpxx.a
  </code></pre>
  
  <p>... which returns the following linker error:</p>
  
  <pre><code>Creating library TestProgram.lib and object TestProgram.exp
  TestProgram.obj : error LNK2019: unresolved external symbol __gmpz_init referenced in function mexFunction
  TestProgram.mexw64 : fatal error LNK1120: 1 unresolved externals
  </code></pre>
  
  <p>... And I do not know why it does this. Apparently, there are functions missing in the GMP library I compiled (?)... but why? Or is it a problem between mex and the windows SDK compiler?</p>
  
  <p>Any suggestions of how to debug this? I already searched extensively, but the combination of mex and GMP is rarely used ==> little search results :-/</p>
  
  <p>Edits:</p>
  
  <ul>
  <li>Do I have to supply/link an additional library/DLL since Cygwin is used to compile?</li>
  <li>Could it be a name mangling issue? (I also tried a purely C-based fuction/compilation, but with the exact same result)</li>
  <li>If I compile a dynamic library with Cygwin (and supply the DLLs to matlab), I get the same linker error</li>
  </ul>
  
  <p>Best and many thanks!
  Mario</p>