在simulink中重用C代码

时间:2015-03-29 02:50:05

标签: c simulink stateflow

我是否有可能从状态流图表调用c程序,然后我复制此图表,仍然在同一模型中,并执行这两个没有任何冲突?

例如像这样的C程序:

int var;              // var is global
int myfunction(int n)
{
  var = var + n;
  return var;
}

我的意思是,将它们视为两个不同的实体,并且不会搞砸全局变量。

顺便说一句,也没有重命名源代码中的函数,我有一个很大的程序:)

1 个答案:

答案 0 :(得分:0)

This is more a C - related issue. If you are using the same C function that operates on a global, then yes, all calls to this function will operate on the same variable. What you can do instead is make this variable local to each of the calling Stateflow states and then pass it to the C function. This way you should not have conflicts and be able to reuse your code. It's also a good design choice since you otherwise are potentially hiding a state variable in the function i.e. outside of your state machine.