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

3,343 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 Sep 2024
Edited: MathWorks Support Team on 7 Aug 2024
MATLAB detects the MinGW compiler by reading the environment variable "MW_MINGW64_LOC". Use the following instructions to set this environment variable manually.
Note: Be aware that in order for the below instructions to work, the "MATLAB Support for MinGW-w64 C/C++/Fortran Compiler" support package for your current MATLAB version must be uninstalled if it was previously installed. See: Uninstall Support Packages 
 

Step 1: Verify the installed MinGW version

Before following the steps below, verify that you have installed the appropriate version of MinGW-w64 according to your version of MATLAB before following the steps below.
  • MinGW-w64 version 8.1 for R2023a and later
  • MinGW-w64 version 6.3 for R2018b and later
  • MinGW-w64 version 5.3 for R2017b through R2018a
  • MinGW-w64 version 4.9.2 for R2015b through R2017a
To check the MinGW version, open the Windows Command Prompt, navigate to the MinGW bin directory, and execute "gcc --version":
> cd C:\mingw64\bin > gcc --version gcc (x86_64-posix-seh-rev2, Built by MinGW-W64 project) 6.3.0
In this example, the MinGW-w64 version is 6.3.0.
 

Step 2: Set the MW_MINGW64_LOC environment variable

There are three options for setting the MW_MINGW64_LOC environment variable. Option A and B require administrative privileges, whereas Option C is suited for users without privileges.
Option A: Use the 'configuremingw' function (MATLAB R2017b and later only)
  1. Start MATLAB with administrative privileges.
  2. Download the attached MATLAB function 'configuremingw' (at the top of the answer).
  3. In the MATLAB Command Window run:
>> configuremingw
        Then navigate to the installation directory of MinGW.
Option B: Set "MW_MINGW64_LOC" as a system environment variable on Windows 10/11:
  1. Make sure you have administrative privileges.
  2. Enter "Edit the system environment variables" in the Windows search.
  3. Click "Environment Variables...".
  4. Under System variables, select "New...".
  5. In the New System Variable dialog box, type MW_MINGW64_LOC in the Variable name field.
  6. In the Variable value field, type the location of the MinGW-w64 compiler installation, for example, C:\mingw64 or C:\TDM-GCC-64.
  7. Click "OK" to close the dialog boxes, then close the Control Panel dialog box.
Option C: Set "MW_MINGW64_LOC" as a temporary environment variable using MATLAB:
If you have no administrative priviledges, run the following in the MATLAB Command Window:
>> setenv('MW_MINGW64_LOC',folder)
where 'folder' is the installation directory of MinGW. For example, 'C:\mingw64' (note that this path should not contain any spaces). This command will need to be run every time you start MATLAB. To automate this, add the above command to your 'startup.m' file. This will execute the command every time MATLAB starts up. See: https://www.mathworks.com/help/matlab/ref/startup.html
 

Step 3: Verify that the existing MinGW installation is recognized by MATLAB

After performing the above steps, verify that the compiler was recognized by MATLAB by running the following command:
>> mex -setup -v
You will be able to see what the "MW_MINGW64_LOC" environment variable detected by MATLAB is set to.
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.
  6 Comments
Walter Roberson
Walter Roberson on 10 May 2023
Edited: Walter Roberson on 10 May 2023
https://azrael.digipen.edu/~mmead/www/public/mingw/ has instructions for installing the 32 bit version.
CANoe_MatlabIntegrationPackage_MinGW_32_bin appears to be part of CANoe configuration, not MATLAB, so you might need to redo some CANoe installation steps.
Jerry George
Jerry George on 11 Jun 2024
Edited: Jerry George on 11 Jun 2024
Just to clarify, MW_MINGW64_LOC should be set to the folder that contains the 'bin' folder. That is mex -setup is going to look for a file %MW_MINGW64_LOC%\bin\gcc.exe

Sign in to comment.

More Answers (3)

Paul Wintz
Paul Wintz on 16 Aug 2021
Moved: Stefanie Schwarz on 10 Aug 2023
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.

Zhibin Deng
Zhibin Deng on 15 May 2023
Moved: Stefanie Schwarz on 10 Aug 2023
I got an error
"Error using configuremingw
Unable to setup the MinGW compiler due to administrator privileges not granted."
My matlab version is 2023a under Windows 11. I have run matlab using in the administrative mode.
How to solve this issue?

Richard Crozier
Richard Crozier on 20 Jan 2020
Moved: Stefanie Schwarz on 10 Aug 2023
I am trying the described solution in Matlab R2018a. If I run without administrator priviliges I get the following when trying to run configuremingw
>> configuremingw
Error using configuremingw
Unable to setup the MinGW compiler due to administrator privileges not granted
I then tried manually changing MW_MINGW64_LOC but without success, see the below matlab session:
>> mex -setup C++
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:
https://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
>> getenv ('MW_MINGW64_LOC')
ans =
'C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\mingw_w64.instrset'
>> mingwdir = getenv ('MW_MINGW64_LOC')
mingwdir =
'C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\mingw_w64.instrset'
>> ls (mingwdir)
. build-info.txt lib opt
.. etc libexec share
bin include licenses x86_64-w64-mingw32
>> ls ('C:\Program Files\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev2\mingw64')
. build-info.txt lib opt
.. etc libexec share
bin include licenses x86_64-w64-mingw32
>> setenv ('MW_MINGW64_LOC', 'C:\Program Files\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev2\mingw64')
>> getenv ('MW_MINGW64_LOC')
ans =
'C:\Program Files\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev2\mingw64'
>> mex -setup
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:
https://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
To choose a different language, select one from the following:
mex -setup C++
mex -setup FORTRAN
>> mex -setup C++
Error using mex
No supported compiler 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/compilers.
>> mex -setup FORTRAN
Error using mex
No supported compiler was found. For options, visit https://www.mathworks.com/support/compilers.
>> CC = mex.getCompilerConfigurations
CC =
2×1 CompilerConfiguration array with properties:
Name
Manufacturer
Language
Version
Location
ShortName
Priority
Details
LinkerName
LinkerVersion
MexOpt
>> CC(1)
ans =
CompilerConfiguration with properties:
Name: 'MinGW64 Compiler (C++)'
Manufacturer: 'GNU'
Language: 'C++'
Version: '5.3.0'
Location: 'C:\Program Files\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev2\mingw64'
ShortName: 'mingw64-g++'
Priority: 'E'
Details: [1×1 mex.CompilerConfigurationDetails]
LinkerName: 'C:\Program Files\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev2\mingw64\bin\g++'
LinkerVersion: ''
MexOpt: 'C:\Users\User\AppData\Roaming\MathWorks\MATLAB\R2018a\mex_C++_win64.xml'
>> CC(2)
ans =
CompilerConfiguration with properties:
Name: 'MinGW64 Compiler (C)'
Manufacturer: 'GNU'
Language: 'C'
Version: '5.3.0'
Location: 'C:\Program Files\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev2\mingw64'
ShortName: 'mingw64'
Priority: 'E'
Details: [1×1 mex.CompilerConfigurationDetails]
LinkerName: 'C:\Program Files\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev2\mingw64\bin\gcc'
LinkerVersion: ''
MexOpt: 'C:\Users\User\AppData\Roaming\MathWorks\MATLAB\R2018a\mex_C_win64.xml'
>> rnfoundry_setup
Setting up mexslmeval.
Warning: mexslmeval compilation failed, you may be missing required libraries, gsl and gslcblas
> In mexslmeval_setup (line 63)
In rnfoundry_setup (line 319)
Finished building mexslmeval.
Setting up mexMBCNodal and mexMBCNodalSharedMem.
Warning: Unable to compile mex function mexMBCNodal. Error reported was:
No supported compiler 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/compilers.
As you can see, merely changing the MW_MINGW64_LOC does not corectly modify the compiler configurations, at least when matlab is run as non-root.
I tried again, after starting matlab.exe using 'Run as administrator' as below:
If I do this, I get the exact same issue when tryiong to run configuremingw
>> configuremingw ('C:\Program Files\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev2\mingw64')
Error using configuremingw
Unable to setup the MinGW compiler due to administrator privileges not granted.
setenv doesn't help either
>> setenv ('MW_MINGW64_LOC', 'C:\Program Files\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev2\mingw64')
>> mex -setup
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:
https://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
To choose a different language, select one from the following:
mex -setup C++
mex -setup FORTRAN
>> mex -setup C++
Error using mex
No supported compiler 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/compilers.
As others have noted, if configuremingw wasn't an obfuscated p file I could attempt to fix this. My suspicion is that configuremingw calls another system program or script which doesn't run with the same permissions.
Any suggestions for getting an alternative mingw to work on R2018a?
p.s. I also tried activating the administrator account, logging in as administrator and still get
>> configuremingw
Error using configuremingw
Unable to setup the MinGW compiler due to administrator privileges not granted.
  1 Comment
Stefanie Schwarz
Stefanie Schwarz on 10 Aug 2023
Since you appear to have an existing installation of the MinGW Support Package, it is much easier if you just edit the system environment variable MW_MINGW64_LOC in the Windows settings (with the help of an admin) to have this value:
C:\Program Files\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev2\mingw64
Instead of:
C:\ProgramData\MATLAB\SupportPackages\R2018a\3P.instrset\mingw_w64.instrset
(which is where the MinGW compiler shipped with the support package resides).
Then, restart MATLAB.

Sign in to comment.

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!