| | 1 | [[PageOutline]] |
| | 2 | |
| | 3 | = Using C, C++, or Fortran Code with MATLAB on Cypress = |
| | 4 | |
| | 5 | '''Note:''' The following included code samples are for reference with `matlab/r2023a` and `gcc/10.5.0`. Otherwise, refer to the table [#GCCmodulesfornewnative-codebuilds GCC modules for new native-code builds] below for the appropriate supported gcc version. |
| | 6 | |
| | 7 | 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: |
| | 8 | |
| | 9 | {{{#!sh |
| | 10 | module load matlab/r2023a |
| | 11 | }}} |
| | 12 | |
| | 13 | == When is GCC needed? == |
| | 14 | |
| | 15 | Load GCC when you are building C, C++, or Fortran code for use from MATLAB. This includes: |
| | 16 | |
| | 17 | * Running `mex ...` to build an extension. |
| | 18 | * Compiling a Simulink S-function. |
| | 19 | * Building MATLAB Coder generated native code. |
| | 20 | * Building code that lets MATLAB call an existing C/C++/Fortran library, hardware SDK, or vendor software. |
| | 21 | |
| | 22 | A '''MEX file''' is a compiled extension that lets MATLAB call C/C++/Fortran code. On Linux, its filename normally ends in `.mexa64`. |
| | 23 | |
| | 24 | == GCC modules for new native-code builds == |
| | 25 | |
| | 26 | ||= MATLAB module =||= GCC module to use for new MEX / S-function / native-code builds =|| |
| | 27 | || `matlab/r2017b` || `gcc/4.9.4` || |
| | 28 | || `matlab/r2020a` || `gcc/6.3.0` || |
| | 29 | || `matlab/r2022b` || `gcc/9.5.0` || |
| | 30 | || `matlab/r2023a` || `gcc/10.5.0` || |
| | 31 | |
| | 32 | == Supported compiler workflow == |
| | 33 | |
| | 34 | {{{#!comment |
| | 35 | Note to Editors: replace the first live sentence in the following with following. |
| | 36 | The `matlab/r2023a` module does '''not''' load or select a compiler. |
| | 37 | }}} |
| | 38 | |
| | 39 | 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: |
| | 40 | |
| | 41 | {{{#!sh |
| | 42 | module purge |
| | 43 | module load matlab/r2023a |
| | 44 | module load gcc/10.5.0 |
| | 45 | }}} |
| | 46 | |
| | 47 | Then start MATLAB and build the extension, for example: |
| | 48 | |
| | 49 | {{{#!matlab |
| | 50 | mex mycode.cpp |
| | 51 | }}} |
| | 52 | |
| | 53 | Confirm that GCC 10.5 is selected before starting MATLAB: |
| | 54 | |
| | 55 | {{{#!sh |
| | 56 | which gcc g++ |
| | 57 | gcc --version |
| | 58 | g++ --version |
| | 59 | }}} |
| | 60 | |
| | 61 | The compiler paths should be under: |
| | 62 | |
| | 63 | {{{ |
| | 64 | /share/apps/gcc/10.5.0/bin/ |
| | 65 | }}} |
| | 66 | |
| | 67 | == Why load GCC 10.5 explicitly? == |
| | 68 | |
| | 69 | 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. |
| | 70 | |
| | 71 | Loading `gcc/10.5.0` explicitly makes the compiler choice visible and prevents MATLAB from silently using the older system compiler. |
| | 72 | |
| | 73 | == Existing compiled extensions == |
| | 74 | |
| | 75 | 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. |
| | 76 | |
| | 77 | If you maintain an existing C++ extension, rebuild and test it with GCC 10.5 before distributing the rebuilt version. |
| | 78 | |
| | 79 | == MATLAB Compiler (`mcc`) == |
| | 80 | |
| | 81 | `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. |