If you already have MinGW-w64 installed on your Windows computer (for instance, through Code::Blocks, CLion, MSYS2, or standalone installation), you do not need to download the full MATLAB Add-On package from scratch. You can configure MATLAB to detect and use your existing MinGW installation for compiling MEX files, C/C++ code, and Simulink S-functions.

Here is the complete step-by-step guide to configuring your existing MinGW compiler in MATLAB.


Step 1: Check Your MinGW Architecture & Installation Path

Before connecting MinGW to MATLAB, ensure your existing installation meets MATLAB's requirements:

  • 64-bit Architecture: Modern 64-bit MATLAB requires x86_64 MinGW-w64. A 32-bit (i686) compiler will cause architecture mismatch errors.
  • No Spaces in the Path: MinGW must be installed in a folder path with no spaces (e.g., C:\mingw64 or C:\msys64\mingw64). Avoid directories like C:\Program Files\MinGW.

Step 2: Set the MW_MINGW64_LOC Environment Variable

MATLAB locates third-party MinGW installations by checking a dedicated environment variable named MW_MINGW64_LOC.

Option A: Configure for the Current MATLAB Session

In the MATLAB Command Window, run the following command (replace C:\mingw64 with your actual MinGW root folder):

% Set the MinGW root folder (Do not include \bin)
setenv('MW_MINGW64_LOC', 'C:\mingw64');
Important: Set the path to the root folder of MinGW-w64 (the folder that contains bin, include, and lib), not the \bin folder itself.

To avoid setting the variable every time you restart MATLAB, add it permanently to Windows System Environment Variables:

  1. Press Win + R, type sysdm.cpl, and press Enter.
  2. Navigate to the Advanced tab and click Environment Variables.
  3. Under System Variables, click New...
  4. Enter the following details:
    • Variable name: MW_MINGW64_LOC
    • Variable value: C:\mingw64 (your MinGW root path)
  5. Click OK on all windows, then restart MATLAB.

Step 3: Run MEX Setup in MATLAB

Once the environment variable is configured, instruct MATLAB to register the C and C++ compilers:

% Setup C++ compiler
mex -setup C++

% Setup C compiler
mex -setup C

You should see a confirmation message similar to:

MEX configured to use 'MinGW64 Compiler (C++)' for C++ language compilation.

Step 4: Verify MEX Compilation with a Test File

To confirm that your compiler builds working binaries, compile and run MATLAB's built-in yprime.c test example:

% Navigate to the examples directory
cd(fullfile(matlabroot, 'extern', 'examples', 'mex'));

% Compile the C MEX source file
mex yprime.c

% Test the compiled MEX function
yprime(1, 1:4)

If the function outputs 2.0000 8.9685 4.0000 -1.0947, your MinGW compiler is fully operational!


Common Troubleshooting & Fixes

1. "No supported compiler was found"

  • Verify that the MW_MINGW64_LOC path contains bin\gcc.exe and bin\g++.exe.
  • Ensure there are no trailing backslashes in the variable path (use C:\mingw64, not C:\mingw64\).
  • Confirm your MinGW is 64-bit by running gcc -v in the Windows Command Prompt (look for Target: x86_64-w64-mingw32).

2. MSYS2 MinGW-w64 Users

If you installed MinGW through MSYS2, set the variable to the mingw64 directory inside MSYS2:

setenv('MW_MINGW64_LOC', 'C:\msys64\mingw64');
mex -setup C++

Frequently Asked Questions

Do I need to install Microsoft Visual Studio if I use MinGW?

No. MinGW-w64 is a lightweight, free, and fully functional GCC/G++ compiler for compiling standard MEX files and MATLAB Coder / Simulink models without requiring Microsoft Visual Studio.

Why shouldn't I include the "\bin" folder in MW_MINGW64_LOC?

MATLAB's internal MEX build scripts expect the root folder of the MinGW installation and automatically append \bin, \include, and \lib during compilation. Specifying C:\mingw64\bin will break the path resolver.