MATLAB Answers


I already have MinGW on my computer. How do I configure it to work with MATLAB?

I already have MinGW on my computer. How do I configure it to work with MATLAB?




7 Answers

Answer by MathWorks Support Team on 18 May 2018
Edited by MathWorks Support Team on 18 May 2018
 Accepted Answer

MATLAB detects the MinGW compiler by reading the environment variable MW_MINGW64_LOC. When you install MinGW from the MATLAB Add-Ons menu, MATLAB sets this variable. The following instructions can be used to set this environment variable manually.

For MATLAB R2017b and later:

Verify you have installed MinGW-w64 version 5.3 before following these steps.

1) Download the attached MATLAB function 'configuremingw'.

2) Identify the full path to the MinGW installation directory containing the MinGW compiler binaries. This is identified as MINGWROOT.

3) In the MATLAB Command Window run:

>> configuremingw(MINGWROOT)

Alternatively, run:

>> configuremingw

Then navigate to MINGWROOT.

For MATLAB R2015b through R2017a:

Verify you have installed MinGW-w64 version 4.9.2 before following these steps.

The MW_MINGW64_LOC environment variable can be (A) set for the entire system level or (B) set temporarily every time you open MATLAB. Administrative privileges are required to set MW_MINGW64_LOC as a system environment variable. No special privileges are required to set the environment variable temporarily.

(A) To set MW_MINGW64 as a system environment variable on Windows 7/8/10:

  1. Make sure you have administrative privileges.
  2. Select Computer from the Start menu.
  3. Choose System properties from the context menu.
  4. Click Advanced system settings > Advanced tab.
  5. Click Environment Variables.
  6. Under System variables, select New.
  7. In the New System Variable dialog box, type MW_MINGW64_LOC in the Variable name field.
  8. In the Variable value field, type the location of the MinGW-w64 compiler installation, for example, 'C:\TDM-GCC-64'.
  9. Click "OK" to close the dialog boxes, then close the Control Panel dialog box.

(B) To set MW_MINGW64 as a temporary environment variable using MATLAB:

Run the following in the MATLAB Command Window:

>> setenv('MW_MINGW64_LOC',folder)

where 'folder' is the installation directory of MinGW. For example, 'C:\TDM-GCC-64'. This command will need to be run every time you start MATLAB for MATLAB to correctly use MinGW. You can consider adding the above command to your 'startup.m' file. This will execute the command every time MATLAB starts up.

To Verify:

After performing the above steps, you can verify if the compiler was recognized by MATLAB by running the following command:

>> mex -setup


Also running R2017b.

I ran MATLAB R2017b with admin rights, ran configuremingw (navigating to the installation folder D:\Programs\mingw64 and choosing [Select folder]). I also got the error message

Undefined variable "mex_spkg" or class "mex_spkg.getEnvVarName".
Error in configuremingw

Tried again, with configuremingw('D:\Programs\mingw64'), and got the same error.

Running MATLAB R2017b without admin rights, mex -setup reports:

MEX configured to use 'MinGW64 Compiler (C)' for C language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
	 variables with more than 2^32-1 elements. You will be required
	 to update your code to utilize the new API.
	 You can find more information about this at:
To choose a different language, select one from the following:
 mex -setup C++ 
 mex -setup FORTRAN

The MEX compiler also reports error: mex yprime.c gives:

Building with 'MinGW64 Compiler (C)'.
Warning: MinGW compiler is not configured correctly. Please visit Manually Configure MinGW
for MATLAB to configure the MinGW compiler correctly and recompile your source code. 
MEX completed successfully.

Any idea how to fix this, or can we have an un-obfuscated configuremingw.m function, to try and identify the problem, please?

I have an idea regarding this Warning message during mex compile, even if it did work successfully:

Please check, if a file mex_C_win64.xml is located in your Matlab userprofile folder %APPDATA%\MathWorks\MATLAB\R2017b.

Matlab R2017b seems to generate this file creating the folder for the first time, but for an older MinGW Compiler 4.x. mex -setup does not correct or overwrite this file. So delete or rename the file mex_C_win64.xml and execute mex -setup again in Matlab. This generated a new, correct xml file in my case for MinGW 5.3, and the warning did not appear again.

I haven't figured out how to prevent Matlab R2017b from generating a incorrect mex_C_win64.xml file or force mex -setup to overwrite an already existing file... Any hints for this are welcome!

Many thanks, Tobias: that fixed the problem.

Running mex -setup has created a new mex_C_win64.xml file with four differences from the old one:

  <Version="5.3.0"             <--(was "4.x")
    LDTYPE="-shared -static"   <--(was "-shared")
      LDTYPE="=-static"        <--(was "")
    <mbuild                    <--(a whole new section)

Also, mbuild -setup now reports:

"MBUILD configured to use 'MinGW64 Compiler (C)' for C language compilation."

as expected, and it created the configuration file:


(which is identical to the new mex_C_win64.xml).

Both files are copies of D:\Program Files\MATLAB\R2017b\bin\win64\mexopts\mingw64.xml.

Sign in to comment.

Answer by S Crombie on 22 Nov 2017

I am using MATLAB 2017b so followed the first set of instructions. After setting MINGWROOT = 'C:\mingw64' and running configuremingw(MINGWROOT) I get the following error: Undefined variable "mex_spkg" or class "mex_spkg.getEnvVarName".

Error in configuremingw

Where are these variables supposed to be? Have I not configured the path correctly? Grateful for any advice.


Despite these errors when I closed MATLAB and reopened it the MINGW compiler had been recognised and could be used.

I have same issue did you find how can we resolve it?

Sign in to comment.

Answer by emrah sever on 4 Dec 2017

Thank you. The explanation solved my problem


Sign in to comment.

Answer by Wesley P
on 31 Jan 2018

Hi Crombie, can you share the link for the installer to install manually. Thank you


Hi Crombie Thanks for sharing the link , I tried but I land on the message when I click Next - "Cannot download repository.txt" and exits, can you help me on this issue. I have Windows 10 OS.

Sign in to comment.

Answer by racharla
on 28 Feb 2018

I did as per above said..I use matlab2017a I have configured environmental variables and I am having MinGw in some location in my system and I have given location in environment variables but even then I am getting same error like "Error using mex No supported compiler or SDK was found. You can install the freely available MinGW-w64 C/C++ compiler; see Install MinGW-w64 Compiler. For more options, visit" Can anyone please me


hello , you can find instruction available MinGw-w64 C/C++ compiler, Thing what i understand when do you finish the installation shut down the Matlab and then restart again. Note that you already set the environment variable.

I had the same probem but then I realized that I had set the wrong path to the environment variable. I now have it as "C:\MinGW\mingw64" and it works. Make sure the path does NOT have any spaces, like "C:\Program Files\MinGW\mingw64" to avoid future erros, and that the bit version corresponds to your machine's!

Sign in to comment.

Answer by Fabrice
on 25 Jun 2018

And what for Matlab 2014a ?


Sign in to comment.

Answer by Fabrice
on 25 Jun 2018
Edited by Fabrice
on 25 Jun 2018

I have VisualStudio 2017 too, but mex -setup cannot find/use the included compilers ? What are the solutions to have Matlab mex files working in this configuration ?


  • Matlab 2014a
  • Windows 10 x64
  • Various compilers installed (VS2017, MinGW...) ; could install another one if needed


  • Intel Core i7

  1 Comment

See for the list of compilers you can use. Note that where it talks about Professional editions, it really does mean Professional editions (or Ultimate editions), not Community or Express editions.

Sign in to comment.