如何直接在Visual Studio中构建mex文件? [英] How to build mex file directly in Visual Studio?
问题描述
我有一个Visual Studio 2010解决方案,其中包含一个函数库,并且我希望能够将MATLAB用作该库的几种可能的前端之一.因此,我希望Visual Studio在构建解决方案时自动生成一个mex文件,而不必将所有构建选项和路径导出到mexopts.bat并打开MATLAB来从那里构建文件.我在这些帖子中看到了一些实现类似目标的建议:
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+和Visual Studio 2005
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?
推荐答案
在问题中提到的此页面,似乎是从一个空的C ++项目开始,该项目的属性页"中的以下设置对于从Visual Studio 2010构建可运行的.mexw64是必要且足够的:
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)
是Matlab根文件夹的路径,例如. C:\ Program Files \ MATLAB \ R2013a.
$(MATLAB_ROOT)
is the path to Matlab's root folder, eg. C:\Program Files\MATLAB\R2013a.
到目前为止,这仅是从零开始创建并为64位Matlab 2013构建的解决方案中进行了尝试.我假设要为32位构建一个,只需要将两次出现的64更改为32.当我确认该方法适用于现有解决方案时,我将更新该帖子.
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.
编辑:如预期的那样,这适用于添加到现有解决方案中的项目.请记住,将新项目设置为依赖于创建库的项目.
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.
这篇关于如何直接在Visual Studio中构建mex文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!