Configure Code Generation and Build Settings
Configure MATLAB® Coder™ software by specifying the build type (MEX, lib, dll, or exe) and language (C or C++). Use additional settings to specify where the generated code should be built, apply target specific optimizations, enable variable-sizing support, include comments in the generated code, and apply other customizations. You can modify code generation and build settings at the command line or by using the MATLAB Coder app.
Build Type and Language
Available Build Types and Languages
The MATLAB Coder software can generate these types of builds:
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
C is the default language for code generation with MATLAB Coder software. To generate C++ code, you must specify the code generation language explicitly in the MATLAB Coder app or at the command line. The MATLAB Coder software automatically locates and uses a supported installed compiler. To change the default compiler, see Change Default Compiler.
By default, the folders in which the code generator generates code are determined by the output type. Each time the code generator generates the same type of output for the same entry-point function or MATLAB Coder project, it overwrites the files from the previous build. To preserve files from a build, copy them to a different location before starting another build. Alternatively, change the names or locations of the generated files. See Generated Files and Locations.
Specify Output Type and Language
To specify build type:
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
codegen
function with the-config
and-lang
options.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
codegen
with the-c
option.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.
The table shows how to generate various different types of C or C++ builds for
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
specifying the source file containing the |
codegen -config:exe main.cpp -lang:c++ foo |
Specify Additional Build Configuration Settings
Besides build type and language, you can specify additional build configuration settings in the MATLAB Coder app, at the command line, or using configuration object dialog boxes. Use the table to determine which specification method to use.
If you are using | Use | Details |
---|---|---|
The MATLAB Coder app | The project settings dialog box | Specify Build Configuration Parameters in the MATLAB Coder App |
The codegen function at the command line and want
to specify a few parameters | Configuration objects | Specify Build 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 | Configuration object dialog boxes | Specify Code Configuration Parameters Interactively |
Specify Build Configuration Parameters in the MATLAB Coder App
To access the project build settings, 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 Build Configuration Parameters at the Command Line Using Configuration Objects
Use configuration objects with the codegen
function to
customize your environment for code generation. The following 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"); |
Configuration object parameters are initialized to default values. To modify configuration objects to customize your environment for code generation:
In the MATLAB workspace, create a configuration object.
Modify the parameters of the configuration object as required, using one of these methods:
Dot notation at the command line. See Modifying Configuration Objects at the Command Line Using Dot Notation.
Dialog boxes. See Specify Code Configuration Parameters Interactively.
Call the
codegen
function with the-config
option. Specify the configuration object as theconfig
argument.The
-config
option instructscodegen
to generate code for the target MATLAB using the settings in the configuration object. For example, at the command line, create a static library configuration objectcfg
. Then, generate code for MATLABfoo
using thecodegen
command with this configuration object.cfg = coder.config("lib"); codegen -config cfg foo
Modifying Configuration Objects at the Command Line Using Dot Notation. You can use dot notation to modify the value of one configuration object
parameter 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
main
function 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
Saving 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 prompt,
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 prompt,
enter:
load mexcfg.mat
load
command loads the objects defined in
mexcfg.mat
to the MATLAB workspace.See Also
codegen
| coder.MexCodeConfig
| coder.CodeConfig
| coder.EmbeddedCodeConfig
Topics
- Understand and Control Partitioning of the Generated Code
- Specify Code Configuration Parameters Interactively
- Generating Standalone C/C++ Executables from MATLAB Code
- Overview of Code Generation Using MATLAB Coder