Integrate and call external C code from a Simulink model
Simulink / User-Defined Functions
TheC Functionblock integrates and calls external C code from a Simulink®model. Use this block to define external code and customize the integration of your code by preprocessing or postprocessing the data. In addition, you can specify customized code for simulation and C code generation. You can call functions defined in your code conditionally, and you can also call multiple functions in one block. Using this block, you can initialize the block's persistent data and pass it to an external function.
You can also useC Functionblock to call a subset of C Math Library functions. SeeCall C Library Functions From C Function Blockfor more information.
TheC Functionblock supports initializing persistent data and calling external functions from the block dialog. TheC Functionblock supports only initializing and terminating persistent data; the block does not support updating the data during simulation. To model a dynamic system with continuous states, use anS-Functionblock. To learn more about S-functions, seeWhat Is an S-Function?.
Define the source code and supporting files to be called by theC Functionblock in theSimulation Targetpane of the configuration parameters.
The following features of Simulink are not compatible with theC Functionblock.
Simulink Coverage™
Only execution coverage is measured.
Simulink Code Inspector™
Simulink Design Verifier™
Simulink Test™
– Test harness
In addition, the following limitations apply to the source code referenced by aC Functionblock.
The C keywordstatic
is not supported.
To cache values across time steps, define the symbol asPersistent
in theSymbolstable of the block dialog.
Files cannot be included in the script.
External functions must be specified in theSimulation Targetpane of the Configuration Parameters dialog.
If you have existing includes defined in theSimulation Targetpane that cannot be mixed with the new include, consider adding the include and C Function block in a library model and use a link to the block in the main model. For more information, seeCreate Custom Library.
不能分配给不同类型的指针each other. The pointer types should match when you call external functions.
The code cannot take the address of a constant.
Directly calling of some C library functions from theC Functionblock is not supported. To see a list of the C Math Library functions that you can call directly, seeCall C Library Functions From C Function Block. To call other C library functions, create a wrapper function that calls the C library function.