Stateflow use custom C code, while generating C++ using Embedded coder

5 views (last 30 days)
Hi, I'm using Stateflow inside simulink. I used some C functions as custom code using this article, and also I want to generate a C++ code based on my model and chart using Embedded Coder. The code generates fine, but it can not be compiled because of undefined reference linker error.
After some investigation, I found that there is no extern "C" guard for C header related to custom code in the C++ header of the generated code. After I added extern manually, linker error fixed.
My question, is about some configs or checkbox to use and having C custom code, while generating C++ code (embedded coder).
thanks

Accepted Answer

Yash Sharma
Yash Sharma on 24 Jun 2024
Hi Jafar,
To integrate C custom code with C++ code generation in Simulink using Embedded Coder, you can configure your model and custom code settings to ensure proper linkage. Here are the steps you can follow:
  1. Specify Custom Code in Model Configuration:
  • Open your Simulink model.
  • Go to Model Settings by clicking on the gear icon or using the menu Modeling > Model Configuration Parameters.
  • Navigate to Code Generation > Custom Code.
  • In the Header file field, add your custom C header file(s).
  • In the Source file field, add your custom C source file(s).
2. Add extern "C" Guards Manually: Since you have already identified that adding extern "C" guards manually works, you can continue doing this. However, you can automate this process by modifying your custom header files. Wrap your function declarations in extern "C" guards like this:
#ifdef __cplusplus
extern "C" {
#endif
// Your C function declarations
void myFunction();
#ifdef __cplusplus
}
#endif
3. Use coder.cinclude for Custom Headers: If you are using MATLAB Function blocks or Stateflow charts, you can use the coder.cinclude directive to include your custom headers. This ensures that the headers are included properly during code generation:
coder.cinclude('my_custom_header.h');
Please find attached links to documentation that may be helpful for your implementation.
Hope it helps!

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!