[CMake] Building Matlab Mex Functions: Best Approach?

Maik Beckmann beckmann.maik at googlemail.com
Fri May 30 06:06:19 EDT 2008


Am Freitag 30 Mai 2008 02:13:09 schrieb Adam Weiss:
> 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?

I started using cmake for this two years ago. 


A note about mex extensions for those who don't know them:
A mex extension is similar to a C/C++ python module. Its a shared library 
which has an entry function called  mexFunction
{{{
void mexFunction(
  int ret_num, /*out*/
  mxArray** ret_ptrs,  /out*/
  int arg_num, /*in*/
  mxArray const** arg_ptrs /*in*/
) 
{...}
}}}

Thus the right cmake command to build a mexfile is 
  add_library(foo SHARED ...).  

But it isn't that simple :)


I've done my matlab work on windows, thus I'll explain what to do at this 
platform.  Since my personal desktop is a Linux one I will add notes what I 
expect matlab on linux will require.

 It works like a charm once you overcome some issues:
  1. if using the gcc, don't add the matlabs lib folder to link_directories
  2. export symbols via a .def file if using msvc.
  3. choose the proper extension (mexw32, mexw64, mexglx, mexa64)


The first issue is one because matlab ships libs which got the same name as 
those shipped with the gcc. The linker is confused.  I overcome it by doing 
this at the toplevel CMakeLists.txt
{{{
set(MATLAB_MEX_LIBS
  libmex.dll
  libmx.dll
  libmat.dll )
foreach(item ${MATLAB_MEX_LIBS} )
  configure_file(${MATLAB_ROOT}/bin/win32/${item} 
${CMAKE_CURRENT_BINARY_DIR}/${item} COPYONLY )
endforeach(item)
}}}
on linux replace .dll with .so and /bin/win32/${item} 
with /bin/glnx86/${item}. 

I didn't try to use find_library yet as I would today, since I wrote the above 
stuff at my very first cmake days and it just worked. 


The second one is simple two solve.  Just create a .def file containing
{{{
EXPORTS 
	mexFunction 
}}}
I called it mexFunction.def :) and placed it in the toplevel dir.


The third issue is solved by setting i.e.
  set(MATLAB_MEXFILE_EXT mexw32)


Now you can add an arbitrary number of mexfiles via i.e.
{{{
add_library(foo SHARED bar.cpp ${CMAKE_SOURCE_DIR}/mexFunction.def)
target_link_libraries(foo ${MATLAB_MEX_LIBS})
set_target_properties(foo PROPERTIES
    PREFIX ""
    SUFFIX  ".${MATLAB_MEXFILE_EXT}"
)
}}}


One day I'll overwork all this and add it to the cmake wiki.  Maybe it will be 
good enough for being a cmake module.



Hope this helps,
 -- Maik


PS: Ah, I found an old message of mine at this list
  http://tinyurl.com/3vvrmz
maybe it is helpful for you.


More information about the CMake mailing list