Configure Code Generation Settings
R2026bConfigure output of code generation by specifying the build type (MEX, LIB, DLL, or EXE) and language (C or C++). Use additional settings to specify the location of the generated code, apply target specific optimizations, enable variable-sizing support, include comments in the generated code, and apply other customizations. You can modify these settings at the command line or by using the MATLAB® Coder™ app.
Specify Target Language and Build Type
The code generator supports these target languages and build types:
C/C++ MEX function
Standalone C/C++ code, source code only
Standalone C/C++ code, compiled to a static library
Standalone C/C++ code, compiled to a dynamically linked library
Standalone C/C++ code, compiled to an executable
To specify the target language and build type, use one of these approaches:
In the MATLAB Coder tab of the toolstrip, click Language and select the output language. Click Output Type and select an output type.
At the command line, call the
codegenfunction with the-configand-langoptions.Set the configuration parameters Language and Build type.
To generate source code only, without invoking the make command
and without generating compiled object code, use one of these approaches:
In the MATLAB Coder tab of the toolstrip, click Generate Code.
At the command line, use
codegenwith the-coption.Set the configuration parameter Generate code only.
When you iterate between modifying MATLAB code and generating C/C++ code and you want to inspect the generated code, this option can save you time. You can also use this option if you want to compile the generated code with your own compiler.
This table shows how to generate various types of C or C++ builds for the MATLAB function foo by using the codegen
command.
| Build Type to Generate | Command |
|---|---|
| C MEX function |
codegen foo
|
| Standalone C++ code, compiled to a static library; generate source code only |
codegen -config:lib -lang:c++ -c foo |
| Standalone C code, compiled to a dynamically linked library |
codegen -config:dll foo |
Standalone C++ code, compiled to an executable and using the
source file |
codegen -config:exe main.cpp -lang:c++ foo |
Specify Additional Configuration Settings
Besides target language and build type, you can specify additional code configuration settings in the MATLAB Coder app, at the command line, or using configuration object dialog boxes. Use this table to determine which specification method to use.
| If you are using | Use | Details |
|---|---|---|
| The MATLAB Coder app | The code generation settings dialog box | Specify Code Configuration Parameters in the MATLAB Coder App |
The codegen function at the command line and want
to specify a few parameters | Configuration objects | Specify Code Configuration Parameters at the Command Line Using Configuration Objects |
The codegen function in build scripts | ||
The codegen function at the command line and want
to specify many parameters | The code generation settings dialog box | Specify Code Configuration Parameters Interactively |
Specify Code Configuration Parameters in the MATLAB Coder App
In the MATLAB Coder tab of the toolstrip, click Settings to open the Code Generation Settings dialog box. The Code Generation Settings dialog box provides the set of configuration parameters applicable to the output type that you select. Code generation uses a different set of configuration parameters for MEX functions than it uses for the other build types. When you switch the build types, verify these settings.
Certain configuration parameters are used for both MEX and standalone code generation. If you change any of these parameters when the build type is MEX, and you want to use the same setting for standalone code generation, you must set the parameter again.
Changes to parameter settings are effective immediately.
Specify Code Configuration Parameters at the Command Line Using Configuration Objects
To customize the code generation process. use configuration objects with the
codegen command. This table lists the available configuration
objects and example commands that you can use to create the configuration
objects.
| Configuration Object | Description | Example Creation Commands |
|---|---|---|
Specifies parameters for MEX code generation. |
cfg = coder.config("mex"); | |
If no Embedded Coder® license is available or you disable the Embedded Coder license, specifies parameters for C/C++ library or executable generation. |
% To generate a static library cfg = coder.config("lib"); % To generate a dynamic library cfg = coder.config("dll") % To generate an executable cfg = coder.config("exe"); | |
If an Embedded Coder license is available, specifies parameters for C/C++ library or executable generation. |
% To generate a static library cfg = coder.config("lib"); % To generate a dynamic library cfg = coder.config("dll") % To generate an executable cfg = coder.config("exe"); |
When you create a configuration object, the properties are initialized to their default values. To modify configuration objects to customize your environment for code generation:
In the MATLAB workspace, create a configuration object.
Modify the property of the configuration object as required, using one of these methods:
Dot notation at the command line. See Modify Configuration Objects at the Command Line Using Dot Notation.
Dialog boxes. See Specify Code Configuration Parameters Interactively.
Call the
codegencommand with the-configoption. Specify the configuration object as theconfigargument.The
-configoption instructscodegento generate code for the entry points using the settings in the configuration object. For example, at the command line, create a static library configuration objectcfg. Then, generate code for the MATLAB functionfoousing thecodegencommand with this configuration object.cfg = coder.config("lib"); codegen -config cfg foo
Modify Configuration Objects at the Command Line Using Dot Notation. You can use the dot notation to modify the value of one configuration object
property at a time. The dot notation syntax uses the object
variable and the property name connected with a dot (.):
object.PropertyName = value
For example:
Specify a
mainfunction for C code generation.cfg = coder.config("exe"); cfg.TargetLang = "C"; cfg.CustomInclude = "c:\myfiles"; cfg.CustomSource = "main.c"; codegen -config cfg foo
Automatically generate and launch code generation reports after generating a C++ static library.
cfg = coder.config("lib"); cfg.TargetLang = "C++"; cfg.GenerateReport= true; cfg.LaunchReport = true; codegen -config cfg foo
Save Configuration Objects
Configuration objects do not automatically persist between MATLAB sessions. To preserve your settings, write a script to recreate the configuration object or save the configuration object to a MAT file.
For example, assume that you create and customize a MEX configuration object
mexcfg in the MATLAB workspace. To save the configuration object, at the MATLAB command line,
enter:
save mexcfg.mat mexcfg
save command saves mexcfg to the file
mexcfg.mat in the current folder.To restore mexcfg in a new MATLAB session, at the MATLAB command line,
enter:
load mexcfg.matload command loads the objects defined in
mexcfg.mat to the MATLAB workspace.Share Configuration Settings
To share configuration settings among multiple projects or between the MATLAB
Coder app and the command line, you can export the settings to and import the
settings from a code generation configuration object. You can use a code configuration
object when you generate code at the command line by passing the code configuration
object to the codegen command with the
-config option.
Export App Settings to Configuration Object
To export app project settings to a code configuration object, use one of these approaches:
On the MATLAB Coder app toolstrip, click the Export button and select Export Code Generation Settings. Enter a name for the created configuration object.
At the command line, use the
-toconfigoption with thecodercommand. If the project file is not in the working directory, you must specify the file path.
The type of configuration object that the app creates depends on the build type of your MATLAB Coder project and on the configuration parameter Use Embedded Coder features.
| Build Type Setting | Parameter Setting | Created Configuration Object |
|---|---|---|
| MEX | Any | coder.MexCodeConfig |
| Standalone (static library, dynamic library, or executable) | Use Embedded Coder features check box selected | coder.EmbeddedCodeConfig |
| Standalone (static library, dynamic library, or executable) | Use Embedded Coder features check box cleared | coder.CodeConfig |
Import Configuration Object Settings into App
To import the settings from a code generation configuration object into the MATLAB Coder app, the configuration object must be stored in the base workspace. On the MATLAB Coder toolstrip, click Settings > Import from workspace variable. In the Import Code Generation Settings dialog box, select the configuration object.
See Also
coder | coder.config | codegen | coder.MexCodeConfig | coder.CodeConfig | coder.EmbeddedCodeConfig
Topics
- Overview of Code Generation Using MATLAB Coder
- Specify Code Configuration Parameters Interactively
- Convert MATLAB Coder Project to MATLAB Script
- Convert codegen Command to Equivalent MATLAB Coder Project