How to generate custom unoptimized code within a system composer software architecture model?

C-function code generation appears to work differently when generating code inside of a system composer software architecture model isntead of a standalone simulink model. I am trying to insert certain processer primitaves (mutexes) into my simulink model such that it is incorporated into the autogenerated code. It is a few simple lines of c++ code, so I opted to use the C-function block. In both cases, I have specified in the c-function dialogue window "Generate code as-is (optimizations off)"
Inside of an export-function simulink model, I created a c-function block that includes a comment and then a simple line of code, which generates correctly:
However, if I copy this exact same c-function into an export-function model within a system composer software architecture model, it no longer generates my my specified code. Instead, it generates function calls but does not define the functions anywhere. (Even doing a simple text search of my entire workspace only shows that the function call shown below gets generated).

 Accepted Answer

Hi @Alex,
I see the code from the c-caller block when I tried. Can you look at the attached and see if there are any differences in how we configured the models?
Josh

6 Comments

Thanks for the input! It took me a bit to figure out what the differences are, because yours seemed to be working while mine would not. It appears that this code generation issue only seems to be happening within a service function call, instead of a normal export function call. I created an architecture model to show this:
Within that model I have the service function call and a regular export function call, both of which only have the exact same c-function block. You can see that the regular export function call generates the lines of code correctly, but the service function for some reason generates a function call, and no where generates the comment or a = 2+3. Any ideas what might be causing this, or if there is way around this?
More generally, it seems it is happening whenever the c-function block is embedded within any Simulink Function
I was able to reproduce the issue you are seeing. I'll do some digging and let you know what I find.
This is a known product bug. It will be fixed in R2024b update 7 available August 2025.
Are there any possible workarounds? We are using system composer for our software architecture which seems to heavily rely on Simulink Functions, but we absolutely need the ability to insert custom C++ code into the software.
The System Outputs block can be used as an alternate approach to inject arbitrary code into the generated C++ code.

Sign in to comment.

More Answers (0)

Categories

Find more on System Composer in Help Center and File Exchange

Products

Release

R2024b

Asked:

on 7 Apr 2025

Commented:

on 15 Apr 2025

Community Treasure Hunt

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

Start Hunting!