From: DaBu Butt on
BACKGROUND: I'm using a reasonably large simulink model which has been developed over several years. Code is generated directly from the model and all variables which are passed between modules are defined as global variables. Five modules are coded by hand in order to optimise for speed. At present these modules are 'represented' by equivalent modules in the simulink model. In order to reduce the effort of maintaining c-code and the simulink model I wish to incorporate the c-code into the model by using the s-function builder.

THE PROBLEM: The existing code has functions which use global variables instead of passing parameters to the function. When I try to declare the global variables in the c-code I get a "declaration syntax error". This must be something simple (i hope so anyway) but I can't find anywhere in the documentation or forum which explains the syntax and the rules for when it can be used in a c-file s-function.

THE CODE: To analyse the problem, I've set up a simple simulink model which adds two inputs. Initially i've just tried to set the output to a global variable (,the next stage is to read the inputs as global variables). Here's the relevant bits of code..

In file WithNoParameters_wrapper.c :

void WithNoParameters_Outputs_wrapper(const int16_T *u0,
const int16_T *u1,
int16_T *y0)
{
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_BEGIN --- EDIT HERE TO _END */
HSB_add(u0, u1);
/* %%%-SFUNWIZ_wrapper_Outputs_Changes_END --- EDIT HERE TO _BEGIN */
}

In file

global RS2_Output;

#include "typegene.h"

void HSB_add(const T_SWORD *in1, const T_SWORD *in2)
{
RS2_Output = *in1 + *in2;
}

With this configuration, I receive the following errors:
Error E2141 ./\HSB_Function_without_parameter.c 2: Declaration syntax error
Error E2451 ./\HSB_Function_without_parameter.c 9: Undefined symbol 'RS2_Output' in function HSB_add

If I move the declaration to the function then it doesn't seem to recognise the key word "global".

Any suggestions / help would be much appreciated - thanks in advance !
From: Praetorian on
On Jan 8, 6:49 am, "DaBu Butt" <Ask.For.My.N...(a)continental-
corporation.com> wrote:
> BACKGROUND: I'm using a reasonably large simulink model which has been developed over several years. Code is generated directly from the model and all variables which are passed between modules are defined as global variables. Five modules are coded by hand in order to optimise for speed. At present these modules are 'represented' by equivalent modules in the simulink model. In order to reduce the effort of maintaining c-code and the simulink model I wish to incorporate the c-code into the model by using the s-function builder.
>
> THE PROBLEM: The existing code has functions which use global variables instead of passing parameters to the function. When I try to declare the global variables in the c-code I get a "declaration syntax error". This must be something simple (i hope so anyway) but I can't find anywhere in the documentation or forum which explains the syntax and the rules for when it can be used in a c-file s-function.
>
> THE CODE: To analyse the problem, I've set up a simple simulink model which adds two inputs. Initially i've just tried to set the output to a global variable (,the next stage is to read the inputs as global variables). Here's the relevant bits of code..
>
> In file WithNoParameters_wrapper.c :
>
> void WithNoParameters_Outputs_wrapper(const int16_T *u0,
>                           const int16_T *u1,
>                           int16_T *y0)
> {
> /* %%%-SFUNWIZ_wrapper_Outputs_Changes_BEGIN --- EDIT HERE TO _END */
> HSB_add(u0, u1);
> /* %%%-SFUNWIZ_wrapper_Outputs_Changes_END --- EDIT HERE TO _BEGIN */
>
> }
>
> In file
>
> global RS2_Output;
>
> #include "typegene.h"
>
> void HSB_add(const T_SWORD *in1, const T_SWORD *in2)
> {
>     RS2_Output = *in1 + *in2;
>
> }
>
> With this configuration, I receive the following errors:
> Error E2141 ./\HSB_Function_without_parameter.c 2: Declaration syntax error
> Error E2451 ./\HSB_Function_without_parameter.c 9: Undefined symbol 'RS2_Output' in function HSB_add
>
> If I move the declaration to the function then it doesn't seem to recognise the key word "global".
>
> Any suggestions / help would be much appreciated - thanks in advance !

That is because C/C++ does not have a 'global' keyword. What
determines whether an identifier is global or not is the scope it has
been declared in. Your declaration should look like this:

<type_name_of_RS2_Output> RS2_Output;

Also, be aware that if you use a global variable in an S-Function then
you can only use that S-Function block once within your model because
all instances of S-Functions share global and static variables
declared at file scope. To have persistent data variables that are
private to the S-Function instance, take a look at the documentation
for "work vectors".

HTH,
Ashish.
From: DaBu Butt on
Thanks Ashish for your reply. Unfortunately it only partly helps me. I'm aware that C doesn't have a 'global' keyword. However, documentation on other webpages had given me the impression that this was the correct method (although it wasn't standard c). I've now removed this line from the code.

I still, however, have the problem that my c-code s-function can't find the variable which has been defined as global in the workspace. I've included an "extern int RS2_Output" to the file but when I try to build the s-function I get the error message "Error: Unresolved external '_RS2_Output' referenced from ... " .

I'm not sure if the problem is in the s-function or possibly in the definition of the global variable in workspace.

Again, thanks in advance for any help or tips!
From: Boni on
hi i am also having the problem with global declaration of a variable in s-function builder need help in this matter