Main Content

Generate C/C++ Code From MATLAB Code Using MATLAB Coder

You can use the MATLAB® Coder™ software to generate C/C++ code from your MATLAB code as source code, a static library, a dynamic linked library, or an executable. The generated code can be embedded in applications outside the MATLAB environment.

You can also use MATLAB Coder to generate a C/C++ MATLAB executable (MEX) function designed for MATLAB acceleration. Because C/C++ can be faster than MATLAB code, you can generate and use accelerated MEX functions in the MATLAB environment to increase the speed of your MATLAB code.

Generating working, optimized C/C++ code from MATLAB code can require multiple cycles of troubleshooting and code generation.

Writing MATLAB Code for Code Generation

When generating C/C++ code, the code generator converts dynamically typed MATLAB code to statically typed C/C++ code. In dynamically typed languages, the class and size of a variable can change at run time. Because C and C++ are statically typed languages, the code generator must determine variable types when you generate code. Many of the errors that you encounter during code generation occur when the code generator tries to convert dynamically typed MATLAB variables to statically typed C/C++ variables. You can avoid the most common code generation errors by following these best practices when you write MATLAB code for code generation:

  • Define the class and size of all variables before using them.

  • Preallocate arrays before indexing.

  • Verify code generation support for MATLAB functions and language features.

For additional details of these best practices, see Best Practices for Defining Variables for C/C++ Code Generation, Data Definition Considerations for Code Generation, and Functions and Objects Supported for C/C++ Code Generation.

Code Generation Workflow

Before you begin, install a supported compiler. For an up-to-date list of supported compilers, see Supported and Compatible Compilers.

When generating code, follow these steps:

  1. Identify Entry-Point Functions and Check MATLAB Code — Prepare for code generation by selecting one or more entry-point functions and checking if your MATLAB code is ready for code generation.

  2. Specify Types of Entry-Point Function Inputs — If your entry-point functions have inputs, specify the class and size of these inputs. You can specify input types in the MATLAB Coder app, at the command line, or in your entry-point functions.

  3. Check for Issues in MATLAB Code Using MEX Functions — Before you generate standalone C/C++ code or an accelerated MEX function, check for issues in your MATLAB code. This step enables you to detect and fix issues that are much harder to detect in the generated code.

  4. Optimize Generated C/C++ and MEX Code — Optionally, you can optimize the execution speed or memory usage of the generated code. After implementing an optimization strategy, re-check for issues in your MATLAB Coder code.

  5. Generate C/C++ Code — Generate standalone C/C++ code or an accelerated MEX function.

See Also

Related Topics

External Websites