[MATLAB] Compile C/C++ to MEX file with MinGW-w64

supakit kriangkhajorn
2 min readAug 18, 2018

1. Install MinGW-w64 from TDM-GCC MinGW Compiler.

1.1 Need to install TDM-GCC MinGW Compiler in a location is not spaces (not C:\TDM GCC 64) in the path.
1.2 Recommand install in C:\TDM-GCC-64.

download here: https://sourceforge.net/projects/tdm-gcc/files/TDM-GCC%20Installer/Previous/1.1309.0/

2. Open command windows in MATLAB for setup environment. Run command :

setenv(‘MW_MINGW64_LOC’, ‘path’)
e.g. setenv(‘MW_MINGW64_LOC’, ‘C:\TDM-GCC-64’)

And

mex -setup

2.2 Select language that you want to compile, click mex -setup C++.

MEX configured

3. Now, You can compile c language program, please check your file is in the current folder. Run command :

mex filename.c

If you want to test, you can use my file (read_c.c). In my project, I compile read_c.c to read_c.mexw64 https://github.com/toyotaAmasKU/vision/blob/master/read_c.c

Result of MEX

References
: https://www.mathworks.com/help/matlab/matlab_external/what-you-need-to-build-mex-files.html
: https://www.mathworks.com/help/matlab/matlab_external/install-mingw-support-package.html
: https://www.mathworks.com/help/matlab/matlab_external/compiling-c-mex-files-with-mingw.html

--

--