MEX compilation with external libraries errors - .lib needed?

10 views (last 30 days)
I want to call external C code (in particular, MPIR libraries) via mex on my (Windows 7 64bit) machine.
The gcc compilation outside Matlab works fine, but translating it into a workable mex compilation command gives me mex errors for seven days now. The compiler complains that it does not see any ".lib" files, and says, upon adding "-lmpir", as is required in the gcc compilation, that it looks for a file "libmpir.lib" which it does not find, and which also I do not see anywhere in the MPIR directories.
Should such files be there after a successful installation (i.e. do I have to set the MPIR build options differently)? What are there are the files mpir.h, libmpir.la, libmpir.dll.a, libmpir-3.dll.def, among many others, but eg. no file "libmpir.lib", what is mentioned in the mex documentation. The only file ending on ".lib" or ".dll" is "libmpir-16.dll" in the mpir-2.7.0/.libs directory.
I conjecture this because on this post, you can read "I was trying to link my program with ftd2xx.dll. However, for MEX files you need to link them with lib library, ftd2xx.lib. So I found and downloaded lib version of the library..."
Details and settings:
mex compile commands I tried:
mex -IC:/MPIR/mpir-2.7.0/ -LC:/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/mpf/ mexlib.cpp myfile.cpp
mex -IC:/MPIR/mpir-2.7.0/ -LC:/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/mpf/ -lmpir mexlib.cpp myfile.cpp
mex -IC:/MPIR/mpir-2.7.0/ -LC:/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/mpf/ -lmpir.dll mexlib.cpp myfile.cpp
Here 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;
}
and here 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);
}
Or is this a problem from wrong flags with mex? Or should I try to install the library (MPIR) differently, such that .lib files are produced?
I have R2015a and also tried it with R2011b. Thank you.

Answers (1)

dpb
dpb on 19 Jun 2015
Edited: dpb on 19 Jun 2015

Haven't ever used it myself, but...

The web site installation says...

"MPIR has an autoconf/automake/libtool based conguration system. ... Important note: by default MPIR produces libraries named libmpir, etc., and the header file mpir.h."

If you can compile (and link) externally, then the needed libraries do exist somewhere; your problem is that mex setup doesn't know about external non-TMW libraries and so it doesn't set the LIB environment variable to include that location (nor the INCLUDE path to find header files if they're not in local directory either).

To build this way under mex, you'll have to modify the default mexopts.bat file manually to include those locations needed or set them in an autostart routine so they're globally already included in the startup. Remember that mex spawns a new console process to execute under and that will be a virgin environment that only has those environment variables set that are either explicitly set in the batch file itself or were inherited from the parent.

ADDENDUM

Or, you could use the $COMPFLAGS$/$LINKFLAGS$ option variables when you call mex but that means specifying them every time which is why I suggested first to either modify the mexopts.bat file or add the flags to the base environment if you're going to be using this library a lot.

  13 Comments
Attila Kolekrov
Attila Kolekrov on 25 Jun 2015
Ok, thanks again, this seems too complicated to be solved by setting a path or adding flags to the mex command. I guess the libs are creating in a way that mex does not understand. I will try to find some detailed mex documentation; the one in the help does not explain this issue in a depth sufficient for this issue. Best.
dpb
dpb on 25 Jun 2015
No, if you can build from the command line, you can build a mexopts.bat file that does the same thing and then use mex.
The key is you have to progress logically and figure out how to build the DLL with that additional library after you've built a working demonstration mex file without it. At that point it really should be simply adding the reference to the additional library.

Sign in to comment.

Categories

Find more on Troubleshooting in MATLAB Compiler SDK in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!