Finds Matlab installations and provides Matlab tools and libraries to cmake.

This package first intention is to find the libraries associated with Matlab in order to be able to build Matlab extensions (mex files). It can also be used:

  • run specific commands in Matlab
  • declare Matlab unit test
  • retrieve various information from Matlab (mex extensions, versions and release queries, ...)

The module supports the following components:

  • MX_LIBRARY and ENG_LIBRARY respectively the MX and ENG libraries of Matlab
  • MAIN_PROGRAM the Matlab binary program.


The version given to the find_package() directive is the Matlab version, which should not be confused with the Matlab release name (eg. R2014). The matlab_get_version_from_release_name() and matlab_get_release_name_from_version() allow a mapping from the release name to the version.

The variable Matlab_ROOT_DIR may be specified in order to give the path of the desired Matlab version. Otherwise, the behaviour is platform specific:

  • Windows: The installed versions of Matlab are retrieved from the Windows registry
  • OS X: The installed versions of Matlab are given by the MATLAB paths in /Application. If no such application is found, it falls back to the one that might be accessible from the PATH.
  • Unix: The desired Matlab should be accessible from the PATH.

Additional information is provided when MATLAB_FIND_DEBUG is set. When a Matlab binary is found automatically and the MATLAB_VERSION is not given, the version is queried from Matlab directly. On Windows, it can make a window running Matlab appear.

The mapping of the release names and the version of Matlab is performed by defining pairs (name, version). The variable MATLAB_ADDITIONAL_VERSIONS may be provided before the call to the find_package() in order to handle additional versions.

A Matlab scripts can be added to the set of tests using the matlab_add_unit_test(). By default, the Matlab unit test framework will be used (>= 2013a) to run this script, but regular .m files returning an exit code can be used as well (0 indicating a success).

Module Input Variables

Users or projects may set the following variables to configure the module behaviour:

the root of the Matlab installation.
outputs debug information
additional versions of Matlab for the automatic retrieval of the installed versions.

Variables defined by the module

Result variables

TRUE if the Matlab installation is found, FALSE otherwise. All variable below are defined if Matlab is found.
the final root of the Matlab installation determined by the FindMatlab module.
the Matlab binary program. Available only if the component MAIN_PROGRAM is given in the find_package() directive.
the path of the Matlab libraries headers
library for mex, always available.
mx library of Matlab (arrays). Available only if the component MX_LIBRARY has been requested.
Matlab engine library. Available only if the component ENG_LIBRARY is requested.
the whole set of libraries of Matlab
the mex compiler of Matlab. Currently not used. Available only if the component MEX_COMPILER is asked

Cached variables

the extension of the mex files for the current platform (given by Matlab).
the location of the root of the Matlab installation found. If this value is changed by the user, the result variables are recomputed.

Provided macros

returns the version from the release name
returns the release name from the Matlab version

Provided functions

adds a target compiling a MEX file.
adds a Matlab unit test file as a test to the project.
parses the registry for all Matlab versions. Available on Windows only. The part of the registry parsed is dependent on the host processor
returns all the possible Matlab paths, according to a previously given list. Only the existing/accessible paths are kept. This is mainly useful for the searching all possible Matlab installation.
returns the suffix to be used for the mex files (platform/architecture dependent)
returns the version of Matlab, given the full directory of the Matlab program.

Known issues

Symbol clash in a MEX target

By default, every symbols inside a MEX file defined with the command matlab_add_mex() have hidden visibility, except for the entry point. This is the default behaviour of the MEX compiler, which lowers the risk of symbol collision between the libraries shipped with Matlab, and the libraries to which the MEX file is linking to. This is also the default on Windows platforms.

However, this is not sufficient in certain case, where for instance your MEX file is linking against libraries that are already loaded by Matlab, even if those libraries have different SONAMES. A possible solution is to hide the symbols of the libraries to which the MEX target is linking to. This can be achieved in GNU GCC compilers with the linker option -Wl,--exclude-libs,ALL.

Tests using GPU resources
in case your MEX file is using the GPU and in order to be able to run unit tests on this MEX file, the GPU resources should be properly released by Matlab. A possible solution is to make Matlab aware of the use of the GPU resources in the session, which can be performed by a command such as D = gpuDevice() at the beginning of the test script (or via a fixture).



The root folder of the Matlab installation. If set before the call to find_package(), the module will look for the components in that path. If not set, then an automatic search of Matlab will be performed. If set, it should point to a valid version of Matlab.


If set, the lookup of Matlab and the intermediate configuration steps are outputted to the console.


If set, specifies additional versions of Matlab that may be looked for. The variable should be a list of strings, organised by pairs of release name and versions, such as follows:




The order of entries in this list matters when several versions of Matlab are installed. The priority is set according to the ordering in this list.


Returns the version of Matlab (17.58) from a release name (R2017k)


Returns the release name (R2017k) from the version of Matlab (17.58)


This function parses the registry and founds the Matlab versions that are installed. The found versions are returned in matlab_versions. Set win64 to TRUE if the 64 bit version of Matlab should be looked for The returned list contains all versions under HKLM\\SOFTWARE\\Mathworks\\MATLAB or an empty list in case an error occurred (or nothing found).


Only the versions are provided. No check is made over the existence of the installation referenced in the registry,


Populates the Matlab root with valid versions of Matlab. The returned matlab_roots is organized in pairs (version_number,matlab_root_path).

the versions of each of the Matlab installations
the location of each of the Matlab installations

Returns the extension of the mex files (the suffixes). This function should not be called before the appropriate Matlab root has been found.

the root of the Matlab installation
the variable name in which the suffix will be returned.

This function runs Matlab program specified on arguments and extracts its version.

the location of the matlab binary executable
the version extracted from Matlab

Adds a Matlab unit test to the test set of cmake/ctest. This command requires the component MAIN_PROGRAM. The unit test uses the Matlab unittest framework (default, available starting Matlab 2013b+) except if the option NO_UNITTEST_FRAMEWORK is given.

The function expects one Matlab test script file to be given. In the case NO_UNITTEST_FRAMEWORK is given, the unittest script file should contain the script to be run, plus an exit command with the exit value. This exit value will be passed to the ctest framework (0 success, non 0 failure). Additional arguments accepted by add_test() can be passed through TEST_ARGS (eg. CONFIGURATION <config> ...).

    NAME <name>
    UNITTEST_FILE matlab_file_containing_unittest.m
    [UNITTEST_PRECOMMAND matlab_command_to_run]
    [TIMEOUT timeout]
    [ADDITIONAL_PATH path1 [path2 ...]]
    [MATLAB_ADDITIONAL_STARTUP_OPTIONS option1 [option2 ...]]
    [TEST_ARGS arg1 [arg2 ...]]

The function arguments are:

name of the unittest in ctest.
the matlab unittest file. Its path will be automatically added to the Matlab path.
Matlab script command to be ran before the file containing the test (eg. GPU device initialisation based on CMake variables).
the test timeout in seconds. Defaults to 180 seconds as the Matlab unit test may hang.
a list of paths to add to the Matlab path prior to running the unit test.
a list of additional option in order to run Matlab from the command line.
Additional options provided to the add_test command. These options are added to the default options (eg. “CONFIGURATIONS Release”)
when set, indicates that the test should not use the unittest framework of Matlab (available for versions >= R2013a).

Adds a Matlab MEX target. This commands compiles the given sources with the current tool-chain in order to produce a MEX file. The final name of the produced output may be specified, as well as additional link libraries, and a documentation entry for the MEX file. Remaining arguments of the call are passed to the add_library() command.

    NAME <name>
    SRC src1 [src2 ...]
    [OUTPUT_NAME output_name]
    [DOCUMENTATION file.txt]
    [LINK_TO target1 target2 ...]
name of the target.
list of tje source files.
a list of additional link dependencies. The target links to libmex by default. If Matlab_MX_LIBRARY is defined, it also links to libmx.
if given, overrides the default name. The default name is the name of the target without any prefix and with Matlab_MEX_EXTENSION suffix.
if given, the file file.txt will be considered as being the documentation file for the MEX file. This file is copied into the same folder without any processing, with the same name as the final mex file, and with extension .m. In that case, typing help <name> in Matlab prints the documentation contained in this file.

The documentation file is not processed and should be in the following format:

% This is the documentation
function ret = mex_target_output_name(input1)