Main Content

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:

  1. Open the MATLAB Coder app and proceed to the Generate Code page.

  2. Click More Settings.

  3. In the Code Appearance tab, select Enable custom data type replacement.

  4. Specify your custom names for the data types in Enable custom data type replacement table.

  5. Select the Import custom types from external header files check box.

  6. 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.

  1. Create a code configuration object for generation of a static library.

    cfg = coder.config('lib','ecoder',true);

  2. Specify custom names for the data types. For example, double is named as Custom_Double and int8 is named as Custom_Int8 in the code.

    cfg.EnableCustomReplacementTypes = true;
    cfg.ReplacementTypes.double = "Custom_Double";
    cfg.ReplacementTypes.int8 = "Custom_Int8";
    

  3. 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.

  4. Generate code by using codegen and the -config option.

    codegen myAdd.m -args {1,int8(1)} -config cfg -report
    

See Also

Functions

Objects

Related Topics