Main Content

Macro Definitions (#define)

C Construct

#define myParam 9.8;

Export Generated Macro Definition

1. Open example modelex_param_macro.

2. In the model, select the Gain block. In the Property Inspector, set the value of theGainparameter tomyParam.

3. Next to the parameter value, click the action button (the button with three vertical dots) and selectCreate.

4.In the Create New Data dialog box, setValuetoSimulink.Parameter(9.8). ClickCreate.A Simulink.Parameterobject,myParam, appears in the base workspace. The Gain block uses the object to set the value of the Gain parameter, in this case,9.8.

5. On theCode Generationtab, click theConfigure in Coder Appbutton. In the Code Mappings editor, setStorage Classto定义. ClickOK.

6. To build the model and generate code, pressCtrl+B.

The generated header fileex_param_macro.h定义了myParamas a macro.

/* Definition for custom storage class: Define */ #define myParam 9.8 /* Referenced by: '/Gain' */

Reuse Macro from Handwritten Code

1. In the Code Mappings editor, on theParameterstab, click theUpdate Code Mappingsbutton.

2. ChangeStorage ClassofmyParamfrom定义toImportedDefine.

3. In the Property Inspector, underCodesection, setHeader Filetoexternal_params.h. The generated code imports the macro definition from a custom header file namedexternal_params.h.

4.In your current folder, create the C header fileexternal_params.h, which contains the#definestatement.

#ifndef _EXTERNAL_PARAMS #define _EXTERNAL_PARAMS #define myParam 9.8 #endif /* EOF */

5. To build the model and generate code, pressCtrl+B.

The generated header fileex_param_macro.hdoes not define the macro. Instead, the file includes(#include)the custom header fileexternal_params.h.

/* Includes for objects with custom storage classes */ #include "external_params.h"

The source fileex_param_macro.ccontains a guard to check that a definition formyParamexists.

#include "rtwtypes.h" #include "external_params.h" /* * Check that imported macros with storage class "ImportedDefine" are defined */ #ifndef myParam #error The variable for the parameter "myParam" is not defined #endif

Related Topics