coder.cinclude - Include header file in generated code - MATLAB (original) (raw)

Include header file in generated code

Syntax

Description

coder.cinclude([headerfile](#btsacfj-headerfile)) includes a header file in generated C/C++ source code.

MATLAB® Coder™ generates the include statement in the C/C++ source files that are generated from the MATLAB code that contains the coder.cinclude call.

In a Simulink® model, when a coder.cinclude call appears in aMATLAB Function block, the code generator puts the include statement in the model header file.

example

Note

In most cases, use coder.ceval with the"-headerfile" option instead ofcoder.cinclude. Use coder.cinclude only if you use coder.ceval to call multiple C/C++ functions defined in the same header file. You must callcoder.cinclude before the correspondingcoder.ceval call, and you must callcoder.cinclude and coder.ceval in the same MATLAB function.

coder.cinclude([headerfile](#btsacfj-headerfile),'InAllSourceFiles',[allfiles](#btsacfj-allfiles)) uses the allfiles option to determine whether to include the header file in almost all C/C++ source files.

If allfiles is true, MATLAB Coder generates the include statement in almost all C/C++ source files, except for some utility files. This behavior is the coder.cinclude behavior from R2016a and earlier releases. The presence of the include statement in these additional files can increase compile time and make the generated code less readable. Use this option only if your code depends on the legacy behavior. Ifallfiles is false, the behavior is the same as the behavior of coder.cinclude(headerfile).

In a MATLAB Function block,coder.cinclude(headerfile,'InAllSourceFiles', allfiles) is the same as coder.cinclude(headerfile).

Examples

collapse all

Generate code from a MATLAB function that calls an external C function. Use coder.cinclude to include the required header file in the generated C code.

In a writable folder, create a subfolder mycfiles.

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

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

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

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

Write a MATLAB function, myfunc, that includes myMult2.h and calls myMult2 for code generation only.

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

Create a code configuration object for a static library. Specify the locations of myMult2.h and myMult2.c

cfg = coder.config('lib'); cfg.CustomInclude = fullfile(pwd,'mycfiles'); cfg.CustomSource = fullfile(pwd,'mycfiles','myMult2.c');

Generate the code.

codegen -config cfg myfunc -report

The file myfunc.c contains this statement:

The include statement does not appear in any other file.

Generate code from a MATLAB Function block that calls an external C function. Use coder.cinclude to include the required header file in the generated C code.

In a writable folder, create a subfoldermycfiles.

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

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

Write the header file myMult2.h. Save it inmycfiles.

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

Create a Simulink model that contains a MATLAB Function block connected to an Outport block.

This image shows a MATLAB Function block attached to an Outport block.

In the MATLAB Function block, add the functionmyfunc that includes myMult2.h and callsmyMult2.

function y = myfunc %#codegen y = 21; coder.cinclude('myMult2.h'); y = coder.ceval('myMult2', y); % Specify the locations of myMult2.h and myMult2.c coder.extrinsic('pwd', 'fullfile'); customDir = coder.const(fullfile(pwd, 'mycfiles')); coder.updateBuildInfo('addIncludePaths', customDir); coder.updateBuildInfo('addSourcePaths', customDir); coder.updateBuildInfo('addSourceFiles', 'myMult2.c'); end

Open the Configuration Parameters dialog box.

On the Solver pane, select a fixed-step solver.

Save the model as mymodel.

Build the model.

The file mymodel.h contains this statement:

To read more about integrating custom code in a MATLAB Function block, see Integrate C Code by Using the MATLAB Function Block (Simulink).

Input Arguments

collapse all

Option to include header file in all generated C/C++ source files. If allfiles is true, MATLAB Coder generates the include statement in almost all of the C/C++ source files, except for some utility files. If allfiles is false, the behavior is the same as the behavior of coder.cinclude(headerfile).

In a MATLAB Function block, the code generator ignores the all source files option.

Data Types: logical

Limitations

Tips

Extended Capabilities

expand all

The coder.cinclude function support only MATLAB to High-Level Synthesis (HLS) workflow in HDL Coder™.

Version History

Introduced in R2013a