MATLAB: How to inline external C-Code with legacy code tool

simulink coder

I am calling external C functions via an S-function. The S-function is created using Legacy Code Tool (LCT), which also generates a TLC file.
Despite the TLC file, the external C code is not inlined when generating C code. What is the correct workflow to inline the external C code?

Best Answer

You can use #ifdef MATLAB_MEX_FILE pragma in your external header file to control compiler function inline process
Assume we would inline external code: myfoo.c, myfoo.h
Step 1: Add pragma to your external header file myfoo.h e.g.
#ifdef MATLAB_MEX_FILE // This is for simulation only, we create empty functions
void myfoo(uint32_t instance, uint8_t channel);
#else // Code generation
#include "a.h"
#include "b.h"
static inline void myfoo(uint32_t instance, uint8_t channel)
{
...
}
#endif
Step 2: The myfoo.c contains this
#include "myfoo.h"
#ifdef MATLAB_MEX_FILE // This is for simulation only, we create empty functions
void myfoo(uint32_t instance, uint8_t channel
{
}
#endif
In the model step function, the external code will be inlined after compiling code from your Simulink model.
Related Question