如何使用 CMake 和 Visual Studio 2010(64 位)构建 MATLAB R2011a(64 位)mex 文件? [英] How to use CMake and Visual Studio 2010 (64 bit) to build a MATLAB R2011a (64 bit) mex file?
问题描述
我想编写一个 CMakeLists.txt
以便 CMake 编写一个 Visual Studio 2010(64 位)解决方案文件,以便从 C++ 代码中为 MATLAB R2011a(64 位)构建一个 mex 函数 example.cxx
.
I would like to write a CMakeLists.txt
such that CMake writes a Visual Studio 2010 (64 bit) solution file to build a mex function for MATLAB R2011a (64 bit) from C++ code example.cxx
.
- 我不想使用 MATLAB 的编译器包装器
mex
,而是设置 Visual Studio 解决方案文件,以便 Visual C++ 链接相关的 MATLAB 库. example.cxx
除了 mex 文件所需的 MATLAB 库外,没有任何依赖项.- CMake 2.8.7 已正确设置,因此它使用 Visual Studio 2010 的 64 位生成器.
- I do not want to use MATLAB's compiler wrapper
mex
but set up the Visual Studio solution file such that Visual C++ links the relevant MATLAB libraries. example.cxx
has no dependencies except for the MATLAB libraries that are necessary for mex files.- CMake 2.8.7 is set up correctly such that it uses the 64 bit generator for Visual Studio 2010.
我现在所做的事情的本质是
The essence of what I am doing right now is
find_package(Matlab)
add_library(example STATIC example.cxx)
target_link_libraries(example ${MATLAB_LIBRARIES})
编译器和链接器都不会发出任何错误.我将输出 example.lib
以 example.mexw64
名称安装在 MATLAB 路径中的目录中.当我从 MATLAB 调用 example
时,我收到错误消息
Neither the compiler nor the linker issues any errors. I install the output example.lib
under the name example.mexw64
in a directory in MATLAB's path. When I call example
from MATLAB, I get the error message
??? Invalid MEX-file
'C:...example.mexw64':
C:...example.mexw64 is not a valid Win32 application.
欢迎提出任何建议!
推荐答案
我遇到了同样的问题,这个链接 非常有帮助(也可以作为一个很好的例子,说明如何在 MATLAB MEX 文件中使用 ITK 顺便说一句).我认为对于您的情况,您需要将链接标志 "/export:mexFunction"
添加到您的 CMakeLists.txt
文件中.下面的例子:
I had the same problem and this link was very helpful (also serves as a nice example of how to use ITK in a MATLAB MEX file btw). I think for your case, you need to add the link flag "/export:mexFunction"
to your CMakeLists.txt
file. Example below:
PROJECT(example)
FIND_PACKAGE(Matlab REQUIRED)
INCLUDE_DIRECTORIES(${MATLAB_INCLUDE_DIR})
ADD_LIBRARY(example MODULE example.cpp)
ADD_DEFINITIONS(-DMATLAB_MEX_FILE)
# Needed for entry point.
SET_TARGET_PROPERTIES(example
PROPERTIES
LINK_FLAGS "/export:mexFunction"
)
# Change the dll extension to a mex extension.
set_target_properties(example PROPERTIES SUFFIX ".mexw64")
TARGET_LINK_LIBRARIES(example ${MATLAB_LIBRARIES})
请注意,FIND_PACKAGE(Matlab)
似乎并没有那么好用,所以这对某些人来说可能是个问题.我不得不通过将 MATLAB 路径硬编码到 CMakeLists.txt(本示例中未显示)来解决这个问题.
Note that the FIND_PACKAGE(Matlab)
doesn't seem to work all that well, so that may be a problem for some people. I had to get around it by hard-coding the MATLAB paths into CMakeLists.txt (not shown in this example).
这篇关于如何使用 CMake 和 Visual Studio 2010(64 位)构建 MATLAB R2011a(64 位)mex 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!