coder.CodeConfig class

Package: coder

codegen configuration object

Description

A coder.CodeConfig object contains the configuration parameters that the codegen function requires to generate standalone C/C++ libraries and executables. Use the -config option to pass this object to the codegen function.

Construction

cfg = coder.config('lib') creates a code generation configuration object for C/C++ static library generation. If the Embedded Coder® product is not installed, it creates a coder.CodeConfig object. Otherwise, it creates a coder.EmbeddedCodeConfig object.

cfg = coder.config('dll') creates a code generation configuration object for C/C++ dynamic library generation. If the Embedded Coder product is not installed, it creates a coder.CodeConfig object. Otherwise, it creates a coder.EmbeddedCodeConfig object.

cfg = coder.config('exe') creates a code generation configuration object for C/C++ executable generation. If the Embedded Coder product is not installed, it creates a coder.CodeConfig object. Otherwise, it creates a coder.EmbeddedCodeConfig object.

cfg = coder.config(output_type, 'ecoder', false) creates a coder.CodeConfig object for the specified output type even if the Embedded Coder product is installed.

cfg = coder.config(output_type, 'ecoder', true) creates a coder.EmbeddedCodeConfig object for the specified output type even if the Embedded Coder product is not installed. However, you cannot generate code using a coder.EmbeddedCodeConfig object unless an Embedded Coder license is available.

Properties

BuildConfiguration

Specify build configuration. 'Faster Builds', 'Faster Runs', 'Debug', 'Specify'.

Default: 'Faster Builds'

CodeReplacementLibrary

Specify a code replacement library for the generated code.

ValueDescription
'None'

Does not use a code replacement library.

'GNU C99 extensions'

Generates calls to the GNU® gcc math library, which provides C99 extensions as defined by compiler option -std=gnu99.

'Intel IPP for x86-64 (Windows)'

Generates calls to the Intel® Performance Primitives (IPP) library for the x86-64 Windows® platform.

'Intel IPP/SSE for x86-64 (Windows)'

Generates calls to the IPP and Streaming SIMD Extensions (SSE) libraries for the x86-64 Windows platform.

'Intel IPP for x86-64 (Windows using MinGW compiler)'

Generates calls to the IPP library for the x86-64 Windows platform and MinGW compiler.

'Intel IPP/SSE for x86-64 (Windows using MinGW compiler)'

Generates calls to the IPP and SSE libraries for the x86-64 Windows platform and MinGW compiler.

'Intel IPP for x86/Pentium (Windows)'

Generates calls to the IPP library for the x86/Pentium Windows platform.

'Intel IPP/SSE x86/Pentium (Windows)'

Generates calls to the IPP and SSE libraries for the x86/Pentium Windows platform.

'Intel IPP for x86-64 (Linux)'

Generates calls to the IPP library for the x86-64 Linux® platform.

'Intel IPP/SSE with GNU99 extensions for x86-64 (Linux)'

Generates calls to the GNU libraries for IPP and SSE, with GNU C99 extensions, for the x86-64 Linux platform.

Compatible libraries depend on these parameters:

  • TargetLang

  • TargetLangStandard

  • ProdHWDeviceType in the hardware implementation configuration object.

Embedded Coder offers more libraries and the ability to create and use custom code replacement libraries.

MATLAB® Coder™ generates the minimal set of #include statements for header files required by the selected code replacement library.

Before setting this parameter, verify that your compiler supports the library that you want to use. If you select a parameter value that your compiler does not support, compiler errors can occur.

    Note:   MATLAB Coder software does not support TLC callbacks.

Default: 'None'

CompileTimeRecursionLimit

For compile-time recursion, control the number of copies of a function that are allowed in the generated code. To disallow recursion in the MATLAB code, set CompileTimeRecursionLimit to 0. The default compile-time recursion limit is high enough for most recursive functions that require this type of recursion. If code generation fails because of the compile-time recursion limit, and you want compile-time recursion, try to increase the limit. Alternatively, change your MATLAB code so that the code generator uses run-time recursion. See Compile-Time Recursion Limit Reached.

Default: integer, 50

ConstantFoldingTimeout

Specify the maximum number of instructions that the constant folder will execute before stopping. In some situations, code generation might require specific instructions to be constant. Increase this value if code generation is failing.

Default: integer, 10000

CustomHeaderCode

Specify code to appear near the top of each C/C++ header file generated from your MATLAB algorithm code. Specify code as a character vector.

Default: ''

CustomInclude

Specify a space-separated list of include folders to add to the include path when compiling the generated code. Specify list of include folders as a character vector.

If your list includes Windows paths that contain spaces, enclose each instance in double quotes, for example:

'C:\Project "C:\Custom Files"'

Default: ''

CustomInitializer

Specify code to appear in the initialize function of the generated .c or .cpp file. Specify code as a character vector.

Default: ''

CustomLAPACKCallback

Specify the name of a LAPACK callback class that derives from coder.LAPACKCallback. Specify name as a character vector. If you specify a LAPACK callback class, for certain linear algebra functions, the code generator produces LAPACK calls by using the LAPACKE C interface to your LAPACK library. The callback class provides the name of your LAPACKE header file and the information required to link to your LAPACK library. If this parameter is empty, the code generator produces code for linear algebra functions instead of a LAPACK call.

Default: ''

CustomLibrary

Specify a space-separated list of static library or object files to link with the generated code. Specify list as a character vector.

Default: ''

CustomSource

Specify a space-separated list of source files to compile and link with the generated code. Specify list of source files as a character vector.

Default: ''

CustomSourceCode

Specify code to appear near the top of the generated .c or .cpp file, outside of a function. Specify code as a character vector.

Default: ''

CustomTerminator

Specify code to appear in the terminate function of the generated .c or .cpp file. Specify code as a character vector.

Default: character vector, ''

CustomToolchainOptions

Specify custom settings for each tool in the selected toolchain, as a cell array.

Dependencies:

  • Toolchain determines which tools and options appear in the cell.

  • Setting BuildConfiguration to Specify enables the options specified by CustomToolchainOptions.

Start by getting the current options and values. For example:

rtwdemo_sil_topmodel;
set_param(gcs, 'BuildConfiguration', 'Specify')
opt = get_param(gcs, 'CustomToolchainOptions')

Then edit the values in opt.

These values derive from the toolchain definition file and the third-party compiler options. See Custom Toolchain Registration.

Default: cell array

DataTypeReplacement

Specify whether to use built-in C data types or pre-defined types from rtwtypes.h in generated code.

Set to 'CoderTypeDefs' to use the data types from rtwtypes.h. Otherwise, to use built-in C data types, retain default value or set to 'CBuiltIn'.

Default: 'CBuiltIn'

Description

Description of the coder.CodeConfig object specified as a character vector.

Default: 'class CodeConfig: C code generation configuration.'

DynamicMemoryAllocation

Control use of dynamic memory allocation for variable-size data.

By default, dynamic memory allocation is enabled for variable-size arrays whose size (in bytes) is greater than or equal to DynamicMemoryAllocationThreshold. codegen allocates memory for this variable-size data dynamically on the heap.

Set this property to 'Off' to allocate memory statically on the stack. Set it to 'AllVariableSizeArrays' to allocate memory for all variable-size arrays dynamically on the heap. You must use dynamic memory allocation for unbounded variable-size data.

Dependencies:

  • EnableVariableSizing enables this parameter.

  • Setting this parameter to 'Threshold' enables the DynamicMemoryAllocationThreshold parameter.

Default: 'Threshold'

DynamicMemoryAllocationThreshold

Specify the size threshold in bytes.codegen allocates memory on the heap for variable-size arrays whose size is greater than or equal to this threshold.

Dependency:

  • Setting DynamicMemoryAllocation to 'Threshold' enables this parameter.

Default: 65536

EnableAutoExtrinsicCalls

Specify whether MATLAB Coder must treat common visualization functions as extrinsic functions. When this option is enabled, MATLAB Coder detects calls to many common visualization functions, such as plot, disp, and figure. For MEX code generation, MATLAB Coder calls out to MATLAB for these functions. For standalone code generation, MATLAB Coder does not generate code for these visualization functions. This capability reduces the amount of time that you spend making your code suitable for code generation. It also removes the requirement to declare these functions extrinsic using the coder.extrinsic function.

Default: true

EnableMemcpy

Enable the memcpy optimization. To optimize code that copies consecutive array elements, the memcpy optimization replaces the code with a memcpy call. A memcpy call can be more efficient than code, such as a for-loop or multiple, consecutive element assignments. The code generatorinvokes the memcpy optimization when the following conditions are true:

  • EnableMemcpy is true.

  • The number of bytes to copy is greater than or equal to MemcpyThreshold. The number of bytes to copy is the number of array elements multiplied by the number of bytes required for the C/C++ data type.

See memcpy Optimization.

Default: true

EnableOpenMP

If possible, enable OpenMP. Using the OpenMP library, the C/C++ code that MATLAB Coder generates for parfor-loops can run on multiple threads. With OpenMP disabled, MATLAB Coder treats parfor-loops as for-loops and generates C/C++ code that runs on a single thread.

Default: true

EnableRuntimeRecursion

Allow recursive functions in generated code. If your MATLAB code requires run-time recursion and this parameter is false, code generation fails.

Some coding standards, such as MISRA®, do not allow recursion. To increase the likelihood of generating code that is compliant with MISRA C®, set EnableRuntimeRecursion to false.

Default: true

EnableVariableSizing

Enable support for variable-size arrays.

Dependency:

  • This parameter enables the parameter DynamicMemoryAllocation.

Default: true

FilePartitionMethod

Specify whether to generate one C/C++ file for each MATLAB language file ('MapMFileToCFile') or generate all C/C++ functions into a single file ('SingleFile').

Default: 'MapMFileToCFile'

GenCodeOnly

Specify code generation versus an executable or static library build.

Default: false

GenerateComments

Place comments in the generated files.

Dependency:

  • Enables MATLABSourceComments.

Default: true

GenerateExampleMain

Specify whether to generate an example C/C++ main function. If example main generation is enabled, MATLAB Coder generates source and header files for the main function in the examples subfolder of the build folder. For C code generation, it generates the files main.c and main.h. For C++ code generation, it generates the files main.cpp and main.h.

The example main function declares and initializes data. It calls entry-point functions but does not use values returned from the entry-point functions.

Before you use the example main files, copy them to another location and modify them to meet the requirements of your application.

ValueDescription
'DoNotGenerate'

Does not generate an example C/C++ main function.

'GenerateCodeOnly'

Generates an example C/C++ main function but does not compile it.

'GenerateCodeAndCompile'

Generates an example C/C++ main function and compiles it to create a test executable. This executable does not return output.

If the GenCodeOnly parameter is true, MATLAB Coder does not compile the C/C++ main function.

Default: 'GenerateCodeOnly'

GenerateMakefile

Specify whether to generate a makefile during the build process.

Default: true

GenerateReport

Document generated code in an HTML report.

Default: false

HardwareImplementation

Handle to coder.HardwareImplementation object that specifies hardware-specific configuration parameters for C/C++ code generation. Defaults to a coder.HardwareImplementation object with properties that are set for the MATLAB host computer.

InitFltsAndDblsToZero

Enable the memset optimization for assignment of float or double 0 to consecutive array elements. To optimize code that assigns a constant value to consecutive array elements, the memset optimization replaces the code with a memset call. A memset call can be more efficient than code, such as a for-loop or multiple, consecutive element assignments. To assign float or double 0 to consecutive array elements, the code generatorinvokes the memset optimization when the following conditions are true:

  • InitFltsAndDblsToZero is true.

  • The number of bytes to assign is greater than or equal to MemcpyThreshold. The number of bytes to assign is the number of array elements multiplied by the number of bytes required for the C/C++ data type.

See memset Optimization.

Default: true

InlineStackLimit

Specify the stack size limit on inlined functions. This specification determines the amount of stack space allocated for local variables of the inlined function.

Specifying a limit on the stack space constrains the amount of inlining allowed. For out-of-line functions, stack space for variables local to the function is released when the function returns. However, for inlined functions, stack space remains occupied by the local variables even when the function returns.

This feature is especially important for embedded processors, where stack size can be limited.

Default: 4000

InlineThreshold

Specify function size for inline threshold. Unless there are conflicts with other inlining conditions, MATLAB Coder inlines functions that are smaller than this size.

The function size is measured in terms of an abstract number of instructions, not actual MATLAB instructions or instructions in the target processor. You must experiment with this parameter to obtain the inlining behavior that you want. For instance, if the default setting for this parameter is leading to large functions being inlined and in turn generating large C code, you can tune the parameter in steps until you are satisfied with the size of generated code.

Default: 10

InlineThresholdMax

Specify the maximum size of functions after inlining. If the size of the calling function after inlining exceeds InlineThresholdMax,MATLAB Coder does not inline the called function.

The function size is measured in terms of an abstract number of instructions, not actual MATLAB instructions or instructions in the target processor. You must experiment with this parameter to obtain the inlining behavior that you want. For instance, if the default setting for this parameter is leading to large functions being inlined and in turn generating large C code, you can tune the parameter in steps until you are satisfied with the size of generated code.

Default: 200

LaunchReport

Specify whether to display a report after code generation is complete or an error occurs.

Default: true

MATLABSourceComments

Include MATLAB source code as comments in the generated code.

Dependency:

  • GenerateComments enables this parameter.

Default: false

MaxIdLength

Specify maximum number of characters in generated function, type definition, and variable names. To avoid truncation of identifiers by the target C compiler, specify a value that matches the maximum identifier length of the target C compiler.

This parameter does not apply to exported identifiers, such as the generated names for entry-point functions or emxArray API functions. If the length of an exported identifier exceeds the maximum identifier length of the target C compiler, the target C compiler truncates the exported identifier.

Minimum is 31. Maximum is 256.

Default: 31

MemcpyThreshold

Specify the minimum number of bytes for the code generatorto invoke the memcpy optimization or the memset optimization. To optimize generated code that copies consecutive array elements, the code generatortries to replace the code with a memcpy call. To optimize generated code that assigns a literal constant to consecutive array elements, the code generatortries to replace the code with a memset call. A memcpy or memset call can be more efficient than code, such as a for-loop or multiple, consecutive element assignments.

The number of bytes is the number of array elements to copy or assign multiplied by the number of bytes required for the C/C++ data type.

See memcpy Optimization and memset Optimization.

Default: 64

MultiInstanceCode

Generate reusable, multi-instance code that is reentrant.

Default: false

Name

Name of the configuration object.

Default: character vector, 'CodeConfig'

OutputType

Specify whether to generate a standalone C/C++ static library, dynamic library, or executable. Set to 'LIB' to generate a static library, 'DLL' to generate a dynamic library, or 'EXE' to generate an executable.

Default: 'LIB'

PassStructByReference

Specify whether to pass structures by reference to entry-point functions. Set to true to pass structures by reference, which reduces memory usage and execution time by minimizing the number of copies of parameters at entry-point function boundaries. Set to false to pass structures by value.

This parameter applies only to entry-point functions.

If you set this parameter to true, an entry-point function that writes to a field of a structure parameter overwrites the input value.

Default: true

PostCodeGenCommand

Specify command to customize build processing after code generation using codegen. Specify command as a character vector.

Default: ''

PreserveVariableNames

Specify which variable names the code generator must preserve in the generated code.

ValueDescription
'None'

The code generator does not have to preserve any variable names. It can reuse any variables that meet the requirements for variable reuse.

If your code uses large structures or arrays, setting PreserveVariableNames to 'None' can reduce memory usage or improve execution speed.

'UserNames'

The code generator preserves names that correspond to variables that you define in the MATLAB code. It does not replace your variable name with another name and does not use your name for another variable. To improve readability, set PreserveVariableNames to 'UserNames'. Then, you can more easily trace the variables in the generated code back to the variables in your MATLAB code.

Setting PreserveVariableNames to 'UserNames' does not prevent an optimization from removing your variables from the generated code or prevent the C/C++ compiler from reusing the variables in the generated binary code.

'All'

Preserve all variable names. This parameter value disables variable reuse. Use it only for testing or debugging, not for production code.

Default: 'None'

ReservedNameArray

Enter a space-separated list of names that MATLAB Coder is not to use for naming functions or variables. Specify list as a character vector.

Default: ''

RuntimeChecks

Enable run-time error detection and reporting in the generated C/C++ code. If you select this option, the generated code checks for errors such as out-of-bounds array indexing.

The error reporting software uses fprintf to write error messages to stderr. It uses abort to terminate the application. If fprintf and abort are not available, you must provide them. The abort function abruptly terminates the program. If your system supports signals, you can catch the abort signal (SIGABRT) so that you can control the program termination.

Error messages are in English.

Default: false

SaturateOnIntegerOverflow

Overflows saturate to either the minimum or maximum value that the data type can represent. Otherwise, the overflow behavior depends on your target C compiler. Most C compilers wrap on overflow.

This parameter applies only to MATLAB built-in integer types. It does not apply to doubles, singles, or fixed-point data types.

Default: true

StackUsageMax

Specify the maximum stack usage per application in bytes. Set a limit that is lower than the available stack size. Otherwise, a run-time stack overflow can occur. The C compiler detects and reports overflows.

Default: 200000

SupportNonFinite

Specify whether to generate nonfinite data and operations.

Default: true

TargetLang

Specify the target language. Set to 'C' to generate C code. Set to C++ to generate C++ code. If you specify C++, MATLAB Coder wraps the C code into .cpp files so that you can use a C++ compiler and interface with external C++ applications. It does not generate C++ classes.

Default: 'C'

TargetLangStandard

Specify a standard math library for the generated code. Options include 'C89/C90 (ANSI)', 'C99 (ISO)', and 'C++03 (ISO)'.

Before setting this parameter, verify that your compiler supports the library that you want to use. If you select a parameter value that your compiler does not support, compiler errors can occur.

Default: 'C89/C90 (ANSI)' for C, 'C++03 (ISO)' for C++.

Toolchain

Specify the toolchain to use. If you do not specify a toolchain, MATLAB Coder automatically locates an installed toolchain.

Default: 'Automatically locate an installed toolchain'

Verbose

Display code generation progress.

Default: false

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects in the MATLAB documentation.

Examples

Generate a standalone C/C++ static library from a MATLAB function that is suitable for code generation:

  1. Write a MATLAB function, coderand, that generates a random scalar value from the standard uniform distribution on the open interval (0,1).

    function r = coderand() %#codegen
    % The directive %#codegen declares that the function
    % is intended for code generation
    r = rand();

  2. Create a code generation configuration object to generate a static library.

    cfg = coder.config('lib')

  3. Generate the C library files in the default folder (codegen/lib/coderand). Use the -config option to specify the configuration object.

    codegen -config cfg coderand

Generate a C executable file from a MATLAB function that is suitable for code generation. Specify the main C function as a configuration parameter.

  1. Write a MATLAB function, coderand, that generates a random scalar value from the standard uniform distribution on the open interval (0,1).

    function r = coderand() %#codegen
    r = rand();

  2. Write a main C function, c:\myfiles\main.c, that calls coderand.

    /*
    ** main.c
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include "coderand.h"
    
    int main()
    {
        coderand_initialize();
        
        printf("coderand=%g\n", coderand());
        
        coderand_terminate();
        
        return 0;
    }

  3. Configure your code generation parameters to include the main C function, then generate the C executable.

    cfg = coder.config('exe');
    cfg.CustomSource = 'main.c';
    cfg.CustomInclude = 'c:\myfiles';
    codegen -config cfg coderand

    codegen generates C executables and supporting files in the default folder codegen/exe/coderand.

    This example shows how to specify a main function as a parameter in the configuration object coder.CodeConfig. Alternatively, you can specify the file containing main() separately on the command line. You can use a source, object, or library file.

Alternatives

Use the coder function to create a MATLAB Coder project. The project provides a user interface that facilitates adding MATLAB files, defining input parameters, and specifying build parameters.

Was this topic helpful?