Configure Build Settings
Specify Build Type
Build Types
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
main
function, as described in Specifying main Functions for C/C++ Executables.
Location of Generated Files
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.
Specify the Build Type Using the MATLAB Coder App
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.
Certain configuration parameters are relevant for both MEX and standalone code
generation. If you enable any of these parameters
when the output type is MEX Function
, and you want to use
the same setting for C/C++ code generation as well, you must enable it again for
C/C++ Static Library
, C/C++ Dynamic
Library
, and C/C++ Executable
.
Specifying the Build Type at the Command Line
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 Specify Properties of Entry-Point Function Inputs.
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 Note You must specify a |
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 Note You must specify a |
Specify a Language for Code Generation
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.
Specify the Language Using the MATLAB Coder App
To open the Generate dialog box, on the Generate Code page, click the Generate arrow
.
Set Language to
C
orC++
.Note
If you specify
C++
, MATLAB Coder wraps theC
code into.cpp
files. You can use aC++
compiler and interface with externalC++
applications. MATLAB Coder does not generateC++
classes.
Specifying the Language Using the Command-Line Interface
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
C++
, MATLAB Coder wraps theC
code into.cpp
files. You can then use aC++
compiler and interface with externalC++
applications. MATLAB Coder does not generateC++
classes.
See Also
Specify Output File Name
Specify Output File Name Using the MATLAB Coder App
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 foldertarget
is:mex
for MEX functionslib
for static C/C++ librariesdll
for dynamic C/C++ librariesexe
for C/C++ executables
Command-Line Alternative
Use the codegen
function -o
option.
Specify Output File Locations
Specify Output File Location Using the MATLAB Coder App
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
, orExecutable
(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/fcn1fcn1
is the name of the alphabetically first entry-point file.target
is:mex
for MEX functionslib
for static C/C++ librariesdll
for dynamically linked C/C++ librariesexe
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/fcn1Set Build Folder to
Specified folder
. In the Build folder name field, provide the path to the folder.
Command-Line Alternative
Use the codegen
function -d
option.
Parameter Specification Methods
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
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.
Specify Build Configuration Parameters MATLAB Coder App
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
, orExecutable
(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
andSource Code
,Static Library
,Dynamic Library
, orExecutable
, verify these settings.Certain configuration parameters are relevant for both MEX and standalone code generation. If you enable any of these parameters when the output type is
MEX Function
, and you want to use the same setting for C/C++ code generation as well, you must enable it again forC/C++ Static Library
,C/C++ Dynamic Library
, andC/C++ Executable
.Modify the parameters as required. For more information about parameters on a tab, click Help.
Changes to the parameter settings take place immediately.
Specify Build Configuration Parameters at the Command Line Using Configuration Objects
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 instructscodegen
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, seecodegen
.
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'); Note If an Embedded Coder license is available, creates a If you use concurrent licenses, to disable the check out
of an Embedded Coder license, use one of the following
commands: cfg = coder.config('lib', 'ecoder', false) cfg = coder.config('dll', 'ecoder', false) cfg = coder.config('exe', 'ecoder', false) |
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'); Note Requires an Embedded Coder license; otherwise creates a |
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.
Specifying Build Configuration Parameters at the Command Line Using Dialog Boxes
After you have created a configuration object, you can modify the properties of the object by using the configuration parameter dialog box. See Specify Configuration Parameters in Command-Line Workflow Interactively.