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

1,756 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 1 Sep 2022
Edited: MathWorks Support Team on 7 Sep 2022
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.
Be aware that in order for any changes to be permanent, the "MATLAB Support for MinGW-w64 C/C++ Compiler" support package must be uninstalled if it was previously installed.
For MATLAB R2017b and later:
Verify you have installed MinGW-w64 version 5.3 (or MinGW-w64 version 6.3 for R2018b and later) 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 and seeing what the MW_MINGW64_LOC environment variable detected by MATLAB is set to:
>> mex -setup -v
Note: Be aware that due to caching limitations, the option to select the "MinGW64 Compiler" after running this command may show the incorrect command to be run. In reality, clicking on this option will use the version of MinGW pointed to by the MW_MINGW64_LOC environment variable.
Paul Wintz
Paul Wintz on 16 Aug 2021
The instruction the description of MINGWROOT as the "MinGW installation directory containing the MinGW compiler binaries" is confusing. I thought, initally, that the path would include the bin folder (after all, that is where the binaries are located). To the contrary, however, MINGWROOT should point to the parent directory of bin.
So I needed to set MINGWROOTC to \mingw\mingw64 not C:\mingw\mingw64\bin.

Sign in to comment.

More Answers (14)

flat cloud
flat cloud on 15 Jun 2021
After install the MinGW64,you should add the MinGW64 to system path。
Then run the mex -setup C++, it works

Marcel Brito
Marcel Brito on 8 Oct 2021
Edited: Marcel Brito on 8 Oct 2021
What I've done to get this to work was uninstalling the old minGW version 8.1.0 and installing the new version 9.
But instead of the previous path (Program Files) I just placed the minGW folder to my C hard drive so that there is no spaces in the path!
After that I started matlab as admin and run the >> configuremingw command and selected the minGW folder (not the bin folder!).
Now everything is working with the newest compiler version of minGW!
@MathWorks Support Team this should be added to the documentation section of how to add the minGW compiler manually to be able to use the newest version of the minGW compiler. Or at least update the minGW compiler from the Add-Ons section!

S Crombie
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.
Walter Roberson
Walter Roberson on 5 Oct 2018
setenv('MW_MINGW64_LOC', 'c:\program files(x86)\mingw-w64')
If it works there will be no output from MATLAB, but you could check afterwards with

Sign in to comment.

racharla on 28 Feb 2018
Edited: MathWorks Support Team on 23 Aug 2021
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: https://www.mathworks.com/support/requirements/supported-compilers.html
M.Sy on 22 Sep 2020
But the default instatllation path is under matlab's \Program Files\***, so that even the configuration is ok, but I cannot compile .cpp files with mex command

Sign in to comment.

Fabrice on 25 Jun 2018
Edited: 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
Walter Roberson
Walter Roberson on 25 Jun 2018
See https://www.mathworks.com/content/dam/mathworks/mathworks-dot-com/support/sysreq/files/SystemRequirements-Release2014a_SupportedCompilers.pdf 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.

Jawhar Chebbi
Jawhar Chebbi on 5 Sep 2019
Edited: Jawhar Chebbi on 5 Sep 2019
Hello, this is not working for me. I have Matlab 2017b, I run it as administrator and when I execute the function configuremingw with the correct path, I get the following error
Error using configuremingw
Unable to setup the MinGW compiler due to administrator privileges not granted.

emrah sever
emrah sever on 4 Dec 2017
Thank you. The explanation solved my problem

Wesley P
Wesley P on 31 Jan 2018
Hi Crombie, can you share the link for the installer to install manually. Thank you
Wesley P
Wesley P on 20 Feb 2018
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.

Fabrice on 25 Jun 2018
And what for Matlab 2014a ?

Afaf Saad
Afaf Saad on 23 Sep 2018
Edited: Afaf Saad on 23 Sep 2018
i downloaded version 8 and installed but i can not connect it with matlab, any help?
am getting this error: ">> configuremingw Error using configuremingw Folder 'C:\Program Files (x86)\mingw-w64' does not contain MinGW binaries."
Ed Mendes
Ed Mendes on 10 Mar 2019
I have followed the steps shown above and got mex -setup to see the mingw binaries. However when I try mex yprime.c, an error comes up.
Building with 'MinGW64 Compiler (C)'.
C:\mingw-w64\i686-6.3.0-posix-dwarf-rt_v5-rev2\mingw32\bin\gcc -c -DUSE_MEX_CMD -m64 -DMATLAB_MEX_FILE -I"C:\Program Files\MATLAB\R2017b/extern/include" -I"C:\Program Files\MATLAB\R2017b/simulink/include" -I"C:\Program Files\MATLAB\R2017b/extern\lib\win64\mingw64" -fexceptions -fno-omit-frame-pointer -O2 -fwrapv -DNDEBUG "C:\Users\virtual_000\Documents\MATLAB\yprime.c" -o C:\Users\virtual_~1\AppData\Local\Temp\mex_1847762154678_3576\yprime.obj
Error using mex
C:\Users\virtual_000\Documents\MATLAB\yprime.c:1:0: sorry, unimplemented: 64-bit mode not
compiled in
Mingw-64 came as result of using mingw-w64-install from where I choose the version to be installed (\i686-6.3.0-posix-dwarf-rt_v5-rev2).
What could be wrong?
Many thanks

Sign in to comment.

zeng xianren
zeng xianren on 30 May 2019
I have matlabR2008a. To run 'mbuild -setup', the matlab can't connect the gcc, can you help me ?

Sign in to comment.

asaf benjamin
asaf benjamin on 23 Dec 2021
I've update my Matlab version to 2021b on a Windows 10 machine and installed MinGW based on the instrction found above, but when I try to run a Matlab script that calls 'mexcuda' (and used to run with no problem using Matlab 2019b) I get the warning and error:
Warning: The selected C++ compiler is not supported for CUDA compilation. Searching for a supported compiler.
> In mexcuda (line 96)
In mexGPUall (line 7)
Error using mex
Supported compiler not detected. For options, visit https://www.mathworks.com/support/compilers.
Error in mexcuda (line 168)
[varargout{1:nargout}] = mex(mexArguments{:});
Error in mexGPUall (line 7)
mexcuda -largeArrayDims mexThSpkPC.cu
getenv MW_MINGW64_LOC
and running:
mex -setup
MEX configured to use 'MinGW64 Compiler (C)' for C language compilation.
To choose a different C compiler, select one from the following:
MinGW64 Compiler (C) mex -setup:C:\Users\asafbe\AppData\Roaming\MathWorks\MATLAB\R2021b\mex_C_win64.xml C
Microsoft Visual C++ 2015 (C) mex -setup:'C:\Program Files\MATLAB\R2021b\bin\win64\mexopts\msvc2015.xml' C
To choose a different language, select one from the following:
mex -setup C++
mex -setup FORTRAN
but I've tried noth VS 2015 and MinGW64 and none of my choices fix the error above. Any help would be highly appreciated.
  1 Comment
Walter Roberson
Walter Roberson on 23 Dec 2021
And look at the GPU Coder column. For R2021b, the supported compilers for Windows for that task are Microsoft Visual C++ 2019 product family and Microsoft Visual C++ 2017 product family

Sign in to comment.

Kangyi Sun
Kangyi Sun on 17 Mar 2022
When I type "mex -setup" it seems that the configuration is right. But then I type "mex yprime.c" it shows "gcc: error: CreateProcess: No such file or directory"
What does that means? Can you help me?

亚雄 on 3 Aug 2022
- -




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!