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 inSpecifying main Functions for C/C++ Executables.
Location of Generated Files
By default,MATLAB Codergenerates files in output folders based on your output type. For more information, seeGenerated Files and Locations.
Note
Each timeMATLAB Codergenerates 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 theMATLABCoderApp
To open theGeneratedialog box, on theGenerate Codepage, click theGeneratearrow.
SetBuild typeto one of the following.
源代码
MEX
Static Library
Dynamic Library
Executable
If you select源代码
,MATLAB Coderdoes 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 toStatic Library
with theGenerate code onlybox 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 betweenMEX Function
andSource
,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 isMEX 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
.
Specifying the Build Type at the Command Line
Callcodegen
with the-config
option. For example, suppose that you have a primary functionfoo
that takes no input parameters. The following table shows how to specify different output types when compilingfoo
. If a primary function has input parameters, you must specify these inputs. For more information, seeSpecify Properties of Entry-Point Function Inputs.
Note
C is the default language for code generation withMATLAB Coder. To generate C++ code, seeSpecify 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 themain.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 Codercan 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 theMATLABCoderApp
To open theGeneratedialog box, on theGenerate Codepage, click theGeneratearrow.
SetLanguageto
C
orC++
.Note
If you specify
C++
,MATLAB Coderwraps theC
code into.cpp
files. You can use aC++
compiler and interface with externalC++
applications.MATLAB Coder不产生C++
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 Coderwraps theC
code into.cpp
files. You can then use aC++
compiler and interface with externalC++
applications.MATLAB Coder不产生C++
classes.
See Also
Specify Output File Name
Specify Output File Name Using theMATLABCoderApp
To open theGeneratedialog box, on theGenerate Codepage, click theGeneratearrow.
In theOutput file namefield, 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 isfcn1
, the output file name is:
fcn1
for C/C++ libraries and executables.fcn1_mex
for MEX functions.
By default,MATLAB Codergenerates 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 thecodegen
function-o
option.
Specify Output File Locations
Specify Output File Location Using theMATLABCoderApp
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 theGeneratedialog box, on theGenerate Codepage, click theGeneratearrow.
SetBuild typeto
源代码
,Static Library
,Dynamic Library
, orExecutable
(depending on your requirements).ClickMore Settings.
Click thePathstab.
The default setting for theBuild folderfield is
A subfolder of the project folder
. By default,MATLAB Codergenerates 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:
SetBuild Folderto
A subfolder of the current MATLAB working folder
MATLAB Codergenerates files in the
folderMATLAB_working_folder
/codegen/target/fcn1SetBuild Folderto
Specified folder
. In theBuild folder namefield, provide the path to the folder.
Command-Line Alternative
Use thecodegen
function-d
option.
Parameter Specification Methods
If you are using | Use | Details |
---|---|---|
TheMATLAB Coderapp | 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 theMATLAB Coderproject settings dialog box, the command line, or configuration object dialog boxes.
Specify Build Configuration ParametersMATLABCoderApp
To open theGeneratedialog box, on theGenerate Codepage, click theGeneratearrow.
SetBuild typeto
源代码
,Static Library
,Dynamic Library
, orExecutable
(depending on your requirements).ClickMore Settings.
“项目设置”对话框中提供了一组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源代码
,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, clickHelp.
Changes to the parameter settings take place immediately.
Specify Build Configuration Parameters at the Command Line Using Configuration Objects
Types of Configuration Objects.Thecodegen
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 inCreating 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 inSpecify Build Configuration Parameters at the Command Line Using Configuration Objects
Dialog boxes, as described inSpecifying 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 inModifying 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.配置对象不会自动保存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. SeeSpecify Configuration Parameters in Command-Line Workflow Interactively.