Manual setup of MinGW compilers for building Matlab MEX files in Windows

Setting up a Matlab compatible MinGW compiler in Windows

◼ For Matlab R2015b up to R2017a, you can use MinGW 4.9.2 TDM-GCC.
◼ For Matlab R2017b, MinGW 5.3 is required.
Instructions are given below for both categories.

Matlab R2015b up to R2017a

► Download the compiler binary file -> link: tdm64-gcc-4.9.2-3.exe
► Uncheck box "Check for updated files on the TDM-GCC server".
► Create a new TDM-GCC installation.
► Select "MinGW-w64/TDM64".
► Choose an installation directory. Default = C:\TDM-GCC-64
► If you are going to write programs with parallel computations, make sure to check OpenMP in optional components - gcc.
► In front of gcc section, make sure that the correct version 4.9.2 is given; if not, you probably forgot to uncheck updates in the beginning.

TDM-gcc-installation

Now, to inform the path of the compiler to Matlab, you can write in Matlab command-window, or directly in your m-file the following command.
Replace the given default installation path of the compiler, if you changed it.

setenv('MW_MINGW64_LOC','C:\TDM-GCC-64');

To test whether the compiler is set correctly, type the following command in the Matlab Command Window

mex -setup C++

In Matlab R2017a, it gives you the following output:

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

Matlab R2017b

► Download and run mingw-w64 installer file -> link: mingw-w64-install.exe
► Select the proper version (5.3.0) and architecture (in my case "x86_64").

TDM-gcc-installation

► Choose the destination folder for installation (in my case "C:\mingw-w64\x86_64-5.3.0-posix-seh-rt_v4-rev0") and go to next steps.

After installation, to inform the path of the compiler to Matlab, you can write in Matlab command-window, or directly in your m-file the following command.

setenv('MW_MINGW64_LOC','C:\mingw-w64\x86_64-5.3.0-posix-seh-rt_v4-rev0\mingw64');

To test whether the compiler is set correctly, type the following command in the Matlab Command Window

mex -setup C++

This should give you the following output:

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

Example for building mex function

In the following files, you can try to build a test mex function. Both files should be in an identical folder.

mex_function_test.cpp
                
                
                compile.m 
                

                
                

After running "compile.m", you should be able to see the output message "From C++: Hello world!" in the Matlab command window.