Tricks and Tips:
Compiling mex files with Visual Studio

If you want to compile a *.mex file inĀ  Visual Studio, do the following:
Configuration: Matlab 2010a, Visual Studio 2010, Intel 64bit machine.

1) File -> New Project;

2) Choose MFC DLL as well as the Name of the project and the Location; Choose OK;

3) Choose Next;

4) Choose Finish (do not change any selection);

5) Project -> Add New Item; Choose C++ File (.cpp); choose a Name for the file;

6) Add the line #include "stdafx.h" (of course, include "mex.h");

7) Write your code under the above include line; to make a fast test, you could just copy and paste a Matlab mex file example; you will find such examples at C:\Program Files\MATLAB\R2010a\extern\examples\mex;

8) Add the following lines to the .def file


Replace TestMex with the name of your .cpp file;

9) Project -> Properties -> Configuration Properties -> C/C++ -> General -> Additional Include Directories -> Add C:\Program Files\MATLAB\R2010a\extern\include;

10) Project -> Properties -> Configuration Properties -> Linker -> General -> OutPut File -> $(OutDir)$(ProjectName).mexw64;

11) Project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies -> C:\Program Files\MATLAB\R2010a\extern\lib\win64\microsoft\libmex.lib; C:\Program Files\MATLAB\R2010a\extern\lib\win64\microsoft\libmx.lib;

12) Project -> Properties -> Configuration Properties -> Configuration Manager -> Active Solution Platform -> New -> x64 -> Copy Settings From Win32

13) The directory containing the compiled mex library is ..\x64\Debug;

You can find a worked out example in the attached Visual Studio 2010 project.

Leave a Reply