[CMake] Building Matlab Mex Functions: Best Approach?

Adam Weiss adam at signal11.com
Thu May 29 20:13:09 EDT 2008


Hey Folks,

I'm new to cmake and am in the process of converting a Linux based project
over to use it.  The project I'm converting includes a lot of Matlab mex
extensions.  I've done a good deal of internet research (both archives of
this list and Google) and I've yet to really garner a good sense of the best
way to approach this.

For the uninitiated, Matlab mex extensions are snippets of C compiled
against some headers and libraries provided by Matlab to produce a loadable
shared library.  Compilation of these extensions can be kicked off by either
running commands within Matlab (forget it!) or via an external shell script
that behaves somewhat like gcc.  They do some fairly weird stuff (they give
the loadable libraries platform based filename extensions, among other
things.)  Some more adventurous folks appear to have reverse engineered this
shell script and have implemented it in cmake, but that seems like a lot of
work.  So a few quick questions:

1) If there's anyone out there with Matlab mex inside CMake builds
experience, what is the best route for implementing this?  Hacking the
functionality of mex.sh into a CMake script or configuring CMake to run
mex.sh to do the compilation?

2) Suppose I wanted to go ahead and use mex.sh?  I suspect I'd need to use
ADD_CUSTOM_TARGET and ADD_CUSTOM_COMMAND, but I've been unsuccessful as of
yet to get them to do much....   (here's a quick excerpt of what I've tried,
it prints "foo built" but AFAIK nothing is happening)  Can someone help
steer me in the right direction on this?

ADD_CUSTOM_COMMAND(
    OUTPUT    foo.mexa64
    COMMAND   mex
    ARGS      ${CMAKE_CURRENT_SOURCE_DIR}/foo.c
    DEPENDS   ${CMAKE_CURRENT_SOURCE_DIR}/foo.c
    COMMENT   "get_analog_state"
  )

ADD_CUSTOM_TARGET(foo ALL echo
    DEPENDS   ${CMAKE_CURRENT_SOURCE_DIR}/foo.c
  )

Thanks in advance for any help!

--adam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20080529/dc67d0f2/attachment.htm>


More information about the CMake mailing list