Main Content

Use Generated Initialize and Terminate Functions

从MATLAB生成C/C ++代码时GydF4y2Ba®GydF4y2Ba代码,代码生成器会自动产生两个管家功能,并初始化和终止。初始化函数初始化生成的C/C ++入口点功能运行的状态。首次调用入口点功能之前,必须调用它。终止功能释放了分配的内存并执行其他清理操作。在您最后一次调用入口点功能后,必须调用它。GydF4y2Ba

Initialize Function

生成的初始化函数的名称GydF4y2Baprimary_function_nameGydF4y2Ba_initialize, 在哪里GydF4y2Baprimary_function_nameGydF4y2Bais the name of the first MATLAB entry-point function that you specify while generating code. The initialize function initializes the state on which the generated entry-point functions operate. The initialize function can include:

  • 调用支持代码的非限万博1manbetx时数据(GydF4y2BaInfand南GydF4y2Ba)。如果您的MATLAB代码包含可以生成非精彩值的操作,则会生成这些调用。GydF4y2Ba

  • 初始化的代码GydF4y2Ba全球的GydF4y2Baor执着的GydF4y2Ba变量。GydF4y2Ba

  • Custom code for creating an initial state that you specify. To include custom code in the initialize function, do one of the following:

    • 在一个code configuration object, set定制器GydF4y2Ba到包含自定义代码的字符向量。GydF4y2Ba

    • In theMATLAB CODER™GydF4y2Baapp, on the自定义代码GydF4y2Ba选项卡,为初始化函数指定自定义代码。GydF4y2Ba

In certain situations, no initialization code is necessary and the generated initialize function is empty.

调用初始化函数GydF4y2Ba

If you generate a MEX function, the generated code automatically includes a call to the initialize function. If you generate standalone code, there are two possible situations:

  • By default, if the initialize function is nonempty, the code generator includes a call to the initialize function at the beginning of the generated C/C++ entry-point functions. The generated code also includes checks to make sure that the initialize function is called automatically only once, even if there are multiple entry-point functions. In this situation, you do not need to manually call the initialize function.

    如果初始化函数为空,则生成的C/C ++入口点功能不包括对初始化函数的调用。GydF4y2Ba

  • You can choose to not include a call to the initialize function in the generated entry-point functions. Do one of the following:

    • 在一个GydF4y2Bacoder.CodeConfigorCoder.embeddedCodeConfigGydF4y2Ba对象,设置GydF4y2BaRunInitializeFcn至GydF4y2Ba错误的GydF4y2Ba。GydF4y2Ba

    • In theMATLAB编码器GydF4y2Baapp, on theAll Settings选项卡,设置GydF4y2Ba自动运行初始化函数GydF4y2Ba至GydF4y2BaNo。GydF4y2Ba

    如果做出此选择,则必须在首次调用生成的入口点功能之前手动调用初始化功能。不调用初始化函数会导致生成的入口点函数在无效状态下操作。GydF4y2Ba

If you generate C++ code with a class interface, then the code generator produces a class constructor and destructor that perform initialization and termination operations. You do not need to manually call the初始化GydF4y2Baand终止GydF4y2Ba功能。看GydF4y2BaGenerate C++ Code with Class Interface。GydF4y2Ba

生成初始化函数的示例GydF4y2Ba

Examples of MATLAB code patterns and the corresponding generated initialize functions:

  • 您的MATLAB代码使用GydF4y2Ba全球的GydF4y2Baor执着的GydF4y2Ba变量。例如,定义此MATLAB函数:GydF4y2Ba

    functiony = bar全球的GydF4y2Bag y = g;GydF4y2Ba结尾GydF4y2Ba

    生成一个静态库GydF4y2Ba酒吧GydF4y2Ba。指定初始值GydF4y2BaGGydF4y2Ba作为GydF4y2Ba1GydF4y2Ba。GydF4y2Ba

    代码根GydF4y2Ba-config:lib-globals{'g',1}酒吧GydF4y2Ba

    代码生成器生成文件GydF4y2Ba酒吧_initialize.c在GydF4y2Ba工作GydF4y2Ba\ codegen \ lib \ barGydF4y2Ba, 在哪里GydF4y2Ba工作GydF4y2Bais the folder that containsbar.mGydF4y2Ba。功能GydF4y2Ba酒吧_initialize初始化全局变量GydF4y2BaGGydF4y2Ba。GydF4y2Ba

    void bar_initialize(void){g = 1.0;isInitialized_bar = true;}GydF4y2Ba
    The generated C function酒吧GydF4y2Ba在cludes a call to酒吧_initialized。It uses the booleanisInitialized_bar为了确保初始化函数仅自动称为一次。GydF4y2Ba

    double bar(void){if(!isInitialized_bar){bar_initialize();}返回g;}GydF4y2Ba
  • 您的MATLAB代码包含一个可以生成非限值值的操作(GydF4y2BaInfor南GydF4y2Ba)。For example, define a MATLAB functionfooGydF4y2Bathat calls阶乘GydF4y2Ba。The阶乘GydF4y2Ba功能快速增长并返回GydF4y2BaInf对于大于一定阈值的输入。对于类型的输入GydF4y2Ba双倍的GydF4y2Ba,阈值是GydF4y2Ba170GydF4y2Ba。Executing阶乘(171)在Matlab返回中GydF4y2BaInf。GydF4y2Ba

    functiony = foo(a)y =阶乘(a);GydF4y2Ba结尾GydF4y2Ba

    生成一个静态库GydF4y2BafooGydF4y2Ba。GydF4y2Ba

    代码根GydF4y2Ba-config:libfooGydF4y2Ba-argsGydF4y2Ba{1}GydF4y2Ba

    代码生成器生成文件GydF4y2Bafoo_initialize.cGydF4y2Ba在GydF4y2Ba工作GydF4y2Ba\ codegen \ lib \ fooGydF4y2Ba, 在哪里GydF4y2Ba工作GydF4y2Bais the folder that containsfoo.mGydF4y2Ba。功能GydF4y2Bafoo_initialize调用支持代码万博1manbetx以获取无限数据,GydF4y2Bart_InitInfAndNaN,在另一个生成的文件中定义GydF4y2Bart_nonfinite.cGydF4y2Ba。GydF4y2Ba

    void foo_initialize(void){rt_initinfandnan();isInitialized_foo = true;}GydF4y2Ba

终止功能GydF4y2Ba

The name of the generated terminate function isprimary_function_nameGydF4y2Ba_端GydF4y2Ba, 在哪里GydF4y2Baprimary_function_nameGydF4y2Bais the name of the first MATLAB entry-point function that you specify while generating code. The terminate function frees allocated memory and performs other cleanup operations.

终止功能还可以包括您指定的自定义清理代码。要在终止函数中包括自定义代码,请执行以下操作之一:GydF4y2Ba

  • 在一个code configuration object, setCustomTerminator到包含自定义代码的字符向量。GydF4y2Ba

  • Alternatively, in theMATLAB编码器GydF4y2Baapp, on the自定义代码GydF4y2Ba选项卡,为终止功能指定自定义代码。GydF4y2Ba

If you generate a MEX function, the generated code automatically includes a call to the terminate function.

如果您生成独立代码,则生成的代码不会自动包含对终止函数的调用。在这种情况下,您必须在最后一次调用生成的入口点功能后手动调用终止功能。GydF4y2Ba

终止功能还用于清除持续变量的状态。持久变量保留其状态,直到调用终止功能为止。有关更多信息,请参阅GydF4y2BaGenerate Code for Persistent Variables。GydF4y2Ba

生成的终止功能的示例GydF4y2Ba

定义此MATLAB函数:GydF4y2Ba

functiony = bar全球的GydF4y2Bag y = g;GydF4y2Ba结尾GydF4y2Ba

生成一个静态库GydF4y2Ba酒吧GydF4y2Ba。指定初始值GydF4y2BaGGydF4y2Ba作为GydF4y2Ba1GydF4y2Ba。GydF4y2Ba

代码根GydF4y2Ba-config:lib-globals{'g',1}酒吧GydF4y2Ba

代码生成器生成文件GydF4y2Babar_terminate.CGydF4y2Ba在GydF4y2Ba工作GydF4y2Ba\ codegen \ lib \ barGydF4y2Ba, 在哪里GydF4y2Ba工作GydF4y2Bais the folder that containsbar.mGydF4y2Ba。功能GydF4y2Ba酒吧_端GydF4y2Ba设置布尔值GydF4y2BaisInitialized_bar(那是GydF4y2Batrue初始化函数调用之后)GydF4y2Ba错误的GydF4y2Ba。GydF4y2Ba

void bar_terminate(void) { isInitialized_bar = false; }

也可以看看GydF4y2Ba

|GydF4y2Ba|GydF4y2Ba

Related Topics