coder.cinclude

Include header file in generated code

Syntax

  • coder.cinclude(AppHeaderFile) example
  • coder.cinclude(SysHeaderFile) example

Description

example

coder.cinclude(AppHeaderFile) includes an application header file in generated code using this format:

#include "HeaderFile"

example

coder.cinclude(SysHeaderFile) includes a system header file in generated code using this format:

#include <HeaderFile>

Examples

expand all

Include Header File Conditionally in Generated Code

Generate code from a MATLAB® function that calls an external C function to double its input. The MATLAB function uses coder.cinclude to include an application header file in generated C code running on a target machine, but not when the function runs in the MATLAB environment.

Write a C function myMult2.c that doubles its input. Save it in a subfolder mycfiles.

#include "myMult2.h"
double myMult2(double u)
{
    return 2 * u;
}

Write the application header file myMult2.h. Save it in the subfolder mycfiles.

#if !defined(MYMULT2)
#define MYMULT2
extern double myMult2(double);
#endif

Write a MATLAB function that conditionally includes the application header file and calls the external C function.

function y = myfunc 
%#codegen
    y = 21;
    if ~coder.target('MATLAB')
    % Running in generated code
        coder.cinclude('myMult2.h');
        y = coder.ceval('myMult2', y);
    else
    % Running in MATLAB
        y = y * 2;
    end
end

Compile the MATLAB function. Use the -I option to specify the path to the external header and C files.

codegen -config:lib myfunc -I mycfiles

Here is the generated C code:

/* Include files */
#include "rt_nonfinite.h"
#include "myfunc.h"
#include "myMult2.h"

/* Function Definitions */
double myfunc(void)
{
  /*  Running in generated code */
  return myMult2(21.0);
}

/* End of code generation (myfunc.c) */

Besides the files that coder.cinclude specifies, codegen automatically includes the following files:

  • Header file that defines the prototype for your entry-point function (in this case, myMult2.h)

  • rt_nonfinite.h (if you do not specify SupportNonFinite=false using coder.config when you compile the entry-point function).

Input Arguments

expand all

AppHeaderFile — Name of application header filestring

Name of an application header file, specified as a string. The header file must be located in the include path that you specify with the -I option when generating code using codegen.

Example: coder.cinclude('myheader.h')

Data Types: char

SysHeaderFile — Name of system header filestring

Name of a system header file, specified as a string enclosed in angle brackets < >. The header file must come from a standard list of system directories or from the include path that you specify with the -I option when generating code using codegen.

Example: coder.cinclude('<stdio.h>')

Data Types: char

Limitations

  • Do not call coder.cinclude inside run-time conditional constructs such as if statements, switch statements, while-loops, and for-loops. However, you can call coder.cinclude inside compile-time conditional statements, such as coder.target. For example:

    ...
     if ~coder.target('MATLAB')
       coder.cinclude('foo.h');
       coder.ceval('foo');
    end
    ...

More About

expand all

Tips

  • Call coder.cinclude before calling an external C/C++ function using coder.ceval to include in the generated code the header files required for the external function.

  • Localize use of coder.cinclude at the call sites where you want to include each header file. Do not place all of your coder.cinclude calls in the top-level (entry-point) function unless you want to include the specified header files in every build.

Was this topic helpful?