Include header file in generated code


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



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

#include "HeaderFile"


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

#include <HeaderFile>


collapse 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);

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

function y = myfunc 
    y = 21;
    % Running in generated code
        y = coder.ceval('myMult2', y);
    % Running in MATLAB
        y = y * 2;

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

collapse 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


  • 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 For example:


More About

collapse all


  • 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?