Using C, C++, or Fortran Code with MATLAB on Cypress
Note: The following included code samples are for reference with matlab/r2023a and gcc/10.5.0. Otherwise, refer to the table GCC modules for new native-code builds below for the appropriate supported gcc version.
Most MATLAB users do not need a C/C++ compiler. If your work consists of MATLAB .m files, built-in MATLAB functions, toolboxes, or running an existing MATLAB application, load MATLAB normally:
module load matlab/r2023a
When is GCC needed?
Load GCC when you are building C, C++, or Fortran code for use from MATLAB. This includes:
- Running
mex ...to build an extension. - Compiling a Simulink S-function.
- Building MATLAB Coder generated native code.
- Building code that lets MATLAB call an existing C/C++/Fortran library, hardware SDK, or vendor software.
A MEX file is a compiled extension that lets MATLAB call C/C++/Fortran code. On Linux, its filename normally ends in .mexa64.
GCC modules for new native-code builds
| MATLAB module | GCC module to use for new MEX / S-function / native-code builds |
|---|---|
matlab/r2017b | gcc/4.9.4
|
matlab/r2020a | gcc/6.3.0
|
matlab/r2022b | gcc/9.5.0
|
matlab/r2023a | gcc/10.5.0
|
Supported compiler workflow
Currently, the matlab/r2023a module is configured to select compiler gcc/4.7.4, which is not supported and can result in compiler errors during compilation with mex. Hence, for new MEX files, Simulink S-functions, or MATLAB Coder native-code builds, load gcc/10.5.0 after MATLAB:
module purge module load matlab/r2023a module load gcc/10.5.0
Then start MATLAB and build the extension, for example:
mex mycode.cpp
Confirm that GCC 10.5 is selected before starting MATLAB:
which gcc g++ gcc --version g++ --version
The compiler paths should be under:
/share/apps/gcc/10.5.0/bin/
Why load GCC 10.5 explicitly?
Without a GCC module, CentOS 7 uses its system compiler, GCC 4.8.5. This is not the supported environment for new MATLAB R2023a native-code builds. GCC 4.7.4 is also unsupported for new R2023a C++ MEX builds.
Loading gcc/10.5.0 explicitly makes the compiler choice visible and prevents MATLAB from silently using the older system compiler.
Existing compiled extensions
GCC is required only to compile or rebuild native C, C++, or Fortran code, such as MEX files, Simulink S-functions, and MATLAB Coder generated code, not to run MATLAB or an extension that is already compiled.
If you maintain an existing C++ extension, rebuild and test it with GCC 10.5 before distributing the rebuilt version.
MATLAB Compiler (mcc)
mcc packages MATLAB .m programs for deployment with MATLAB Runtime and does not normally require GCC. If an application includes a MEX file or another C/C++ shared library, that native component must be compiled separately using the workflow above.
