如何直接在 Visual Studio 中构建 mex 文件? [英] How to build mex file directly in Visual Studio?
问题描述
我有一个包含函数库的 Visual Studio 2010 解决方案,我希望能够使用 MATLAB 作为该库的几个可能的前端之一.因此,我希望 Visual Studio 在我构建解决方案时自动生成一个 mex 文件,而不必将我的所有构建选项和路径导出到 mexopts.bat 并打开 MATLAB 以从那里构建文件.我看到了一些实现类似目标的建议,例如在这些帖子中:
Matlab 7.1+ 和 Visual Studio 2005
如何使用 CMake 和 Visual Studio 2010(64 位)构建 MATLAB R2011a(64 位)mex 文件?
然而,它们要么看起来有点过时(引用不再被找到的文件)要么使用外部工具(例如 CMake).有谁知道如何在 Visual Studio(2010 及更高版本)中设置一个新项目(在现有解决方案中),该项目将为当代 MATLAB 版本构建一个 mex 文件?
根据 this page 在问题中提到,似乎从一个空的 C++ 项目开始,项目的属性页中的以下设置对于从 Visual Studio 2010 构建有效的 .mexw64 是必要的和足够的:
<前>配置属性 -> 常规:将目标扩展名设置为 .mexw64将配置类型设置为动态库 (.dll)配置 poperties -> VC++ 目录:添加 $(MATLAB_ROOT)externinclude;包含目录配置属性 -> 链接器 -> 常规:添加 $(MATLAB_ROOT)externlibwin64microsoft;到其他图书馆目录配置属性 -> 链接器 -> 输入:添加 libmx.lib;libmex.lib;libmat.lib;附加依赖项配置属性 -> 链接器 -> 命令行:将/export:mexFunction 添加到其他选项$(MATLAB_ROOT)
是 Matlab 的根文件夹的路径,例如.C:Program FilesMATLABR2013a.
到目前为止,这只是从从头创建并为 Matlab 2013a 64 位构建的解决方案中尝试过的.我假设为 32 位构建只需要将出现的 64 次更改为 32.当我确认这适用于现有解决方案时,我将更新帖子.
正如预期的那样,这适用于添加到现有解决方案的项目.记得设置新项目依赖于创建库的项目.
编辑 2: 遵循 这个问题 我可以确认上述步骤也适用于 Visual Studio 2012、2013 和 2017.
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?
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.
这篇关于如何直接在 Visual Studio 中构建 mex 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!