- 1 What are Matlab mex files?
- 2 Why would you want to use CMake for creating mex files?
- 3 How do you use CMake with Matlab?
- 4 Hello World Example
- 5 More complex example: ITK based image reader
- 6 Platform specific issues
What are Matlab mex files?
Mex files are functions written in C or C++ that are callable in the Matlab scripting language in a Matlab interpretor. Matlab is a 'Matrix Laboratory', a general purpose scientific scripting language owned by The Mathworks company.
Details of writing mex files can be found in the Matlab documentation.
Before you begin
Matlab is a language with good syntax for working with matrices, a variety of functions, excellent documentation, and it is ubiquitous throughout the academic, scientific community. On the other hand, it is extremely slow, has poor memory utilization, is prohibitively expensive, sees little use outside academia, has a language syntax that is only good for simple procedural scripting, is closed source, and has numerous bugs. Writing mex files is often an attempt to address some of these limitations -- speed and memory usage. However, it is a difficult and not very elegant solution. Instead, researchers should consider using superior products, such as SciPy, SciLab, Sage, or Octave.
Why would you want to use CMake for creating mex files?
If you have a very small project that consists of only a single source code file that does not link to other libraries, then simply using the mex compiler at the command line is all that you need. However, if your project is non-trivial, there are many advantages to using CMake. A make system allows you to recompile and link only the files that change during development and to perform parallel builds. CMake provides a configuration system finding libraries on systems. The are many advantages to organizing and controlling the build system in a simple and powerful way, which CMake can do, among other things.
How do you use CMake with Matlab?
Background on the Matlab mex "compiler"
Matlab comes with a mex "compiler". It is not a true compiler, but a frontend build script to the compiler of your choice. On MS Windows it is a perl script, and on Unix-like systems it is a shell script, although they have the same user interface. Issuing
allows you to setup the configuration file the mex script uses to determine which compiler it will use, the flags it will pass, etc.
shows the compiler and settings the build script is using.
The mex compiler can process C, C++, or Fortran code. The code must contain a gateway function with specific syntax that Matlab hooks into. Primary Matlab itself is written is C, and a variety of functions are available in the mex.h header for interrogating Matlab data structures, allocating memory in a way that it is controlled by Matlab, etc. The result of this process is a shared library (dll) mexfile that is loaded by by the Matlab interpretor when the basename is found in Matlab's path.
How can CMake fit into this process?
There are two basic approaches to utilize CMake for the process of generating mexfiles.
Try to emulate the logic built into the mex build script in CMakeLists.txt
Since mexfiles are simply native shared libraries with some special functions, etc., it is possible to bypass the mex build script completely. The logic built into the mex built script can be replaced by appropriate scripting of the CMakeLists.txt.
Unfortunately, this approach requires significant effort, and it is difficult to maintain. In addition to the mex gateway function, a mex file requires specific preprocessor definitions, compiler flags, link scripts, linked libraries, etc. These must be reproduced in the CMakeLists.txt. The options vary across compilers and across platforms. Furthermore, Mathworks produces new Matlab versions twice a year, and these options change across versions. Configuration must be maintained for every Matlab version that is desired, and new releases of Matlab likely break the build process.
Tell CMake to treat the mex build script as the project's compiler
The other option is to tell CMake to treat the mex build script as the project's compiler. This approach dulls the power of CMake in some cases due to the limited capabilities of the mex script, but it makes project much more maintainable. This article describes this approach.
If the mex script is in the system's PATH environment variable, setting the CC or CXX environmental variable is all that is required:
CC=mex CXX=mex cmake /path/to/project/source
There are some differences from normal CMake usage when using the mex build script as the system compiler. Instead of configuring additional compiler flags from within CMake, compiler flags for the underlying compiler must be set in the mexopts configuration file. Compiler flags configured in CMake should be options sent to the mex compiler.
It is still possible to use the
include_directories() link_directories() target_link_libraries()
scripting directives in the project's CMakeLists.txt.
Since all mexfiles are shared libraries,
add_executable() add_library(library_name STATIC sources.cpp)
are not valid ways for generating targets. CMake targets should be made with
add_library(library_name SHARED sources.cpp)
add_library(library_name MODULE sources.cpp)