MinGW-w64 is a compiler suite for Windows based on the GNU tool chain. It includes a GCC compiler and related tools for compiling C and C++ applications for Windows. C and C++ applications compiled with MinGW-w64 GCC can be called from MATLAB using MEX. This GCC compiler can also be helpful for other MathWorks products that require a C or C++ compiler.
This Add-On installs one of the following versions of MinGW GCC based on your version of MATLAB:
MATLAB R2015b, R2016a, R2016b, R2017a: MinGW GCC 4.9.2 from TDM
MATLAB R2017b and beyond: MinGW GCC 6.3 from mingw-w64.org
Installing The Compiler
You can use the MinGW-w64 compiler to build MEX files and standalone MATLAB® engine and MAT-file applications. To install the compiler, use the Add-Ons menu described in Get Add-Ons. Search for MinGW or select from Features. For more information, see the File Exchange article “MATLAB Support for MinGW-w64 C/C++ Compiler”.
You can test the MinGW® compiler by building the
yprime.c example. Copy the source file to a writable folder.
If you only have the MinGW compiler installed on your system, the
mex command automatically chooses MinGW. Go to the next step. However, if you have multiple C or C++ compilers, use
mex -setup to choose MinGW.
Build the MEX file.
MATLAB displays a “Building with” message showing what compiler was used to build the MEX file.
Run the function.
For more information, see Troubleshooting and Limitations Compiling C/C++ MEX Files with MinGW-w64.
MinGW Installation Folder Cannot Contain Space
Do not install MinGW in a location with spaces in the path name. For example, do not use:
Updating MEX Files to Use MinGW Compiler
If you have MEX source files built with a different MATLAB-supported compiler, you might need to modify the code to build with the MinGW compiler. For example:
- Library (
.lib) files generated by Microsoft® Visual Studio® are not compatible with MinGW.
- Object cleanup is not possible when an exception is thrown using the
mexErrMsgIdAndTxtfunction 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.