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

QUICK ANSWER

Learn how to configure MinGW on your computer to work with MATLAB for compiling MEX files, C/C++ code, and Simulink models.

What you'll learn

  • 1. Verify MATLAB Support for MinGW
  • 2. Add MinGW to System PATH
  • 3. Configure MATLAB to Use MinGW
  • For MEX files (C/C++):
  • For Simulink Code Generation (Optional)

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

  • MATLAB officially supports MinGW-w64 (for 64-bit MATLAB versions).

  • To check if your version is supported, run in MATLAB:

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:

  • Windows 10/11:

    • Search for “Environment Variables” → Edit system environment variables → Environment Variables → System Variables → Path → Edit → Add your MinGW bin path.

  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++ 
  • MATLAB will search for installed compilers and detect MinGW.

  • You may see options like:

Detected installed C++ compiler: \"MinGW64 Compiler (C++)\" Would you like to set it as default? 1: Yes 2: No 
  • Choose Yes (1) to set MinGW as default.

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

  • Ensure you installed 64-bit MinGW for 64-bit MATLAB.

  • If MATLAB does not detect MinGW, check that the bin directory is in PATH and restart MATLAB.

  • Update MATLAB to the latest version to avoid compatibility issues.

  • If multiple MinGW versions are installed, explicitly specify the path when configuring MEX:

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

 

Need expert help with MATLAB?

MATLABSolutions provides assignment help, Simulink modeling, and project solutions — delivered by PhD engineers.

Get Expert Help Browse Projects