Main Content

Generating Code

Generate code and build standalone libraries and executables

C/C++ code generation is the core functionality of MATLAB® Coder™. Generate code from the app, which you can open with coder, or from the command line, by using the codegen function. The app provides step-by-step guidance through the code generation process, including guidance for best practices, and clear visual aids for the code generation settings and options. The command line workflow enables simple, repeatable, and efficient initiation of code generation. The code generation workflow supports many different data types, including variable-size data and global data. The workflow also supports multiple entry-point (top-level) functions, multisignature MEX (polymorphic MEX) function generation, and customization of fundamental properties such as the array layout and array indexing of the generated code. In addition, you can generate code that uses key C++ language features such as classes, namespaces, and function overloading.


expand all

MATLAB CoderGenerate C code or MEX function from MATLAB code


expand all

coderOpen MATLAB Coder app
codegenGenerate C/C++ code from MATLAB code
coder.targetDetermine if code generation target is specified target
coder.allowpcodeControl code generation from P-code files
coder.configCreate MATLAB Coder code generation configuration objects
coder.extrinsicDeclare function as extrinsic and execute it in MATLAB
coder.loadLoad compile-time constants from MAT-file or ASCII file
coder.nullcopyDeclare uninitialized variables in code generation
RTW.reservedIdentifiersGet reserved identifiers for code generation (Since R2023b)
coder.newtypeCreate coder.Type object to represent type of an entry-point function input
coder.varsizeDeclare variable-size data
coder.typeofCreate coder.Type object to represent the type of an entry-point function input
coder.resizeResize coder.Type object
coder.cevalCall C/C++ function from generated code
coder.cincludeInclude header file in generated code
coder.cstructnameName C structure type in generated code
coder.opaqueDeclare variable in generated code
coder.refIndicate data to pass by reference
coder.rrefIndicate read-only data to pass by reference
coder.wrefIndicate write-only data to pass by reference


expand all

coder.MexCodeConfigConfiguration parameters for MEX function generation from MATLAB code
coder.CodeConfigConfiguration parameters for C/C++ code generation from MATLAB code
coder.EmbeddedCodeConfigConfiguration parameters for C/C++ code generation from MATLAB code with Embedded Coder
coder.OutputTypeOutput type from an entry-point function to specify as an input type


Generating Code Basics

Switch Between Command Line and Project Workflows

Data-specific Workflows

Extended Functionality

Extended C++ Functionality

Code Appearance

  • Reserved Keywords
    Certain words in your code, that are C/C++ keywords, might be renamed in the generated code.