Import Custom Data Type Definitions from External Header Files
By default, the code generator uses predefined data types in the generated code. You can use customized data type names by importing custom type definitions from external header files. You can then use your own type definitions in the generated C/C++ code. If you have existing legacy code, you can integrate that code, in the generated C/C++ code. You can import a header file by using either the MATLAB® Coder™ app or the command-line interface.
Import Custom Types by Using the MATLAB Coder App
To import custom types from external header files:
Open the MATLAB Coder app and proceed to the Generate Code page.
Click More Settings.
In the Code Appearance tab, select Enable custom data type replacement.
Specify your custom names for the data types in Enable custom data type replacement table.
Select the Import custom types from external header files check box.
In the Header files text field, add the external header file names using either a string array or a cell array of character vectors. For example,
["myHeader1.h","myHeader2.h","myHeader3.h"]
or{'myHeader1.h','myHeader2.h','myHeader3.h'}
.
Import Custom Types by Using the Command-Line Interface
Use the ReplacementTypes.IsExtern
and
ReplacementTypes.HeaderFiles
properties in a coder.EmbeddedCodeConfig
object when you generate code by using
codegen
.
Create a code configuration object for generation of a static library.
cfg = coder.config('lib','ecoder',true);
Specify custom names for the data types. For example,
double
is named asCustom_Double
andint8
is named asCustom_Int8
in the code.cfg.EnableCustomReplacementTypes = true; cfg.ReplacementTypes.double = "Custom_Double"; cfg.ReplacementTypes.int8 = "Custom_Int8";
Specify configuration properties for importing the external header files.
% Include single header file cfg.ReplacementTypes.IsExtern = true; cfg.ReplacementTypes.HeaderFiles = "myHeader.h"; cfg.CustomInclude = 'C:\myFiles'; % Include path of the header file
% Include multiple header files cfg.ReplacementTypes.IsExtern = true; cfg.ReplacementTypes.HeaderFiles = ["myHeader1.h","myHeader2.h","myHeader3.h"] cfg.CustomInclude = '"C:\Program Files\MATLAB\myFiles"'; % Include path of the header files
For more information on
CustomInclude
, see Configure Build by Using the Configuration Object.Generate code by using
codegen
and the-config
option.codegen myAdd.m -args {1,int8(1)} -config cfg -report