[Cmake] Using CMake to build MEX files for Matlab

gareth.jones at stud.man.ac.uk gareth.jones at stud.man.ac.uk
Wed Jan 8 09:17:01 EST 2003


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?  

Thank you,

Gareth Jones



More information about the CMake mailing list