MATLAB® Coder™ can generate code for the following output types:
MEX function
Standalone C/C++ code
Standalone C/C++ code and compile it to a static library
Standalone C/C++ code and compile it to a dynamically linked library
Standalone C/C++ code and compile it to an executable
Note:
When you generate an executable, you must provide a C/C++ file
that contains the |
By default, MATLAB Coder generates files in output folders based on your output type. For more information, see Generated Files and Locations.
Note: Each time MATLAB Coder generates the same type of output for the same code or project, it removes the files from the previous build. If you want to preserve files from a build, copy them to a different location before starting another build. |
To open the Generate dialog
box, on the Generate Code page, click the Generate arrow
.
Set Build type to one of the following.
Source Code
MEX
Static Library
Dynamic Library
Executable
If you select Source Code
, MATLAB Coder does
not invoke the make command or generate compiled object code. 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. This option is equivalent to Static Library
with
the Generate code only box selected.
Code generation uses a different set of configuration parameters
for MEX functions than it uses for the other build types. . When
you switch the output type between MEX Function
and Source
, Static
Library
, Dynamic Library
,
or Executable
, verify these settings. For
more information, see Changing Output Type.
Call codegen
with the -config
option.
For example, suppose that you have a primary function foo
that
takes no input parameters. The following
table shows how to specify different output types when compiling foo
.
If a primary function has input parameters, you must specify these
inputs. For more information, see Primary Function Input Specification.
Note: C is the default language for code generation with MATLAB Coder. To generate C++ code, see Specify a Language for Code Generation. |
To Generate: | Use This Command: | |
---|---|---|
MEX function using the default code generation options | codegen foo | |
MEX function specifying code generation options | cfg = coder.config('mex'); % Set configuration parameters, for example, % enable a code generation report cfg.GenerateReport=true; % Call codegen, passing the configuration % object codegen -config cfg foo | |
Standalone C/C++ code and compile it to a library using the default code generation options | codegen -config:lib foo | |
Standalone C/C++ code and compile it to a library specifying code generation options | cfg = coder.config('lib'); % Set configuration parameters, for example, % enable a code generation report cfg.GenerateReport=true; % Call codegen, passing the configuration % object codegen -config cfg foo | |
Standalone C/C++ code and compile it to an executable using
the default code generation options and specifying the main.c file
at the command line | codegen -config:exe main.c foo
| |
Standalone C/C++ code and compile it to an executable specifying code generation options | cfg = coder.config('exe'); % Set configuration parameters, for example, % specify main file cfg.CustomSource = 'main.c'; cfg.CustomInclude = 'c:\myfiles'; codegen -config cfg foo
|
MATLAB Coder can generate C or C++ libraries and executables. C is the default language. You can specify a language explicitly from the project settings dialog box or at the command line.
To open the Generate dialog
box, on the Generate Code page, click the Generate arrow
.
Set Language to C
or C++
.
Note:
If you specify |
Select a suitable compiler for your target language.
Create a configuration object for code generation. For example, for a library:
cfg = coder.config('lib');
Set the TargetLang
property to 'C'
or 'C++'
.
For example:
cfg.TargetLang = 'C++';
Note:
If you specify |
See Also
To open the Generate dialog
box, on the Generate Code page, click the Generate arrow
.
In the Output file name field, enter the file name.
Note: Do not put spaces in the file name. |
By default, if the name of the first entry-point MATLAB file
is fcn1
, the output file name is:
fcn1
for C/C++ libraries
and executables.
fcn1_mex
for MEX functions.
By default, MATLAB Coder generates files in the folder
:project_folder
/codegen/target/fcn1
project_folder
is your
current project folder
target
is:
mex
for MEX functions
lib
for static C/C++ libraries
dll
for dynamic C/C++ libraries
exe
for C/C++ executables
Use the codegen
function -o
option.
The output file location must not contain:
Spaces (Spaces can lead to code generation failures in certain operating system configurations).
Tabs
\
, $
, #
, *
, ?
Non-7-bit ASCII characters, such as Japanese characters.
To open the Generate dialog
box, on the Generate Code page, click the Generate arrow
.
Set Build type to Source
Code
, Static Library
, Dynamic
Library
, or Executable
(depending
on your requirements).
Click More Settings.
Click the Paths tab.
The default setting for the Build folder field
is A subfolder of the project folder
. By
default, MATLAB Coder generates files in the folder
:project_folder
/codegen/target/fcn1
fcn1
is the name of the alphabetically
first entry-point file.
target
is:
mex
for MEX functions
lib
for static C/C++ libraries
dll
for dynamically linked C/C++
libraries
exe
for C/C++ executables
To change the output location, you can either:
Set Build Folder to A
subfolder of the current MATLAB working folder
MATLAB Coder generates files in the
folderMATLAB_working_folder
/codegen/target/fcn1
Set Build Folder to Specified
folder
. In the Build folder name field,
provide the path to the folder.
Use the codegen
function -d
option.
If you are using | Use | Details |
---|---|---|
The MATLAB Coder app | The project settings dialog box. | Specify Build Configuration Parameters MATLAB Coder App |
codegen at the command line and want to
specify a few parameters | Configuration objects | Specify Build Configuration Parameters at the Command Line Using Configuration Objects |
codegen in build scripts | ||
codegen at the command line and want to
specify many parameters | Configuration object dialog boxes | Specifying Build Configuration Parameters at the Command Line Using Dialog Boxes |
Specify Build Configuration Parameters at the Command Line Using Configuration Objects
Specifying Build Configuration Parameters at the Command Line Using Dialog Boxes
You can specify build configuration parameters from the MATLAB Coder project settings dialog box, the command line, or configuration object dialog boxes.
To open the Generate dialog
box, on the Generate Code page, click the Generate arrow
.
Set Build type to Source
Code
, Static Library
, Dynamic
Library
, or Executable
(depending
on your requirements).
Click More Settings.
The project 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 output
type between MEX Function
and Source
Code
, Static Library
, Dynamic
Library
, or Executable
, verify
these settings. See Changing Output Type.
Modify the parameters as required. For more information about parameters on a tab, click Help.
Changes to the parameter settings take place immediately.
Types of Configuration Objects. The codegen
function uses configuration
objects to customize your environment for code generation. The following
table lists the available configuration objects.
Configuration Object | Description |
---|---|
If no Embedded Coder® license is available or you disable use of the Embedded Coder license, specifies parameters for C/C++ library or executable generation. | |
If an Embedded Coder license is available, specifies parameters for C/C++ library or executable generation. | |
Specifies parameters of the target hardware implementation.
If not specified, | |
Specifies parameters for MEX code generation. |
Working with Configuration Objects. To use configuration objects to customize your environment for code generation:
In the MATLAB workspace, define configuration object variables, as described in Creating Configuration Objects.
For example, to generate a configuration object for C static library generation:
cfg = coder.config('lib'); % Returns a coder.CodeConfig object if no % Embedded Coder license available. % Otherwise, returns a coder.EmbeddedCodeConfig object.
Modify the parameters of the configuration object as required, using one of these methods:
Interactive commands, as described in Specify Build Configuration Parameters at the Command Line Using Configuration Objects
Dialog boxes, as described in Specifying Build Configuration Parameters at the Command Line Using Dialog Boxes
Call the codegen
function with
the -config
option. Specify the configuration object
as its argument.
The -config
option instructs codegen
to
generate code for the target, based on the configuration property
values. In the following example, codegen
generates
a C static library from a MATLAB function, foo
,
based on the parameters of a code generation configuration object, cfg
,
defined in the first step:
codegen -config cfg foo
The -config
option specifies the type of
output that you want to build — in this case, a C static library.
For more information, see codegen
.
Creating Configuration Objects. You can define a configuration object in the MATLAB workspace.
To Create... | Use a Command Such As... | ||
---|---|---|---|
MEX configuration objectcoder.MexCodeConfig | cfg = coder.config('mex'); | ||
Code generation configuration object
for generating a standalone C/C++ library or executablecoder.CodeConfig | % 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');
| ||
Code generation configuration object for generating a standalone
C/C++ library or executable for an embedded targetcoder.EmbeddedCodeConfig | % 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');
| ||
Hardware implementation configuration objectcoder.HardwareImplementation | hwcfg = coder.HardwareImplementation |
Each configuration object comes with a set of parameters, initialized to default values. You can change these settings, as described in Modifying Configuration Objects at the Command Line Using Dot Notation.
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. Use this syntax:
configuration_object.property = value
Dot notation uses assignment statements to modify configuration object properties:
To specify a main
function during
C/C++ code generation:
cfg = coder.config('exe'); cfg.CustomInclude = 'c:\myfiles'; cfg.CustomSource = 'main.c'; codegen -config cfg foo
To automatically generate and launch code generation reports after generating a C/C++ static library:
cfg = coder.config('lib'); cfg.GenerateReport= true; cfg.LaunchReport = true; codegen -config cfg foo
Saving Configuration Objects. Configuration objects do not automatically persist between MATLAB sessions. Use one of the following methods to preserve your settings:
Save a configuration object to a MAT-file and then load the
MAT-file at your next session
Write a script that creates the configuration object and sets
its properties.
Create a configuration object as described in Creating Configuration Objects.
For example, to create a coder.MexCodeConfig
configuration
object for MEX code generation:
mexcfg = coder.config('mex');
Open the property dialog box using one of these methods:
In the MATLAB workspace, double-click the configuration object variable.
At the MATLAB prompt, issue the open
command,
passing it the configuration object variable, as in this example:
open mexcfg
In the dialog box, modify configuration parameters as required, then click Apply.
Call the codegen
function with
the -config
option. Specify the configuration object
as its argument:
codegen -config mexcfg foo
The -config
option specifies the type of
output that you want to build. For more information, see codegen
.