Main Content

Troubleshooting and Limitations Compiling C/C++ MEX Files with MinGW-w64

Do Not Link to Library Files Compiled with Non-MinGW Compilers

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.

MinGW Installation Folder Cannot Contain Space

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

MEX Command Does not Choose MinGW

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.

Manually Configure MinGW for MATLAB

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".

MinGW Behaves Similarly to gcc/g++ on Linux

When modifying compiler flags using the mex command, use the Linux® compiler flags CFLAGS or CXXFLAGS instead of the Windows flag COMPFLAGS.

Potential Memory Leak Inside C++ MEX Files on Using MEX Exceptions

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 Write C++ Functions Callable from MATLAB (MEX Files).

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.

Unhandled Explicit Exceptions in C++ MEX Files Unexpectedly Terminate MATLAB

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.

Workaround

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
    }
}

See Also

Related Topics