Matlab MinGW compiler setup

Matlab requires C / C++ / Fortran compilers for numerous operations such as mex, loadlibrary and more. Matlab ≥ R2015b can use MinGW GNU compilers for Windows.

If Matlab doesn’t detect your MinGW compiler location, you may get an error like

Error in loadlibrary>getLoadLibraryCompilerConfiguration

or

Error using mex No supported compiler was found.

Fix

This fix is for Windows using MinGW compilers. Tell Matlab the MinGW compiler path via Windows environment variable MW_MINGW_LOC.

  1. Find the MinGW compiler location:

    where gcc
    1. in Matlab, assuming MinGW is under C:\mingw64\bin (don’t include “bin” below)
    setenv('MW_MINGW64_LOC', 'c:\mingw64')
  2. Setup MEX permanently

    mex -setup -v
        
    mex -setup -v C++

    Note: Sadly, even through Matlab R2019a, Matlab cannot using MinGW Gfortran. Intel Fortran must be used on Windows and Mac with Matlab. On Linux, Gfortran is fine with MEX Matlab via

    mex -setup -v Fortran

or consider using GNU Octave and Gfortran on Windows and Mac.

Verify

Matlab MEX parameters can be discovered from in Matlab by:

mex.getCompilerConfigurations('C')

mex.getCompilerConfigurations('C++')

mex.getCompilerConfigurations('FORTRAN')