You can use the MinGW-w64 compiler to build MEX files, a MATLAB®interface to a C++ library, and standalone MATLAB engine and MAT-file applications. For more information, see"MATLAB Support for MinGW-w64 C/C++ Compiler".
To install the compiler, use the Add-Ons menu.
On the MATLABHometab, in theEnvironment部分中,点击Add-Ons > Get Add-Ons.
Search for MinGW or select fromFeatures.
yprime.c
ExampleYou can test the MinGW®compiler by building theyprime.c
example. Copy the source file to a writable folder.
copyfile(fullfile(matlabroot,'extern','examples','mex','yprime.c'),'.','f')
If you only have the MinGW compiler installed on your system, themex
command automatically chooses MinGW. Go to the next step. However, if you have multiple C or C++ compilers, usemex -setup
to choose MinGW.
mex-setup
Build the MEX file.
mexyprime.c
MATLAB displays a “Building with” message showing what compiler was used to build the MEX file.
Run the function.
yprime(1,1:4)
For more information, seeTroubleshooting and Limitations Compiling C/C++ MEX Files with MinGW-w64.
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 have MEX source files built with a different compiler that MATLAB supports, you might need to modify the code to build with the MinGW compiler. For example:
库(.lib
) files generated byMicrosoft®Visual Studio®are not compatible with MinGW.
Object cleanup is not possible when an exception is thrown using themexErrMsgIdAndTxt
function from C++ MEX files, resulting in memory leak.
An uncaught exception in C++ MEX files compiled with MinGW causes MATLAB to crash.
MEX files with variables containing large amounts of data cannot be compiled, as the compiler runs out of memory.