If you use the MinGW® compiler to build a MEX file that links to a library compiled with a
non-MinGW compiler, such as Microsoft®
Visual Studio®, the file will not run in MATLAB®. Library (.lib) files generated by different
compilers are not compatible with each other.
You can generate a new library file using the dlltool utility
from MinGW.
Do not install MinGW in a location with spaces in the path name. For example, do not use:
C:\Program Files\mingw-64
Instead, use:
C:\mingw-64
If you only have the MinGW compiler installed on your system, the mex
command automatically chooses MinGW for both C and C++ MEX files. If you have multiple C or C++ compilers,
use mex -setup to choose MinGW for both C and, if required, C++ MEX files.
mex -setup mex -setup cpp
If you only type mex -setup choosing MinGW, when you compile a C++ file, mex might choose a
different compiler.
When you install MinGW from the MATLAB Add-Ons menu, MATLAB automatically detects the MinGW compiler.
If necessary, you can manually configure MinGW, if you have Windows® administrative privileges, using the configuremingw
script. To download this script, see the MATLAB Answers article "I already have MinGW on my computer. How do I configure it to work with
MATLAB".
When modifying compiler flags using the mex command, use the
Linux® compiler flags CFLAGS or
CXXFLAGS instead of the Windows flag COMPFLAGS.
Error handling in C++ MEX files compiled with the MinGW-w64 compiler is not
consistent with MATLAB error handling. If a C++ MEX file contains a class, using the
mexErrMsgIdAndTxt function to throw a MEX exception can cause
a memory leak for objects created for the class.
MathWorks recommends that you use the C++ MEX API instead of the C Matrix API. For more information, see C++ MEX Applications.
For example, the following C++ MEX function contains class
MyClass.
#include "mex.h"
class MyClass {
public:
MyClass() {
mexPrintf("Constructor called");
}
~MyClass() {
mexPrintf("Destructor called");
}
};
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
MyClass X;
if (nrhs != 0) {
mexErrMsgIdAndTxt("MATLAB:cppfeature:invalidNumInputs",
"No input arguments allowed.");
}
}
The MEX function creates object X from
MyClass, then checks the number of input arguments. If the
MEX function calls mexErrMsgIdAndTxt, the MATLAB error handling does not free memory for object X,
thus creating a memory leak.
If a function in a C++ MEX file throws an explicit exception which is not caught
inside the MEX file with a catch statement, then the exception
causes MATLAB to terminate instead of propagating the error to the MATLAB command line.
#include "mex.h"
class error {}; // Throw an exception of this class
class MyClass
{
public:
MyClass(){
mexPrintf("Constructor called.");
}
~MyClass(){
mexPrintf("Destructor called.");
}
};
void doErrorChecking(const MyClass& obj)
{
// Do error checking
throw error();
}
void createMyClass()
{
MyClass myobj;
doErrorChecking(myobj);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
createMyClass();
}The MEX function calls createMyClass, which creates an object
of class MyClass and calls function
doErrorChecking. Function doErrorChecking
throws an exception of type error. This exception, however, is
not caught inside the MEX file and causes MATLAB to crash.
This behavior also occurs for classes inheriting from the class
std::exception.
Catch the exception in the MEX function:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
try{
createMyClass();
}
catch(error e){
// Error handling
}
}