Main Content

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 themainfunction, 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

  1. To open theGeneratedialog box, on theGenerate Codepage, click theGeneratearrow.

  2. 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 Librarywith 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 FunctionandSource,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

Callcodegenwith the-configoption. For example, suppose that you have a primary functionfoothat 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.cfile at the command line

codegen -config:exe main.c foo

Note

You must specify amainfunction for generating a C/C++ executable. SeeSpecifying main Functions for C/C++ Executables

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 amainfunction for generating a C/C++ executable. SeeSpecifying main Functions for C/C++ Executables

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

  1. To open theGeneratedialog box, on theGenerate Codepage, click theGeneratearrow.

  2. SetLanguagetoCorC++.

    Note

    If you specifyC++,MATLAB Coderwraps theCcode into.cppfiles. You can use aC++compiler and interface with externalC++applications.MATLAB Coder不产生C++classes.

Specifying the Language Using the Command-Line Interface

  1. Select a suitable compiler for your target language.

  2. Create a configuration object for code generation. For example, for a library:

    cfg = coder.config('lib');

  3. Set theTargetLangproperty to'C'or'C++'. For example:

    cfg.TargetLang = 'C++';

    Note

    If you specifyC++,MATLAB Coderwraps theCcode into.cppfiles. 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

  1. To open theGeneratedialog box, on theGenerate Codepage, click theGeneratearrow.

  2. 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:

  • fcn1for C/C++ libraries and executables.

  • fcn1_mexfor MEX functions.

By default,MATLAB Codergenerates files in the folderproject_folder/codegen/target/fcn1:

  • project_folderis your current project folder

  • targetis:

    • mexfor MEX functions

    • libfor static C/C++ libraries

    • dllfor dynamic C/C++ libraries

    • exefor C/C++ executables

Command-Line Alternative

Use thecodegenfunction-ooption.

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.

  1. To open theGeneratedialog box, on theGenerate Codepage, click theGeneratearrow.

  2. SetBuild typeto源代码,Static Library,Dynamic Library, orExecutable(depending on your requirements).

  3. ClickMore Settings.

  4. Click thePathstab.

    The default setting for theBuild folderfield isA subfolder of the project folder. By default,MATLAB Codergenerates files in the folderproject_folder/codegen/target/fcn1:

    • fcn1is the name of the alphabetically first entry-point file.

    • targetis:

      • mexfor MEX functions

      • libfor static C/C++ libraries

      • dllfor dynamically linked C/C++ libraries

      • exefor C/C++ executables

  5. To change the output location, you can either:

    • SetBuild FoldertoA subfolder of the current MATLAB working folder

      MATLAB Codergenerates files in theMATLAB_working_folder/codegen/target/fcn1folder

    • SetBuild FoldertoSpecified folder. In theBuild folder namefield, provide the path to the folder.

Command-Line Alternative

Use thecodegenfunction-doption.

Parameter Specification Methods

If you are using Use Details
TheMATLAB Coderapp The project settings dialog box. Specify Build Configuration Parameters MATLAB Coder App
codegenat the command line and want to specify a few parameters Configuration objects Specify Build Configuration Parameters at the Command Line Using Configuration Objects
codegenin build scripts
codegenat 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

You can specify build configuration parameters from theMATLAB Coderproject settings dialog box, the command line, or configuration object dialog boxes.

Specify Build Configuration ParametersMATLABCoderApp

  1. To open theGeneratedialog box, on theGenerate Codepage, click theGeneratearrow.

  2. SetBuild typeto源代码,Static Library,Dynamic Library, orExecutable(depending on your requirements).

  3. 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 betweenMEX Functionand源代码,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.

  4. 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.Thecodegenfunction uses configuration objects to customize your environment for code generation. The following table lists the available configuration objects.

Configuration Object Description

coder.CodeConfig

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.

coder.EmbeddedCodeConfig

If an Embedded Coder license is available, specifies parameters for C/C++ library or executable generation.

coder.HardwareImplementation

Specifies parameters of the target hardware implementation. If not specified,codegengenerates code that is compatible with the MATLAB host computer.

coder.MexCodeConfig

Specifies parameters for MEX code generation.

Working with Configuration Objects.To use configuration objects to customize your environment for code generation:

  1. 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.

  2. Modify the parameters of the configuration object as required, using one of these methods:

  3. Call thecodegenfunction with the-configoption. Specify the configuration object as its argument.

    The-configoption instructscodegento generate code for the target, based on the configuration property values. In the following example,codegengenerates 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-configoption 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 object

coder.MexCodeConfig
cfg = coder.config('mex');
Code generation configuration object for generating a standalone C/C++ library or executable

coder.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 acoder.EmbeddedCodeConfigobject.

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 target

coder.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 acoder.CodeConfigobject.

Hardware implementation configuration object

coder.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 amainfunction 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.