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 oneAPI 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. On Windows, Matlab will use Visual Studio and Intel oneAPI compilers.

MinGW compiler

Tell Matlab the MinGW compiler path via Windows environment variable MW_MINGW_LOC. On Windows, Fortran needs Intel oneAPI compiler instead.

  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 oneAPI Fortran compiler with Matlab is setup by:

mex -setup Fortran -v
mex -setup -v

If the Intel oneAPI compilers are not found:

  1. find the Intel compiler location using where ifort (Windows) or which ifort.

  2. assuming compiler location is at “C:\Program Files (x86)\inteloneapi\compiler\latest\windows\bin\intel64\ifort.exe”

    set('ICPP_COMPILER19', 'C:\Program Files (x86)\inteloneapi\compiler\latest\windows')
    set('IFORT_COMPILER19', 'C:\Program Files (x86)\inteloneapi\compiler\latest\windows')
    

Verify

Matlab MEX parameters are displayed in Matlab by:

mex.getCompilerConfigurations('C')

mex.getCompilerConfigurations('C++')

mex.getCompilerConfigurations('FORTRAN')