[Cmake] Using CMake to build MEX files for Matlab

Andy Cedilnik andy.cedilnik at kitware.com
Wed Jan 8 09:41:00 EST 2003


Hi Gareth,

If everything else fails, you can always do this:

# First create regular librayr
ADD_LIBRARY(yourlib ${yourlib_srcs})

# Now rename
IF(WIN32)
  SET(PREFIX "")
  SET(CMAKE_SHLIB_SUFFIX ".dll")
ELSE(WIN32)
  SET(PREFIX "lib")
ENDIF(WIN32)

SET(INNAME "${PREFIX}yourlib${CMAKE_SHLIB_SUFFIX}")
SET(OUTNAME "whatever_you_want_yourlib.mexglx)

ADD_CUSTOM_COMMAND(TARGET yourlib
                   SOURCE yourlib
                   COMMAND ${CMAKE_COMMAND}
                   ARGS -E 
                   copy
${LIBRARY_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/${INNAME}
${LIBRARY_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/${OUTNAME}
                   )


This should work on all platforms.
Starting with CMake 1.6, there are easier way of doing that, but I would
start with this.

			Andy

On Wed, 2003-01-08 at 09:17, gareth.jones at stud.man.ac.uk wrote:
> I've been trying to get CMake to build MEX files (compiled functions
> that Matlab can execute) but it is not clear to me quite how I should
> go about this.  I can write a FindMatlab.cmake module that gets paths
> and sets variables and so on but I am not sure how best to actually
> build non-trivial MEX files.
> 
> A MEX file is just a shared object (or dll on Windows) that Matlab
> loads using the dynamic loader (or presumably the Windows equivalent).
> Its main distinguishing features are its name: instead of libfoo.so
> (or foo.dll) it is foo.mex (the suffix is actually system dependent
> but is easily predicted by FindMatlab.cmake) and that it defines (and
> exports) mexFunction and mexVersion symbols.  It usually requires some
> extra arguments to be passed to the linker.  In particular, on Linux
> --version-script should be passed.
> 
> It is possible to build MEX files (at least on linux) by setting
> CMAKE_SHARED_LIBRARY_SUFFIX and _PREFIX to "" and ".mexglx", but this
> causes problems if other, ordinary, libraries are build in the same
> CMakeLists.txt.  I know that it is possible to use ADD_CUSTOM_COMMAND
> to copy the library but this does not seem particularly elegant and it
> prevents both libfoo.so and foo.mex being built in the same directory.
> 
> I've used CMAKE_SHARED_LINKER_FLAGS to add the MEX specific linker
> flags but again this is bad because it is used by all shared objects
> (and doubly bad because the MEX flags get added to the variable so if
> two MEX files are compiled then the linker presumably gets the options
> twice).
> 
> In summary, what I want is a way to specify the precise output name
> for a single library target, and to specify link options on a
> per-target basis rather than globally.  Is there any way to do this in
> CMake as it stands?  





More information about the CMake mailing list