0

I want to use an external library (here the (high precision arithmetic) library MPIR, but I guess the problem applies to other libraries as well) in my C code which is called via MEX from Matlab. To this end, I built MPIR using MinGW via the commands make, make install and make check, which performed without error (nevertheless I am not completely sure whether all libraries are in the correct places (see locations of file described below) - how can I test that?). Now I set up a small test program myfile.cpp with header myfile.h, which looks like the following:

// this is myfile.cpp
#include "mex.h"
#include "mpir.h"
#include "myfile.h"
#include <stdio.h>

void myfile(double a)
{
    mpf_t b;
    mp_bitcnt_t bct = 350;
    double f = 10.0, g = 3.0;

    mpf_set_default_prec(bct);  
    mpf_init(b);
    mpf_set_d (b, f);
    gmp_printf("b is %.*Ff \n",120,b );
    mpf_clear(b);       
}

where the various calls refer to high precision types of the MPIR library, with header file

// this is myfile.h
#include "mex.h"
#include "mpir.h"
#include <stdio.h>

void myfile(double a);

I hoped to call this routine by the mex file mexlib.cpp, which reads:

// this is mexlib.cpp
#include "mex.h"
#include "mpir.h"
#include "myfile.h"
#include <stdio.h>

void mexFunction(
         int          nlhs,
         mxArray      *plhs[],
         int          nrhs,
         const mxArray *prhs[]
         )
{
    double a;
    a        = mxGetScalar(prhs[0]);        /* create pointer to the real data in the input arguments  */
    myfile(a);                              /* call the computational routine */    
    return;    
}

The MPIR base directory is C:/MPIR/mpir-2.7.0/, in which I can see the files mpir.h, gmp.h, libmpir.la, among many others, and folders /.libs/, /mpf/, /mpz/, etc. In the /.libs/ folder, there are the files libmpir.la, libmpir.dll.a, libmpir-16.dll, libmpir-3.dll.def, among many others, but eg. no file "libmpir.a" or libmpirdll.a", which I was looking for when learning from some other questions here.

In Matlab, I tried to compile it via mex using the commands
mex -IC:/MPIR/mpir-2.7.0/ -LC:/MPIR/mpir-2.7.0/.libs mexlib.cpp myfile.cpp -v , or
mex -IC:/MPIR/mpir-2.7.0/ -LC:/MPIR/mpir-2.7.0/ mexlib.cpp myfile.cpp -v .

It responded with errors stating:

"myfile.obj : error LNK2019: unresolved external symbol __imp___gmpf_clear referenced in function "void __cdecl myfile(double)" (?myfile@@YAXN@Z)"

and similar lines for the other functions called in myfile.cpp , and exited with

"mexlib.mexw64 : fatal error LNK1120: 5 unresolved externals."  

In the mex documentation, I read that the flags -L and -l point to locations with files ending with .lib (on Windows).
How can I point it to the ".h" files - or is it not what I need? I tried to add some paths to the PATH environment variable, also the MinGW paths, which did not help.
It is a Windows 7 PC with the only compiler found by Matlab being the Microsoft SDK 7.1. Using the -v flag shows as "COMPILER = cl"... is this the SDK? There is also gcc somewhere ( I guesss in the MinGW folder?).
I guess I am confused of all the #includes, header file constructs in C and compiler options; then, of most help to me would be an illumination of which ones are needed (best with an example).
Thanks in advance!

Futurist
  • 75
  • 1
  • 8
  • Can you tell us what version of Matlab you are running, it's often crucial to know in troubleshooting MEX issues. – paisanco Jun 18 '15 at 01:20
  • You have to add the .libs for the all the other libraries (gmp.lib, etc.) to the command line. – chappjc Jun 18 '15 at 05:31
  • Ok; it is R2011b. @chappjc: What is the command to add the other libraries to the command line? E.g. "-lgmp"? Which other libraries do you mean - mpf, mpz etc.? In the folder /MPIR/mpir-2.7.0/mpf/, there is a file "libmpf.la", but I understood the compiler (at least gcc, how about the SDK mex is using?) prepones "lib" and postpones ".a" to the name ("mpf"), but not ".la"; is this still the required file? Thanks! – Futurist Jun 18 '15 at 09:42
  • Don't worry about adding lib to the beginning. You can literally list the exact name of the library after the source files. Figure out what .lib has `gmpf_clear` and what library has the `mpf_*` commands. List them on the command line. – chappjc Jun 18 '15 at 17:31
  • Ok, so should I somewhere in the MPIR directory see files whose names end with “.lib”? Or find the init etc. functions in a directory called /.libs ? I can nowhere find such files. I do have files ending on “.a” and “.la” and also ending on “.o” and “.lo”, such as “libmpir.la” and “libmpir.dll.a” in the /.libs/ direct., or “libmpf.a”, “libmpf.la” and “init.o” in the /mpf/.libs directory , or “init.lo” in the /mpf / directory. Thus, eg for mpf_init, I found the files “init.c” and “init.lo” in the latter, but “init.o” in the former. To which one should I link? Also, with –L or –I? – Futurist Jun 18 '15 at 20:29
  • Further – could it be that “.dll” files are required (I read that they would be the dynamic analogue to “.lib” files)? The only such file is “libmpir-16.dll” in /.libs. I now tried the command: “mex -IC:/MPIR/mpir-2.7.0/ -LC:/MPIR/mpir-2.7.0/.libs/ -LC:/MPIR/mpir-2.7.0/mpf/.libs -LC:/MPIR/mpir-2.7.0/printf/.libs mexlib.cpp myfile.cpp” , which still returned “myfile.obj : error LNK2019: unresolved external symbol __imp___gmpf_clear referenced in function "void __cdecl myfile(double)" (?myfile@@YAXN@Z)” for every function in myfile.cpp. I did also configure MPIR with --enable-shared... (ok?) – Futurist Jun 18 '15 at 20:30
  • You can do it without -l, but with -L to specify the path to them. You literally can list the actual file name at the end of the command line so you don't have to mess with the lower case -l syntax. The .dll.a file is the "import library" for the dll (the dynamic runtime library). The plain .a files are usually the static libraries. The .la and .lo files are "Libtool" files that probably aren't relevant. Try linking with libmpir.dll.a, which should give your MEX file a dependency on the .dll, and maybe also link with libmpf.a. Just keep trying until the linker is happy. – chappjc Jun 18 '15 at 21:33
  • Did you find [this previous answer](http://stackoverflow.com/questions/8552580/using-gcc-mingw-as-matlabs-mex-compiler) about compiling Mex with MinGW? Not sure if it will help but for what it's worth. – paisanco Jun 19 '15 at 01:13
  • How "linking with libmpir.dll.a"? adding "-lmpir.dll" did not work. gcc compiling of a test program involving the mpir commands in the MinGW shell works. But translating this into mex fails. I suspect that, as stated in [this post](http://www.mathworks.com/matlabcentral/answers/107106-error-lnk2019-unresolved-external-symbol-when-compiling-mex) mex needs ".lib" files, which I do not have yet (can they be produced during the MPIR library compilation?). Do you agree? Any further ideas are welcome. – Futurist Jun 19 '15 at 11:41
  • @paisanco: Thanks, no it did not help, it should be possible to compile this with mex anyway, I am just not sure I have all the correct files in my external (MPIR) library or if any flags for mex are missing or incorrect. – Futurist Jun 19 '15 at 11:45

0 Answers0