MATLAB Answers


Trouble inlining C-Mex S-Functions

Hello everyone!

After spending quite a long time fighting with this issue I think I’m at the point of having to ask for some help to clear up my confussion. I consulted the documentation, examples and discussions, and thought I understood, but my failure to get it working proves the contrary.

So: I want to inline a quite heavy iterative algorithm that was given to me in form of a C-Mex S-Function. I’m working with code generation for embedded microcontroller targets, compiling the code in the corresponding IDE (TI CCSv3.3 and CCSv5). Everything was working fine until I decided to implement this S-Function and to inline it, mainly to avoid the S-Function API and improve the efficiency on target.

Writing a wohle new TLC is not a reasonable option for me. So I thought I could use the Legacy Code Tool. Am I wrong here and is this just thought for “pure” C code? Maybe I still could use it later on for registry operations, but first I need to implement the algorithm.

All the legacy_code commands seemed to work (sfcn_cmex_generate, slblock_generate, sfcn_tlc_generate…) except the ‘compile’ which failed with docens of messages like: “sfun_orig.obj : error LNK2005: _ssSetStateAbsTol already defined in ex_sfun_out.obj”

I’m therefore assuming I must use a wrapper. I understand this requires writing a small TLC to embodie the calling function. This shouldn’t be a big deal, as the algorithm has 2 inputs, 1 output, 1 discrite inherited sample time and just needs a couple of values from the previous steps... but I still didn’t manage it. Using it as a common level-2 C-Mex S-Functions works fine.

I’m even starting to think of “extracting” the c routines of the s-function and create a regular .c to work with, but I’m not sure if it’s a good solution... and maybe it’s an unnecessary mess.

Hopefully someone will give me a clue… so that I know in which way to focus my efforts.. .

Regards, and thanks a lot in advance!



Are you using functions like ssSetStateAbsTol in your C code? If yes, that may be the issue. The code that you give to the Legacy Code Tool is supposed to be something that can run independently (including on an embedded target).

Thanks for your answer!... and sorry for my huge reaction time, I was unexpectedly overloaded in another project.

I do have the functions ssSetNumSFcnParams, ssSetNumContStates, ssSetNumDiscStates, ssSetNumInputPorts, ssSetInputPortWidth, ssSetInputPortRequiredContiguous, ssSetInputPortDataType, ssSetInputPortDirectFeedThrough, ssSetNumOutputPorts, ssSetOutputPortWidth, ssSetOutputPortDataType, ssSetNumSampleTimes, ssSetNumRWork, ssSetNumIWork, ssSetNumPWork, ssSetNumModes, ssSetNumNonsampledZCs, ssSetOptions, ssSetSampleTime, ssSetOffsetTime inside the functions static void mdlInitializeSizes(SimStruct *S) and static void mdlInitializeSampleTimes(SimStruct *S).

Where you also refferring to those??

I have made a first attempt to adapt it into a s-function builder block to generate a Wrapper TLC, but I'm not sure if this makes sense.

Could I use the legacy code tool for this? Maybe I'm using it wrong. My aim is to make the code execution more efficient, avoiding the s-function API.

Regards and thanks again!


Log in to comment.

1 Answer

Answer by Kaustubha Govind on 13 Mar 2013
 Accepted Answer

I think you might be using the Legacy Code Tool incorrectly - the code that you supply to the tool is supposed to be standalone C code (probably some legacy algorithms that you inherited which you want to run as part of a Simulink model), and the tool generates the "wrapper" S-function interface like mdlInitializeSizes, mdlOutputs, etc. along with TLC code which basically just asks Simulink Coder to generates calls into your standalone C code. I wonder if it might be easier if you manually create the TLC file, unless there is an easy way to separate out the algorithm part of your S-function into standalone C code, and then generate a new S-function and TLC using Legacy Code Tool or S-function Builder.


Show 1 older comment

Yes, if you separate out your algorithm into generic C, then your TLC will just be a skeleton that calls into your algorithm entry points.

Is there a significant performance difference between using the Legacy Tool and a S-Function Builder which generates a Wrapper TLC?

It's just that I'm not very confident regarding to manually implementing the TLC with the entry points and the data mapping.


Martin: I don't know of a significant difference in the generated TLC for performance - I think you can use whatever you find more convenient.

Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today