">

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

MATLABSolutions. Oct 11 2025 · 7 min read
How to Configure MinGW with MATLAB | Step-by-Step Guide

If you already have MinGW installed, configuring it to work with MATLAB for compiling MEX files, C/C++ code, or Simulink models is straightforward. Here’s a step-by-step guide:


1. Verify MATLAB Support for MinGW

mex -setup C++ 

This will prompt MATLAB to check for installed compilers.


2. Add MinGW to System PATH

MATLAB needs to know where your MinGW compiler is located.

  1. Find the MinGW bin directory (e.g., C:\\MinGW\\bin or C:\\Program Files\\mingw-w64\\...).

  2. Add it to the Windows PATH environment variable:

  1. Restart MATLAB to make sure it detects the updated PATH.


3. Configure MATLAB to Use MinGW

For MEX files (C/C++):

mex -setup mex -setup C++ 
Detected installed C++ compiler: \"MinGW64 Compiler (C++)\" Would you like to set it as default? 1: Yes 2: No 

For Simulink Code Generation (Optional)

If you want to generate C/C++ code from Simulink:

  1. Go to Simulation → Model Configuration Parameters → Code Generation → Toolchain.

  2. Select MinGW64 Compiler as your system target compiler.


4. Test the Configuration

Create a simple MEX file to test:

% Create test.c fileID = fopen(\\\'test.c\\\',\\\'w\\\'); fprintf(fileID,\\\'#include \"mex.h\" void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){mexPrintf(\"Hello from MinGW! \");} \\\'); fclose(fileID); % Compile in MATLAB mex test.c % Run test 

You should see:

Hello from MinGW! 

This confirms MATLAB is successfully using MinGW.


5. Troubleshooting Tips

mex -setup C++ \\\'C:\\Path\\to\\your\\mingw64\\bin\\g++.exe\\\'