Main Content

MATLAB Code Patterns That Require a Nonempty Initialize Function in Generated Code

Issue

When generating standalone code from MATLAB® code, you choose not to generate the initialize function:

  • In a coder.EmbeddedCodeConfig object, IncludeInitializeFcn is set to false.

  • Alternatively, in the MATLAB Coder™ app, on the All Settings tab, Initialize Function Required is set to No.

Also, you ensure that the custom code to appear in the generated initialize function is empty:

  • In a coder.EmbeddedCodeConfig object, CustomInitializer is set to ''.

  • Alternatively, in the MATLAB Coder app, on the Custom Code tab, Custom C Code for Generated Files for initialize function is empty.

You still get this error message:

The initialize function is not empty, but the IncludeInitializeFcn configuration setting requests that
the initialize function be omitted.

Solution

In certain situations, the code generator determines that the initialize function must be nonempty and produces code for the initialize function. This code generation can occur even if the custom code for the initialize function that you specify is empty. Not including this generated code in the initialize function causes the generated entry-point functions to operate on an invalid state. In such situations, if you choose to not generate the initialize function, the code generator produces an error message.

To fix this issue, include the initialize function in your generated code by doing one of the following:

  • In a coder.EmbeddedCodeConfig object, set IncludeInitializeFcn to true (this value is the default value).

  • In the MATLAB Coder app, on the All Settings tab, set Initialize Function Required to Yes (this setting is the default setting).

Examples of MATLAB code patterns that require a nonempty initialize function in the generated code are:

  • Your MATLAB code contains an operation that can generate nonfinite values (Inf or NaN). For example, define a MATLAB function foo that calls factorial. The factorial function grows quickly and returns Inf for inputs greater than a certain threshold. For an input of type double, the threshold is 170. Executing factorial(171) in MATLAB returns Inf.

    function y = foo(a)
    y = factorial(a);
    end

    Generate a static library for foo. In the coder.EmbeddedCodeConfig object cfg, set the parameter CustomInitializer to an empty character array. Set the parameter IncludeInitializeFcn to true. These values are the default values for the parameters.

    cfg = coder.config('lib');
    
    cfg.CustomInitializer = '';  %These are the default values
    cfg.IncludeInitializeFcn = true;
    
    codegen -config cfg foo -args {1}

    The code generator automatically produces a nonempty initialize function foo_initialize that calls supporting code for nonfinite data. This function calls rt_InitInfAndNaN, which is defined in another generated file rt_nonfinite.c.

    /* Include Files */
    #include "foo_initialize.h"
    #include "foo.h"
    #include "foo_data.h"
    #include "rt_nonfinite.h"
    
    /* Function Definitions */
    
    /*
     * Arguments    : void
     * Return Type  : void
     */
    void foo_initialize(void)
    {
      rt_InitInfAndNaN();
      isInitialized_foo = true;
    }
    
    /*
     * File trailer for foo_initialize.c
     *
     * [EOF]
     */

  • Your MATLAB code uses global or persistent variables. For example, define this MATLAB function:

    function y = bar
    global g
    y = g;
    end

    Generate a static library for bar. Specify the initial value of g as 1. In the coder.EmbeddedCodeConfig object cfg, set the parameter CustomInitializer to an empty character array. Set the parameter IncludeInitializeFcn to true. These values are the default values for the parameters.

    cfg = coder.config('lib');
    
    cfg.CustomInitializer = '';  %These are the default values
    cfg.IncludeInitializeFcn = true;
    
    codegen -config cfg -globals {'g',1} bar

    The code generator automatically produces a nonempty initialize function bar_initialize that initializes g.

    /* Include Files */
    #include "bar_initialize.h"
    #include "bar.h"
    #include "bar_data.h"
    
    /* Function Definitions */
    
    /*
     * Arguments    : void
     * Return Type  : void
     */
    void bar_initialize(void)
    {
      g = 1.0;
      isInitialized_bar = true;
    }
    
    /*
     * File trailer for bar_initialize.c
     *
     * [EOF]
     */

See Also

Related Topics