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
.
- 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})
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.
Any suggestions are welcome!
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})
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).
Matlab mex files are dll`s not libs. Try making cmake (not an expert on that) create a dynamic librart، not static.
来源:https://stackoverflow.com/questions/10218626/how-to-use-cmake-and-visual-studio-2010-64-bit-to-build-a-matlab-r2011a-64-bi