Search code examples
matlabcode-generations-function

Matab tlc code for s-functions having multiple instances


i need to inline an s-function having multiple instances in my simulink model. In S-function, i need to load a dll file but it should be done in only one instance of s-function. I wrote tlc as follows : ..... % function Start(block, system) Output

dll_handle = LoadLibraryA("mydll"); dll_setup=(DLL_SETUP)GetProcAddress(dll_handle,"setup"); .....

After code generation, those Start function lines were added to source code for every instance of s-function. However, i need to load dll once. Is it possible to add those lines only for the first instance of s-function?

Note: I know that in TLC documentation it states to avoid inlining when there are multiple instances of s-function in the model.


Solution

  • Simplest thing to do would be to put those lines in an external file and use static variables to track one-time initialization. Then add call to that function from the Start function. If you want to generate that external file from tlc look into BlockTypeSetup TLC function and LibAddToModelSources to add source file to generated code.