Configure Code Generation and Build Settings - MATLAB & Simulink (original) (raw)

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 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. For details, see Generated Files and Locations. 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 Changing Names and Locations of Generated Files.

Specify Output Type and Language

To specify build type:

If you select build type Source Code in the MATLAB Coder app, the code generator does not invoke the make command and does not 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. You can also use this option if you want to compile the generated code with your own compiler. This option is equivalent toStatic Library with the Generate code only box selected. At the command line, you can instruct the code generator to only generate source code by using the -c option.

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 main functionSee Specifying main Functions for C/C++ Executables. 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.

Specify Build Configuration Parameters in the MATLAB Coder App

To access the project build settings, in the Generate Code dialog box, click More Settings. Alternatively, in theCheck for Run-Time Issues step, clickSettings. 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 betweenMEX Function and Source Code, 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, you must enable it again forStatic Library, Dynamic Library, and Executable.

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
coder.MexCodeConfig Specifies parameters for MEX code generation. cfg = coder.config("mex");
coder.CodeConfig 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");
coder.EmbeddedCodeConfig 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:

  1. In the MATLAB workspace, create a configuration object.
  2. Modify the parameters of the configuration object as required, using one of these methods:
  3. Call the codegen function with the-config option. Specify the configuration object as the config 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 the codegen 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. Use this syntax:

configurationobject.property = value

Dot notation uses assignment statements to modify configuration object properties. For example:

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 objectmexcfg in the MATLAB workspace. To save the configuration object, at the MATLAB prompt, enter:

Thesave command saves mexcfg to the filemexcfg.mat in the current folder.

To restore mexcfg in a new MATLAB session, at the MATLAB prompt, enter:

Theload command loads the objects defined inmexcfg.mat to the MATLAB workspace.

See Also

codegen | coder.MexCodeConfig | coder.CodeConfig | coder.EmbeddedCodeConfig

External Websites