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.EmbeddedCodeConfigobject, you setIncludeInitializeFcntofalse.Alternatively, in the MATLAB Coder™ app, you clear the Initialize function required checkbox.
Also, you ensure that the custom code to appear in the generated initialize function is empty:
In a
coder.EmbeddedCodeConfigobject, you setCustomInitializerto''.Alternatively, in the MATLAB Coder app, on the Custom Code tab, you leave the Initialize function parameter 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.EmbeddedCodeConfigobject, setIncludeInitializeFcntotrue(this value is the default value).In the MATLAB Coder app, select the Initialize Function Required checkbox (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 (
InforNaN). For example, define a MATLAB functionfoothat callsfactorial. Thefactorialfunction grows quickly and returnsInffor inputs greater than a certain threshold. For an input of typedouble, the threshold is170. Executingfactorial(171)in MATLAB returnsInf.function y = foo(a) y = factorial(a); end
Generate a static library for
foo. In thecoder.EmbeddedCodeConfigobjectcfg, set the parameterCustomInitializerto an empty character array. Set the parameterIncludeInitializeFcntotrue. 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_initializethat calls supporting code for nonfinite data. This function callsrt_InitInfAndNaN, which is defined in another generated filert_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
globalorpersistentvariables. For example, define this MATLAB function:function y = bar global g y = g; end
Generate a static library for
bar. Specify the initial value ofgas1. In thecoder.EmbeddedCodeConfigobjectcfg, set the parameterCustomInitializerto an empty character array. Set the parameterIncludeInitializeFcntotrue. 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_initializethat initializesg./* 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] */