FindMatlab¶
Finds MATLAB or MATLAB Compiler Runtime (MCR) and provides its tools, libraries and compilers to CMake:
find_package(Matlab [<version>] [COMPONENTS <components>...] [REGISTRY_VIEW <view>] [...])
The primary purpose of this module is to find the libraries associated with MATLAB or the MCR in order to be able to build MATLAB extensions (MEX files). It can also be used:
to run specific commands in MATLAB in case MATLAB is available
for declaring MATLAB unit tests
to retrieve various information from MATLAB (MEX extensions, versions and release queries, ...)
Added in version 3.12: MATLAB Compiler Runtime (MCR) support.
Added in version 3.30: Support for specifying a version range to find_package(),
support for specifying REGISTRY_VIEW argument to
find_package(),
matlab_extract_all_installed_versions_from_registry() and
matlab_get_all_valid_matlab_roots_from_registry() commands.
The default behavior remained unchanged, by using the registry view
TARGET.
Note
The version given to the find_package() argument is the MATLAB
version, which should not be confused with the MATLAB release name
(e.g. R2023b). The matlab_get_version_from_release_name()
and matlab_get_release_name_from_version() commands provide a
mapping between the release name and the version.
Components¶
This module supports optional components which can be specified using the
find_package() command:
find_package(Matlab [COMPONENTS <components>...])
Supported components include:
ENG_LIBRARYAdded in version 3.3.
Finds the
ENGlibrary of MATLAB.MAT_LIBRARYAdded in version 3.7.
Finds the
MATlibrary of MATLAB.MAIN_PROGRAMAdded in version 3.3.
Finds the MATLAB binary program. Note that this component is not available on the MCR version, and will yield an error if the MCR is found instead of the regular MATLAB installation.
MEX_COMPILERAdded in version 3.3.
Finds the MEX compiler.
MCC_COMPILERAdded in version 3.13.
Finds the MCC compiler, included with the MATLAB Compiler add-on.
SIMULINKAdded in version 3.3.
Finds the Simulink environment.
Implicitly Found Components¶
The following components are always found unconditionally, without needing to be specified explicitly:
MX_LIBRARYChanged in version 3.14: This component has been removed and is now always found unconditionally.
Added in version 3.3.
Finds the MATLAB mx library.
ENGINE_LIBRARYChanged in version 3.14: This component has been removed and is now always found unconditionally.
Added in version 3.13.
Finds the MATLAB engine library.
DATAARRAY_LIBRARYChanged in version 3.14: This component has been removed and is now always found unconditionally.
Added in version 3.13.
Finds the C++ MATLAB data array library.
If no components are specified, the module looks for the MX_LIBRARY,
ENGINE_LIBRARY, and DATAARRAY_LIBRARY by default.
Imported Targets¶
This module provides the following Imported Targets:
Matlab::mexAdded in version 3.22.
Target encapsulating the
mexlibrary usage requirements, always available for MATLAB installations. Available for MCR installations if provided by MCR.Matlab::mxAdded in version 3.22.
Target encapsulating the usage requirements of the mx library of MATLAB (arrays), always available for MATLAB installations. Available for MCR installations if provided by MCR.
Matlab::engAdded in version 3.22.
Target encapsulating the MATLAB engine library usage requirements. Available only if the
ENG_LIBRARYcomponent is requested.Matlab::matAdded in version 3.22.
Target encapsulating the MATLAB matrix library usage requirements. Available only if the
MAT_LIBRARYcomponent is requested.Matlab::MatlabEngineAdded in version 3.22.
Target encapsulating the MATLAB C++ engine library usage requirements, always available for MATLAB R2018a and newer. Available for MCR installations if provided by MCR.
Matlab::MatlabDataArrayAdded in version 3.22.
Target encapsulating the MATLAB C++ data array library usage requirements, always available for MATLAB R2018a and newer. Available for MCR installations if provided by MCR.
Result Variables¶
This module defines the following variables:
Matlab_FOUNDAdded in version 3.3.
Boolean indicating whether the (requested version of) MATLAB installation was found. All variables below are defined if MATLAB is found.
Matlab_VERSIONAdded in version 3.27.
The numerical version (e.g.
23.2.0) of MATLAB found. Not to be confused with MATLAB release name (e.g.R2023b) that can be obtained withmatlab_get_release_name_from_version().When a MATLAB/MCR installation is found automatically and the
Matlab_VERSIONis not given, the version is queried from MATLAB directly (on Windows this may pop up a MATLAB window) or from the MCR installation.Matlab_ROOT_DIRAdded in version 3.3.
The final root of the MATLAB installation determined by this module.
Matlab_MAIN_PROGRAMAdded in version 3.3.
The MATLAB binary program. Available only if the component
MAIN_PROGRAMis given in thefind_package()argument.Matlab_INCLUDE_DIRSAdded in version 3.3.
The path of the MATLAB libraries headers.
Matlab_MEX_LIBRARYLibrary for MEX, always available for MATLAB installations. Available for MCR installations if provided by MCR.
Matlab_MX_LIBRARYThe mx library of MATLAB (arrays), always available for MATLAB installations. Available for MCR installations if provided by MCR.
Matlab_ENG_LIBRARYMATLAB engine library. Available only if the component
ENG_LIBRARYis requested.Matlab_MAT_LIBRARYAdded in version 3.7.
Matlab matrix library. Available only if the component
MAT_LIBRARYis requested.Matlab_ENGINE_LIBRARYAdded in version 3.13.
Matlab C++ engine library, always available for MATLAB R2018a and newer. Available for MCR installations if provided by MCR.
Matlab_DATAARRAY_LIBRARYAdded in version 3.13.
Matlab C++ data array library, always available for MATLAB R2018a and newer. Available for MCR installations if provided by MCR.
Matlab_LIBRARIESThe whole set of libraries of MATLAB.
Matlab_MEX_COMPILERAdded in version 3.3.
The MEX compiler of MATLAB. Currently not used. Available only if the component
MEX_COMPILERis requested.Matlab_MCC_COMPILERAdded in version 3.13.
The mcc compiler of MATLAB. Included with the MATLAB Compiler add-on. Available only if the component
MCC_COMPILERis requested.
Cache Variables¶
The following cache variables may also be set:
Matlab_MEX_EXTENSIONAdded in version 3.3.
The extension of the MEX files for the current platform (given by MATLAB).
Matlab_ROOT_DIRAdded in version 3.3.
The location of the root of the MATLAB installation found. If this value is changed by the user, the result variables are recomputed.
Input Variables¶
Users or projects may set the following variables to configure the module
behavior before calling find_package(Matlab):
Matlab_ROOTAdded in version 3.25.
Default value for the
Matlab_ROOT_DIRvariable, the root of the MATLAB installation.Matlab_ROOT_DIRAdded in version 3.3.
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.This variable may be specified in order to give the path of the desired MATLAB version. Otherwise, the behavior is platform specific:
Windows: The installed versions of MATLAB/MCR are retrieved from the Windows registry. The
REGISTRY_VIEWargument may optionally be specified to manually control whether 32bit or 64bit versions shall be searched for.macOS: The installed versions of MATLAB/MCR are given by the MATLAB default installation paths under
$HOME/Applicationsand/Applications. If no such application is found, it falls back to the one that might be accessible from thePATH.Unix: The desired MATLAB should be accessible from the
PATH. This does not work for MCR installation andMatlab_ROOT_DIRshould be specified on this platform.
MATLAB_FIND_DEBUGAdded in version 3.3.
If set to a boolean true, additional debug information is outputted to the console, such as the lookup of MATLAB and the intermediate configuration steps.
MATLAB_ADDITIONAL_VERSIONSAdded in version 3.3.
If set, it specifies additional versions of MATLAB for the automatic retrieval of the installed versions that may be handled and looked for.
The mapping of the release names and the version of MATLAB is performed by defining pairs (name, version). The variable should be a list of strings, organized by pairs of release name and versions, such as follows:
set( MATLAB_ADDITIONAL_VERSIONS "release_name1=corresponding_version1" "release_name2=corresponding_version2" # ... ) find_package(Matlab)
Example:
set( MATLAB_ADDITIONAL_VERSIONS "R2013b=8.2" "R2013a=8.1" "R2012b=8.0" # ... ) find_package(Matlab)
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.
Commands¶
This module provides the following commands:
- matlab_get_version_from_release_name¶
Added in version 3.3.
Returns the version of MATLAB from a release name:
matlab_get_version_from_release_name(<release-name> <version-var>)
The arguments are:
<release-name>Input release name string (e.g.
R2023b).<version-var>The name of the variable in which to store the version of MATLAB. The output result is e.g.
23.2.0.
Note
This command provides correct versions mappings for MATLAB but not MCR.
- matlab_get_release_name_from_version¶
Added in version 3.3.
Returns the release name from the version of MATLAB:
matlab_get_release_name_from_version(<version> <release-name-var>)
The arguments are:
<version>Input MATLAB version string (e.g.
23.2.0).<release-name-var>The name of the variable in which to store the MATLAB release name. The output result is e.g.
R2023b.
Note
This command provides correct version mappings for MATLAB but not MCR.
- matlab_get_version_from_matlab_run¶
Added in version 3.3.
Runs the specified MATLAB program and extracts the version of MATLAB/MCR, given the full directory of the MATLAB/MCR installation path:
matlab_get_version_from_matlab_run(<matlab-binary> <matlab-versions-var>)
The arguments are:
<matlab-binary>The path to the
matlabbinary executable.<matlab-versions-var>The name of the variable in which a list of extracted MATLAB versions are stored.
If the path provided for the MATLAB installation points to an MCR installation, the version is extracted from the installed files.
- matlab_extract_all_installed_versions_from_registry¶
Added in version 3.3.
Parses the Windows registry and finds all installed MATLAB versions:
- matlab_extract_all_installed_versions_from_registry(<versions-var> [REGISTRY_VIEW <view>])¶
The arguments are:
<versions-var>The name of the variable in which to store the list of all MATLAB versions found.
REGISTRY_VIEW <view>Added in version 3.30.
Optional registry view argument that provides a more precise interface on how to interact with the Windows Registry. The argument is passed (or omitted) to
cmake_host_system_information()without further checks or modification. For example,<view>value can be one of64,32,64_32,32_64,HOST,TARGET,BOTH.
The old signature is:
- matlab_extract_all_installed_versions_from_registry(<win64> <versions-var>)¶
The arguments are:
<win64>Boolean whether to search for the 64-bit version of MATLAB. If set to boolean true, 64-bit registry view will be searched. If set to boolean false, 32-bit registry view will be searched. For finer control, use the above signature.
<versions-var>The name of the variable in which to store the list of all MATLAB versions found.
This command is available on Windows only. The part of the registry parsed is dependent on the host processor.
The returned list contains all versions under
HKLM\SOFTWARE\Mathworks\MATLAB,HKLM\SOFTWARE\Mathworks\MATLAB RuntimeandHKLM\SOFTWARE\Mathworks\MATLAB Compiler Runtimeor an empty list in case an error occurred (or nothing found).Note
Only the versions are provided. No check is made over the existence of the installation referenced in the registry.
- matlab_get_all_valid_matlab_roots_from_registry¶
Added in version 3.3.
Returns all the possible MATLAB or MCR paths, according to a previously given list:
matlab_get_all_valid_matlab_roots_from_registry( <matlab-versions> <matlab-roots-var> [REGISTRY_VIEW <view>] )
This command populates the MATLAB root with valid versions of MATLAB or MATLAB Runtime (MCR) and is mainly useful for the searching of all possible MATLAB installations. Only the existing/accessible paths are kept.
The arguments are:
<matlab-versions>A semicolon-separated list of each of the MATLAB or MCR installations. Specify it as a single string value.
<matlab-roots-var>The name of the variable in which to store a list of locations of each of the MATLAB or MCR installations.
The value of this variable is organized in triplets
(type,version_number,matlab_root_path), wheretypeindicates eitherMATLABorMCR.REGISTRY_VIEW <view>Added in version 3.30.
Optional registry view argument that provides a more precise interface on how to interact with the Windows Registry. The argument is passed (or omitted) to
cmake_host_system_information()without further checks or modification. For example,<view>value can be one of64,32,64_32,32_64,HOST,TARGET,BOTH.
- matlab_add_mex¶
Added in version 3.3.
Adds a target that compiles MATLAB MEX target file:
matlab_add_mex( NAME <name> [EXECUTABLE | MODULE | SHARED] SRC <sources>... [OUTPUT_NAME <output-name>] [DOCUMENTATION <file>] [LINK_TO <targets>...] [R2017b | R2018a] [EXCLUDE_FROM_ALL] [NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIES] [...] )
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()oradd_executable()command.The arguments are:
NAME <name>The name of the target.
SRC <sources>...One or more source files to be compiled.
LINK_TO <targets>...A list of additional link dependencies. The target links to
libmexandlibmxby default, unless theNO_IMPLICIT_LINK_TO_MATLAB_LIBRARIESoption is passed.OUTPUT_NAME <output-name>If given, overrides the default name. The default name is the name of the target without any prefix and with
Matlab_MEX_EXTENSIONsuffix.DOCUMENTATION <file>If given, the
<file>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, typinghelp <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)
R2017borR2018aAdded in version 3.14.
May be given to specify the version of the C API to use:
R2017bspecifies the traditional (separate complex) C API, and corresponds to the-R2017bflag for themexcommand.R2018aspecifies the new interleaved complex C API, and corresponds to the-R2018aflag for themexcommand. Ignored for MATLAB versions prior to R2018a. Defaults toR2017b.MODULEorSHAREDAdded in version 3.7.
May be given to specify the type of library to be created.
EXECUTABLEAdded in version 3.7.
May be given to create an executable instead of a library. If no type is given explicitly, the default type is
SHARED.EXCLUDE_FROM_ALLThis option has the same meaning as the
EXCLUDE_FROM_ALLtarget property and is forwarded to theadd_library(), oradd_executable()command.NO_IMPLICIT_LINK_TO_MATLAB_LIBRARIESAdded in version 3.24.
This option permits to disable the automatic linking of MATLAB libraries, so that only the libraries that are actually required can be linked via the
LINK_TOoption.
- matlab_get_mex_suffix¶
Added in version 3.3.
Returns the extension to be used for the MEX files (the suffix):
matlab_get_mex_suffix(<matlab-root> <mex-suffix-var>)
The arguments are:
<matlab-root>The root of MATLAB/MCR installation. For example, the value of the
Matlab_ROOT_DIRvariable.<mex-suffix-var>The name of the variable in which the suffix will be returned.
This command is platform and architecture dependent. It should not be called before the appropriate MATLAB root has been found.
- matlab_add_unit_test¶
Added in version 3.3.
Adds a MATLAB unit test file to the project's test set of CMake/CTest:
matlab_add_unit_test( NAME <name> UNITTEST_FILE <matlab-file-containing-unittest.m> [CUSTOM_TEST_COMMAND <matlab-command-to-run-as-test>] [UNITTEST_PRECOMMAND <matlab-command-to-run>] [TIMEOUT <timeout>] [ADDITIONAL_PATH <paths>...] [MATLAB_ADDITIONAL_STARTUP_OPTIONS <options>...] [TEST_ARGS <args>...] [NO_UNITTEST_FRAMEWORK] [WORKING_DIRECTORY <dir>] )
By default, the MATLAB unit test framework will be used (>= 2013a) to run the added MATLAB script, but regular
.mfiles returning an exit code can be used as well (0 indicating a success).This command requires the component
MAIN_PROGRAMand hence is not available for an MCR installation.The unit test uses the MATLAB unittest framework (default, available starting MATLAB 2013b+) except if the option
NO_UNITTEST_FRAMEWORKis given.The command expects one MATLAB test script file to be given. In the case
NO_UNITTEST_FRAMEWORKis 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 byadd_test()can be passed throughTEST_ARGS(e.g.CONFIGURATIONS <config> ...).The arguments are:
NAME <name>The name of the unittest in ctest.
UNITTEST_FILE <matlab-file-containing-unittest.m>The MATLAB unittest file. Its path will be automatically added to the MATLAB path.
CUSTOM_TEST_COMMAND <matlab-command-to-run-as-test>MATLAB script command to run as the test. If this is not set, then the following is run:
runtests('matlab_file_name'), exit(max([ans(1,:).Failed]))wherematlab_file_nameis theUNITTEST_FILEwithout the extension.UNITTEST_PRECOMMAND <matlab-command-to-run>MATLAB script command to be ran before the file containing the test (e.g. GPU device initialization based on CMake variables).
TIMEOUT <timeout>The test timeout in seconds. Defaults to 180 seconds as the MATLAB unit test may hang.
ADDITIONAL_PATH <paths>...A list of paths to add to the MATLAB path prior to running the unit test.
MATLAB_ADDITIONAL_STARTUP_OPTIONS <options>A list of additional option in order to run MATLAB from the command line. The
-nosplash,-nodesktop, and-nodisplayoptions are always added automatically.TEST_ARGS <args>...Additional options provided to the add_test command. These options are added to the default options (e.g.
CONFIGURATIONS Release).NO_UNITTEST_FRAMEWORKWhen set, indicates that the test should not use the unittest framework of MATLAB (available for versions >= R2013a).
WORKING_DIRECTORY <dir>This will be the working directory for the test. If specified it will also be the output directory used for the log file of the test run. If not specified the temporary directory
${CMAKE_BINARY_DIR}/Matlabwill be used as the working directory and the log location.
Known Issues¶
- Symbol clash in a MEX target
By default, every symbol inside a MEX file defined with the command
matlab_add_mex()have hidden visibility, except for the entry point. This is the default behavior 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 the 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 the 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).
Examples¶
Finding MATLAB and linking imported target to a project target:
find_package(Matlab)
target_link_libraries(example PRIVATE Matlab::mx)