Specify and Configure Custom C/C++ Code
You can specify sources of custom C/C++ code for your Simulink® model. These sources can include header files, source code files, and precompiled library files. Once you specify these sources, you can use functions and variables from the sources in blocks such as the C Caller and C Function blocks.
Configure Custom Code
Specify Custom Code Settings for Simulation
The examples for specifying C source, header, and library files are based on this folder structure.
To configure your model to access custom code during simulation, follow these steps.
Open the Configuration Parameters dialog box. For information on how to do this, see Set Configuration Parameters by Using the Dialog Box.
In the Simulation Target pane, in the Code Information tab, specify these parameters:
Include headers — Enter the code to include at the top of the generated
header file, which declares custom functions and data in the generated code. The code appears at the top of all generated source code files and is visible to all generated code. For example, use this parameter to enter
#definestatements. When you include a custom header file, you must enclose the file name in double quotes. For more information, see Include headers.
The code you specify in this parameter can include
externdeclarations of variables or functions, such as
extern int xor
extern void myfun(void). However, global variable or function definitions such as
void myfun(void)cause linking errors because the definitions appear multiple times in the source files of the generated code.
Include directories — Enter a list of the folder paths that contain custom header, source, and library files that you include either directly in the configuration parameters or indirectly in the compiled target. You can separate entries with newlines, spaces, or semicolons. For more information, see Include directories.
Source files — Enter a list of source files to compile and link into the target separated by newlines, spaces, or semicolons. For more information, see Source files.
Libraries — Enter a list of static libraries that contain custom object code to link into the target, separated by newlines, spaces, or semicolons. For more information, see Libraries.
Defines — Enter a space-separated list of preprocessor macro definitions to add to the generated code. For more information, see Defines.
Compiler flags — Enter additional compiler flags to be added to the compiler command line when your custom code is compiled. For more information, see Compiler flags.
Linker flags — Enter additional linker flags to be added to the linker command line when your custom code is linked. For more information, see Linker flags.
Under Advanced parameters, check that Import custom code is selected. This parameter is enabled by default.
For information on setting simulation options by using the command-line API, see Set Configuration Parameters Programmatically (Stateflow).
Specify Custom Code Settings for Code Generation
To configure your model to access custom code for code generation, use the Code Generation > Custom Code pane of the Configuration Parameters dialog box. You can use the same custom code settings for code generation that you use for simulation or use unique custom code settings.
To use the same custom code settings used for simulation, select Use the same custom code settings as Simulation Target. Specify the custom code settings in the Simulation Target pane as described in Specify Custom Code Settings for Simulation.
To use unique custom code settings, clear Use the same custom code settings as Simulation Target. In the Code Information tab, specify custom code settings for code generation. For descriptions of the parameters in this tab, see Specify Custom Code Settings for Simulation.
For more information, see Use the same custom code settings as Simulation Target (Simulink Coder) and Integrate External Code by Using Model Configuration Parameters (Simulink Coder).
Specify Relative Paths to Custom Code
When you update your model or start the simulation, the model searches for custom code files in these folders:
Model folder (if this folder is different from the current folder)
Custom list of folders that you specify
All folders on the MATLAB® search path, excluding the toolbox folders
You can specify the location of your custom code by using paths relative to one of these
folders. For instance, suppose that, in the previous example, you store the source and
header files for your custom code in the model folder subfolders
To access these files, use the Include directories parameter to specify
the relative paths of the subfolders.
Alternatively, you can use relative pathnames to specify the header and source files individually:
Under Include headers, enter this pathname.
Under Source files, enter this pathname.
Guidelines for Relative Path Syntax
When you construct relative paths for custom code, follow these syntax guidelines:
Use a single period (
.) to indicate the starting point for the relative path.
Use forward slashes (
/) or backward slashes (
\) as file separators, regardless of the platform you are using.
Enclose paths in double quotes (
"...") if they contain nonstandard path characters such as spaces or hyphens (
Enclose expressions with dollar signs (
$...$) to evaluate them in the MATLAB workspace. For example, suppose that
CustomCodeFolderis a variable that you define in the MATLAB workspace as
"module1". If you specify your custom code files using the path name
.\work\source\$CustomCodeFolder$, then the model searches for the custom code files in the folder
Model Configuration Parameters: Simulation Target | Integrate C Code Using C Caller Blocks | Integrate External C/C++ Code into Simulink Using C Function Blocks | Implement Algorithms Using C/C++ Code | C Caller | C Function