Generating PIL from S-Function Unresolved symbol during linking

11 views (last 30 days)
I have some c code that is wrapped in an s-function using legacy_code().
The S-Function has to call c initialisation function void init_Code();
def.StartFcnSpec = ['void init_Code(void)'];
legacy_code() generates the sfunction withought error.
I then add some bus input/outpt to the sfunction and then generate a PIL block out of that so I can deploy it to a microcontroller. If I don't set the StartFcn this works prefectly fine.
With the StartFcn I get compilation error.
undefined first referenced
symbol in file
--------- ----------------
_init_Code ../sfunksCode.obj
error: unresolved symbols remain
error: errors encountered during linking; "sfunksCode.out" not built
Note that the undefined symbol has a leading underscore ( _ ) before the function name.
I had a look at the generated c code and there is no "_init_Code" anywhere in any of the generated files.
The s-function initialisation block looks fine:
44 /* Model initialize function */
45 void sfunksCode_initialize(void)
46 {
47 /* Registration code */
48
49 /* initialize error status */
50 rtmSetErrorStatus(sfunksCode_M, (NULL));
51
52 /* external inputs */
53 sfunksCode_U.input = sfunksCode_rtZsfunkCodeBUS;
54
55 /* external outputs */
56 (void) memset(&sfunksCode_Y.output[0], 0,
57 5U*sizeof(real32_T));
58
59 /* Start for S-Function (customCode): '<Root>/customCode' incorporates:
60 * Inport: '<Root>/ input'
61 * Outport: '<Root>/output'
62 */
63 init_Code();
64 }
Notice the init function is correctly called.
And this only happens at the linker stage ie all the sources compile fine.
I also created another function named _init_Code() which just calles my original init function to see if that would do as a work around, however I get the exact same error.
I'm guessing Coder is doing something exciting and helpful that I need to switch off?
**Edit: Further investigation suggests that this might be related to the specific micro controller which is a TI C2000 Delfino

Answers (1)

Antonin
Antonin on 18 Apr 2018
Hi Michael,
The linker doesn't know where to find the init_code() function that you are using in you S-function. You need to add the C file where it is declared as a Custom Code source file. Open the configuration parameters of the model (Ctl+E) then go to Code Generation --> Custom Code --> Additional build information --> Source files and list the C file where the init_function resides. This will do the trick. I hope it helps,
Antonin.
  1 Comment
Michael Krastev
Michael Krastev on 18 Apr 2018
The linker knows where it is, it's declared in the same place as the run function which doesn't complain (as in the sourcefile is already in the include path). Plus the linker doesn't complain about the init_code() function but about _init_code() doesn't exist in the source code.
The issue seems to be around the setup for the TI build. At some point the TI toolchain adds a leading underscore to function name symbols. When generating the PIL block you can see the TI tools do this in the generated .MAP file. Something in there was going wrong. I deleted the whole file structure for the PIL build and reran it which seems to have cleared the issue so I'm assuming some setup file that isn't regenerated every build had done something odd.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!