Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

matlab - How to build mex file directly in Visual Studio?

I have a Visual Studio 2010 solution that contains a library of functions, and I would like to be able to use MATLAB as one of several possible front-ends to this library. Therefore, I would like Visual Studio to automatically generate a mex file when I build the solution, without having to export all my build options and paths to mexopts.bat and open MATLAB to build the file from there. I have seen several suggestions to achieve something similar, for example in these posts:

Matlab 7.1+ and Visual Studio 2005

Compiling a MEX file with Visual Studio

How to use CMake and Visual Studio 2010 (64 bit) to build a MATLAB R2011a (64 bit) mex file?

However, they either seem a bit outdated (making references to files that are no longer to be found) or make use of external tools (eg. CMake). Does anyone know how to set up a new project (within the existing solution) in Visual Studio (2010 and newer) that will build a mex file for contemporary MATLAB releases?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)externinclude; to Include Directories

Configuration properties -> Linker -> General:
    Add $(MATLAB_ROOT)externlibwin64microsoft; 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 FilesMATLABR2013a.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...