Compiling libraries with MEX in MATLAB

877 views Asked by At

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!

0

There are 0 answers