Compiling a MEX file with Visual Studio




1) If we are working on matrix multiplication, then after compiling the .br file we generate a .cpp like below. Modify the .cpp file directly to use the MEX API. The sections in bold are the minimum code changes necessary.


#include "common.h"

#include "Timer.h"

#include "mex.h"


static int retval = 0;


//int main(int argc, char** argv)

void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])





        float A<Height, Width>;

        float B<Width, Height>;

        float C<Height, Height>;


        streamRead(A, inputA);

        streamRead(B, inputB);


        // Run the brook program here

        for (i = 0; i < cmd.Iterations; ++i)


            simple_matmult((float)Width, A, B, C);



        // Write data back from stream here

        streamWrite(C, output);





       mexPrintf("Time to execute matrix multiplication – fast \n");




2) Follow the direction below to build MEX-files with the Microsoft Visual C++ integrated development environment. These are based on:

Compiling MEX-Files with the Microsoft® Visual C++® IDE

1.      Create a project (matrix_multiply) and insert your MEX source files. (the .cpp file above)

2.      Add mexversion.rc from the MATLAB include directory, matlab\extern\include, to the project.

3.      Create a .def file to export the MEX entry point. On the Project menu, click Add New Item and select Module-Definition File (.def). For example:

4.     LIBRARY “matrix_multiply.mexw32”
5.     EXPORTS mexFunction          <-- for a C MEX-file
6.        or
EXPORTS _MEXFUNCTION@16       <-- for a Fortran MEX-file

7.      On the Project menu, click Properties for the project to open the property pages.

8.      Under C/C++ General properties, add the MATLAB include directory, matlab\extern\include, as an additional include directory.

9.      Under C/C++ Preprocessor properties, add MATLAB_MEX_FILE as a preprocessor definition.

10.  Under Linker General properties, change the output file extension to .mexw32 if you are building for a 32–bit platform or .mexw64 if you are building for a 64–bit platform.

11.  Locate the .lib files for the compiler you are using under matlabroot\extern\lib\win32\microsoft or matlabroot\extern\lib\win64\microsoft. Under Linker Input properties, add libmx.lib, libmex.lib, and libmat.lib as additional dependencies.

12.  Also under Linker General, add the path to those libraries under “Additional Library Directories”. My path was: "C:\Program Files\MATLAB\R2007b\extern\lib\win32\microsoft")

13.  Under Linker Input properties, add the module definition (.def) file you created if it is not already there).

14.  Under Linker Command Line, add the following as Additional options:

/export:mexFunction /dll

15.  Under Linker Debugging properties, if you intend to debug the MEX-file using the IDE, specify that the build should generate debugging information. For more information about debugging, see Debugging on the Microsoft® Windows® Platforms.


This should do it. Now you should have a matrix_multiply.mexw32 file created. You can execute that file in MATLAB by typing: matrix_multiply           in the folder with the file.