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

  1. To open the Generate dialog box, on the Generate Code page, click the Generate arrow .

  2. 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 main function for generating a C/C++ executable. See Specifying 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 a main function for generating a C/C++ executable. See Specifying main Functions for C/C++ Executables

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

  1. To open the Generate dialog box, on the Generate Code page, click the Generate arrow .

  2. Set Language to C or C++.

    Note

    If you specify C++, MATLAB Coder wraps the C code into .cpp files. You can use a C++ compiler and interface with external C++ applications. MATLAB Coder does not generate 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 the TargetLang property to 'C' or 'C++'. For example:

    cfg.TargetLang = 'C++';

    Note

    If you specify C++, MATLAB Coder wraps the C code into .cpp files. You can then use a C++ compiler and interface with external C++ applications. MATLAB Coder does not generate C++ classes.

See Also

Specify Output File Name

Specify Output File Name Using the MATLAB Coder App

  1. To open the Generate dialog box, on the Generate Code page, click the Generate arrow .

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

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.

  1. To open the Generate dialog box, on the Generate Code page, click the Generate arrow .

  2. Set Build type to Source Code, Static Library, Dynamic Library, or Executable (depending on your requirements).

  3. Click More Settings.

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

  5. 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 MATLAB_working_folder/codegen/target/fcn1 folder

    • Set 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 usingUseDetails
The MATLAB Coder appThe project settings dialog box. Specify Build Configuration Parameters MATLAB Coder App
codegen at the command line and want to specify a few parametersConfiguration objectsSpecify Build Configuration Parameters at the Command Line Using Configuration Objects
codegen in build scripts
codegen at the command line and want to specify many parametersConfiguration object dialog boxesSpecifying Build Configuration Parameters at the Command Line Using Dialog Boxes

Specify Build Configuration Parameters

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

  1. To open the Generate dialog box, on the Generate Code page, click the Generate arrow .

  2. Set Build type to Source Code, Static Library, Dynamic Library, or Executable (depending on your requirements).

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

    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.

  4. 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 ObjectDescription

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, codegen generates 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 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.

  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 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 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 a coder.EmbeddedCodeConfig object.

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 a coder.CodeConfig object.

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

  1. 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');

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

  3. In the dialog box, modify configuration parameters as required, then click Apply.

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

The property dialog box is not supported in MATLAB Online™.