External Code Integration Examples
This topic shows various scenarios of external code integration.
Integrate External C++ Code into a Model Using S-Functions
This example shows how to integrate C++ code into a model by using S-functions.
Open Example Model
Open the example model CppSFunctionExternalCode
.
open_system('CppSFunctionExternalCode');
Configure C++ Compiler
1. In the MATLAB® Command Window, enter this command:
mex -setup
2. When prompted, select a C++ compiler that is installed on your development computer.
Integrate C++ Code
You can integrate C++ code into a model by using S-functions.
1. Prepare the S-function source.
Open
sfun_cppcount_cpp.cpp
.Open
sfun_cppcount_cpp.h
.
2. Open the Simulink Coder or Embedded Coder app.
3. Configure S-function source files and folders. Open the Model Configuration Parameter dialog box, navigate to the Code Generation > Custom Code pane, and add the S-function source files and include
folders.
4. Inline the S-function to eliminate overhead of S-functions in the generated code.
Edit sfun_cppcount_cpp.tlc
.
Configure Model for C++ Code Generation
Set model configuration parameter Language to C++
.
Generate and review code
Generate and review the C++ code.
Related Topics
Integrate External C Code into Generated Code by Using Model Configuration Parameters
This example shows how to place external code in generated code by using model configuration parameters.
1. Open the model CustomCode
.
open_system('CustomCode')
2. Open the Simulink Coder or Embedded Coder app.
3. Open the Model Configuration Parameters dialog box and navigate to the Custom Code pane.
4. On the Additional source code tab, examine the settings for model configuration parameters Additional code and Initialize code.
Additional code specifies a comment and sets the variable
GLOBAL_INT2
to -1.Initialize code initializes the variable
GLOBAL_INT2
to 1.
5. Close the dialog box.
6. Generate code and a code generation report.
7. Examine the code in the generated source file CustomCode.c
. At the top of the file, after the #include
statements, you find the following declaration code.
int_T GLOBAL_INT2 = -1;
The following assignment appears in the model initialize entry-point function. The example specifies this assignment with the Initialize code configuration parameter.
GLOBAL_INT2 = 1;
Integrate External C Code into Generated Code by Using Model Configuration Parameters
This example shows how to place external code in generated code by using model configuration parameters.
1. Open the model CustomCode
.
open_system('CustomCode')
2. Open the Simulink Coder or Embedded Coder app.
3. Open the Model Configuration Parameters dialog box and navigate to the Custom Code pane.
4. On the Additional source code tab, examine the settings for model configuration parameters Additional code and Initialize code.
Additional code specifies a comment and sets the variable
GLOBAL_INT2
to -1.Initialize code initializes the variable
GLOBAL_INT2
to 1.
5. Close the dialog box.
6. Generate code and a code generation report.
7. Examine the code in the generated source file CustomCode.c
. At the top of the file, after the #include
statements, you find the following declaration code.
int_T GLOBAL_INT2 = -1;
The following assignment appears in the model initialize entry-point function. The example specifies this assignment with the Initialize code configuration parameter.
GLOBAL_INT2 = 1;