Examples of building Matlab MEX and Matlab Engine targets using Matlab supported compilers. Since R2023b, Matlab's own build system has become quite capable and is recommended over CMake for new projects.
One-time setup from Matlab:
mex -setup c
mex -setup c++
mex -setup fortran
mex -setup -client engine c
mex -setup -client engine c++
mex -setup -client engine fortran
Using Matlab's own build system to build and test MEX examples from the Matlab Command Window:
buildtool test:mex
Matlab Engine is available from several languages including C, C++, Fortran, Python, ... For compiled Matlab Engine programs, the appropriate "matlab" executable must be in environment variable PATH.
buildtool test:engine
The Matlab Engine examples might be shaky on certain systems and configurations. Try running each example individually to see if any work. List tasks by:
buildtool -tasks all
Examples of running individual tests:
buildtool test:engine:c
buildtool test:engine:cpp
buildtool test:engine:fortran
CMake may be used to build and test MEX and Matlab Engine examples:
cmake -B build
cmake --build build
ctest --test-dir build -L mex -V
ctest --test-dir build -L engine -V
Currently, there is a
known CMake bug
with matlab_add_mex()
for Fortran that causes runtime failures of MEX binaries.
This happens on any operating system or Fortran compiler due to the issue with CMake matlab_add_mex()
.
Invalid MEX-file 'matsq.mexa64': Gateway function is missing
Matlab has narrow windows of compiler versions that work for each Matlab release. Especially on Linux, this may require using a specific release of Matlab compatible such that Matlab libstdc++.so and system libstdc++.so are compatible. This is because compiler-switching mechanisms like RHEL Developer Toolset still use system libstdc++ that lack newer GLIBCXX symbols.
- R2022a .. R2024b: Linux: GCC 10
- R2020b .. R2021b: Linux: GCC 8
A frequent issue on Linux systems is failure to link with libstdc++.so.6 correctly. Depending on the particular Matlab version and system libstdc++, putting Matlab libstdc++ first in LD_LIBRARY_PATH may help:
LD_LIBRARY_PATH=<matlab_root>/sys/os/glnxa64/ cmake -Bbuild
It may be necessary to try different Matlab versions to find one Linux compatible with the particular Linux operating system vendor and version.
Matlab MEX compiler ignores environment variables like CFLAGS, CXXFLAGS, FFLAGS.