Integrating Custom C Code with Generated Code

About Custom C Code Integration with Embedded MATLAB™ Coder

You integrate custom C code with generated C code by specifying the locations of your external source files, header files, and libraries to Embedded MATLAB™ Coder. You can specify custom C files on the command line or with configuration objects.

Specifying Custom C Files on the Command Line

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

emlc -T rtw:exe C:\custom\myCfcn.c C:\custom\myCfcn.h myMfcn

Specifying Custom C Files with Configuration Objects

You can specify custom C files by setting custom code properties on Real-Time Workshop® configuration objects (as described in Real-Time Workshop Dialog Box Overview in the Real-Time Workshop Reference documentation). Follow these steps:

  1. Define a Real-Time Workshop configuration object, as described in Creating Configuration Objects.

    For example:

    cc = emlcoder.RTWConfig;
  2. Set one or more of the custom code properties as needed:

    Custom Code PropertyDescription

    CustomInclude

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

      Note   If your directory path name contains spaces, you must enclose it in double quotes, as in this example:

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

    CustomSource

    Specifies additional custom C files to be compiled with the M-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 source file.

    CustomHeaderCode

    Specifies custom code to insert at the top of each generated 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 M-code with the Real-Time Workshop configuration object.

    For example:

    emlc -T rtw:lib -s cc myFunc
  4. Call custom C functions as follows:

    From:Call:
    C source codeCustom C functions directly
    M-code, compiled on the Embedded MATLAB pathCustom C functions using eml.ceval, as described in Calling C Functions from the Embedded MATLAB™ Subset in the Embedded MATLAB documentation.

    For example (from M-code):

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

For more information, see Configuring Your Environment for Code Generation.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS