CMake/MatlabMex: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(Replace content with link to new CMake community wiki)
 
(18 intermediate revisions by 5 users not shown)
Line 1: Line 1:
=What are Matlab mex files?=
{{CMake/Template/Moved}}


Mex files are functions written in C or C++ that are callable in the Matlab scripting language in a Matlab interpretor.  [http://www.mathworks.com/matlab Matlab] is a 'Matrix Laboratory', a general purpose scientific scripting language owned by [http://www.mathworks.com The Mathworks] company.
This page has moved [https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/MatlabMex here].
 
Details of writing mex files can be found in the [http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/bp_kqh7.html 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 [http://scipy.org SciPy], [http://www.scilab.org SciLab], [http://www.sagemath.org/ Sage], or [http://www.gnu.org/software/octave/index.html 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, [[Really_Cool_CMake_Features | 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 [http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f24338.html 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
mex -setup
allows you to setup the configuration file the mex script uses to determine which compiler it will use, the flags it will pass, etc. 
mex -v
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
 
This method requires a patched version of CMake.  The patch can be obtained XXXhereXXX.
 
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 [http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f24338.html 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)
or
add_library(library_name MODULE sources.cpp)
 
=Hello World Example=
 
=More complex example: ITK based image reader=
 
=Platform specific issues=

Latest revision as of 15:40, 30 April 2018


The CMake community Wiki has moved to the Kitware GitLab Instance.

This page has moved here.