How to build mex file directly in Visual Studio?

After some experimenting with guidance from this page mentioned in the question, it seems like starting with an empty C++ project the following settings in the project’s Property Pages are necessary and sufficient to build a working .mexw64 from Visual Studio 2010:

Configuration properties -> General:
    Set Target Extension to .mexw64
    Set Configuration Type to Dynamic Library (.dll)

Configureation poperties -> VC++ Directories:
    Add $(MATLAB_ROOT)\extern\include; to Include Directories

Configuration properties -> Linker -> General:
    Add $(MATLAB_ROOT)\extern\lib\win64\microsoft; to Additional Library Directories

Configuration properties -> Linker -> Input:
    Add libmx.lib;libmex.lib;libmat.lib; to Additional Dependencies

Configuration properties -> Linker -> Command Line:
    Add /export:mexFunction to Additional Options

$(MATLAB_ROOT) is the path to Matlab’s root folder, eg. C:\Program Files\MATLAB\R2013a.

So far this has only been tried from a solution created from scratch and built for Matlab 2013a 64-bit. I assume that to build for 32-bit one only needs to change both occurrences of 64 to 32. I will update the post when I have confirmed that this works for an existing solution.

EDIT: As expected this works for projects added to existing solutions. Remember to set the new project to be dependent on the project that creates the library.

Edit 2: Following this question I can confirm that the above steps work in Visual Studio 2012, 2013, and 2017 too.

Leave a Comment