Main Content

Removal of Unused Class Properties in Generated C/C++ Code

C/C++ code generated by Embedded Coder®contains unused class properties or structure fields. Removing unused properties or fields helps improve the memory footprint and run time of the code. By default, the code generator removes properties or fields that are unused in the generated C/C++ code.

This feature applies to standalone code generation. You can preserve the unused properties or fields in generated C/C++ code by using theMATLAB®Coder™app or at the command line.

Preserve Unused Class Properties or Structure Fields by usingMATLABCoder应用程序

To preserve unused properties or fields:

  1. Open theMATLAB Coder应用程序。

  2. On theGenerate Codepage, clickMore Settings.

  3. On theMemorytab, select thePreserve unused fields and propertiescheck box.

MATLAB Coder GUI to Preserve Unused Fields and Properties

Preserve Unused Class Properties or Structure Fields at the Command Line

To preserve the unused properties or fields, set the configuration parameterPreserveUnusedStructFieldstotrueand use the configuration objectcfgin thecodegencommand.

cfg = coder.config('lib'); cfg.PreserveUnusedStructFields = true;

Examples

The following examples demonstrate the usage of thePreserveUnusedStructFieldsconfiguration option to generate C/C++ code.

Generate C++ Code for MATLAB Classes

This table compares the generated C++ code formyClass, with unused properties removed and with unused properties preserved.

MATLAB Code

Unused Class Properties Removed (default)

Unused Class Properties Preserved

% Entry-point functionfunctiony = myAdd(n)%#codegeno = myClass(n); y = o.a + o.b;end
classdefmyClasspropertiesa b cendmethodsfunctionobj = myClass(x) coder.inline('never'obj。= x;obj。b = x + 1;obj.c = x + 2;endendend

Code generation command

cfg = coder.config('lib'); cfg.TargetLang ="C++"; codegenmyAdd-args{5}-configcfg-report

Generated C++ Code

class myClass { public: void init(double x); double a; double b; }; void myClass::init(double x) { a = x; b = x + 1.0; }

Code generation command

cfg = coder.config('lib'); cfg.PreserveUnusedStructFields = true; cfg.TargetLang ="C++"; codegenmyAdd-args{5}-configcfg-report

Generated C++ Code

class myClass { public: void init(double x); double a; double b; double c; }; void myClass::init(double x) { a = x; b = x + 1.0; c = x + 2.0; }

Generate C Code for MATLAB Structures

This table compares the generated C code formyStruct, with unused fields removed and with unused fields preserved.

MATLAB Code

Unused Structure Fields Removed (default)

Unused Structure Fields Preserved

% Entry-point functionfunctionout = myStruct(n)%# codegens.a = [n n n]; s.b = n+2; s.c = n; out = myAdd([s s]);end
functionout = myAdd(s) coder.inline('never'); out = s(1).b + s(2).b;end

Code generation command

cfg = coder.config('lib'); codegenmyStruct-args{0}-configcfg-report

Generated C Code

typedef struct { double b; } struct_T; /* * Arguments : double n * Return Type : double */ double myStruct(double n) { struct_T b_s[2]; struct_T s; s.b = n + 2.0; b_s[0] = s; b_s[1] = s; return myAdd(b_s); }

Code generation command

cfg = coder.config('lib'); cfg.PreserveUnusedStructFields = true; codegenmyStruct-args{0}-configcfg-report

Generated C Code

typedef struct { double a[3]; double b; double c; } struct_T; /* * Arguments : double n * Return Type : double */ double myStruct(double n) { struct_T b_s[2]; struct_T s; s.a[0] = n; s.a[1] = n; s.a[2] = n; s.b = n + 2.0; s.c = n; b_s[0] = s; b_s[1] = s; return myAdd(b_s); }

Usage Notes

  • WhenPreserveUnusedStructFieldsis set tofalse, the code generator preserves properties or fields of the entry-point function input and output arguments and values passed tocoder.ceval.

  • WhenPreserveUnusedStructFieldsis set totrue, the code generator removes properties or fields whose data types are unknown.

See Also

||