Matlab MinGW / Intel compiler setup

Matlab requires C / C++ / Fortran compilers for numerous operations such as mex, loadlibrary and more. For Windows, besides Visual Studio, Matlab supported compilers include MinGW GNU compilers for Windows for C and C++, or Intel compilers for C / C++ / Fortran.

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

Error in loadlibrary>getLoadLibraryCompilerConfiguration

or

Error using mex No supported compiler was found.

Tell Matlab compiler location

Matlab uses distinct environment variables to communicate the location of specific compiler versions. Here are a couple examples. By default on Windows, Matlab will use Visual Studio unless overridden with environment variables as below.

MinGW compiler

Tell Matlab the MinGW compiler path via Windows environment variable MW_MINGW_LOC.

  1. Find the MinGW compiler location from Terminal using where gcc (Windows) or which gcc.
  2. in Matlab, assuming MinGW is under C:\mingw64\bin (don’t include “bin” below)

    setenv('MW_MINGW64_LOC', 'c:\mingw64')
  3. Setup MEX C / C++ permanently

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

Intel compiler

Intel Fortran compiler must be used on Windows and Mac with Matlab.

  1. find the Intel compiler location using where ifort (Windows) or which ifort.
  2. assuming compiler location is at C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2019\windows\bin\intel64\ifort.exe

    set('ICPP_COMPILER19', 'C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2019\windows')
    set('IFORT_COMPILER19', 'C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2019\windows')
  3. setup MEX Fortran permanently:

    mex -setup Fortran -v
    mex -setup -v

Verify

Matlab MEX parameters are displayed in Matlab by:

mex.getCompilerConfigurations('C')

mex.getCompilerConfigurations('C++')

mex.getCompilerConfigurations('FORTRAN')