Include header file in generated code
coder.cinclude(
includes
a header file in generated C/C++ source code.headerfile
)
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 a MATLAB Function block, the code generator
puts the include statement in the model header file.
coder.cinclude(
uses
the headerfile
,'InAllSourceFiles',allfiles
)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.
If allfiles
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)
.
Do not call coder.cinclude
inside
run-time conditional constructs such as if
statements, switch
statements, while
-loops,
and for
-loops. 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 ...
Before a coder.ceval
call, call coder.cinclude
to
include the header file required by the external function that coder.ceval
calls.
Extraneous include statements in generated C/C++ code can increase compile time and reduce code readability. To avoid extraneous include statements in code generated by MATLAB Coder, follow these best practices:
Place a coder.cinclude
call as
close as possible to the coder.ceval
call that
requires the header file.
Do not set allfiles
to true
.
For the MATLAB Function block, the code generator generates the include statement in the model header file.
In R2016a and earlier releases, for any coder.cinclude
call, MATLAB
Coder included
the header file in almost all generated
C/C++ source files, except for some utility files. If you have code
that depends on this legacy behavior, you can preserve the legacy
behavior by using this syntax:
coder.cinclude(headerfile,'InAllSourceFiles',true)