Specify External File Locations

External File Locations for External Code Integration

To integrate external code with generated C/C++ code, you must specify the locations of your external source files, header files, and libraries to MATLAB® Coder™.

You can specify the file locations:

  • In a class definition file, when you derive a class from coder.ExternalDependency

  • In your MATLAB code using the coder.updateBuildInfo function

  • In the project settings dialog box

  • From the command line

  • In the configuration object

Specify External Files in a Class Derived from coder.ExternalDependency

When you derive a class from coder.ExternalDependency, you write a method updateBuildInfo that specifies the locations of the external files required for the build. See coder.ExternalDependency.

Specify External Files in MATLAB Code Using coder.updateBuildInfo

In your MATLAB code, you can call coder.updateBuildInfo to specify the locations of external files. See coder.updateBuildInfo.

Specify External Files Using the MATLAB Coder App

  1. On the Generate Code page, to open the Generate dialog box, click the Generate arrow .

  2. In the Generate dialog box, set the Build Type to one of the following:

    • Source Code

    • Static Library

    • Dynamic Library

    • Executable

  3. Click More Settings.

  4. On the Custom Code tab, under Custom C-code to include in generated files, specify Source file and Header file. Source file specifies that the code appear at the top of generated C/C++ source files. Header file specifies that the code appear at the top of generated header files.

    Custom Code PropertyDescription
    Under Additional files and directories to be built, provide an absolute path or a path relative to the project folder.

    Include directories

    Specifies a list of folders that contain custom header, source, object, or library files. Separate list items with a semicolon.

    Source files

    Specifies additional custom C/C++ files to be compiled with the MATLAB file. Separate list items with a semicolon.

    Libraries

    Specifies the names of object or library files to be linked with the generated code. Separate list items with a semicolon.

    Under Custom C-code to include in generated files

    Source file

    Specifies code to appear at the top of generated C/C++ source files.

    Header file

    Specifies custom code to appear at the top of generated header files

Specify External Files at the Command Line

When you compile MATLAB function with MATLAB Coder, you can specify custom C/C++ files — such as source, header, and library files — on the command line along with your MATLAB file. For example, suppose you want to generate an embeddable C code executable that integrates a custom C function myCfcn with a MATLAB function myMfcn that has no input parameters. The custom source and header files for myCfcn reside in the folder C:\custom. You can use the following command to generate the code:

codegen C:\custom\myCfcn.c C:\custom\myCfcn.h myMfcn

Specify External Files with Configuration Objects

You can specify custom C/C++ files by setting custom code properties on configuration objects.

  1. Define a configuration object, as described in Creating Configuration Objects.

    For example:

    cc = coder.config('lib');

  2. Set one or more of the custom code properties.

    Custom Code PropertyDescription

    CustomInclude

    Specifies a list of folders that contain custom header, source, object, or library files.

      Note:   If your folder path name contains spaces, you must enclose it in double quotes:

      cc.CustomInclude = '"C:\Program Files\MATLAB\work"'

    CustomSource

    Specifies additional custom C/C++ files to be compiled with the MATLAB file.

    CustomLibrary

    Specifies the names of object or library files to be linked with the generated code.

    CustomSourceCode

    Specifies code to insert at the top of each generated C/C++ source file.

    CustomHeaderCode

    Specifies custom code to insert at the top of each generated C/C++ header file.

    For example:

    cc.CustomInclude = 'C:\custom\src C:\custom\lib';
    cc.CustomSource = 'cfunction.c';
    cc.CustomLibrary = 'chelper.obj clibrary.lib';
    cc.CustomSourceCode = '#include "cgfunction.h"';

  3. Compile the MATLAB code specifying the code generation configuration object.

    codegen -config cc  myFunc

  4. Call custom C/C++ functions.

    From...Call...
    C/C++ source codeCustom C/C++ functions directly
    MATLAB code, compiled on the MATLAB Coder pathCustom C/C++ functions using coder.ceval.

    For example, from MATLAB code:

    ...
    y = 2.5;
    y = coder.ceval('myFunc',y);
    ...

Was this topic helpful?