Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

calling an external C function having a structure for argument

calling an external C function having a structure for argument



I'm trying to execute the following program in OpenModelica

    model MyModel
  record R
    Integer a;
  end R;

  Modelica.Blocks.Interfaces.RealOutput u;
  Real A = 1;
  Real M = 10;
  Modelica.SIunits.AngularVelocity w_start = 0;
  Modelica.SIunits.AngularVelocity w_end = 10;
  R r(a=2);

  function Chirp
    input R r;
    input Modelica.SIunits.AngularVelocity w_start;
    input Modelica.SIunits.AngularVelocity w_end;
    input Real A;
    input Real M;
    input Real t;
    output Real u "output signal";

    external "C"  annotation(Include = "#include \"Chirp.c\"");
  end Chirp;
equation
  u = Chirp(r, w_start, w_end, A, M, time);
end MyModel;

the chirp C function is :

typedef struct {
int a;
}mystruct;
double Chirp(mystruct test,double w1, double w2, double A, double M, double time)
{
test.a = 2;
double res;
res=(test.a)*A*cos(w1*time+(w2-w1)*time*time/(2*M));
return res;

}

I get the following error msg :

myModel_functions.c: In function 'omc_MyModel_Chirp':
myModel_functions.c:37: error: incompatible type for argument 1 of 'Chirp'
C:/OpenModelica1.9.3Nightly//include/omc/c/Chirp.c:6: note: expected 'mystruct' but argument is of type 'MyModel_R'
mingw32-make: *** [myModel_functions.o] Error 1
mingw32-make: *** Waiting for unfinished jobs....
Compilation process failed. Exited with code 2.

can anybody tell me what is wrong with my model ? (I'm using OpenModelica v1.9.3-dev-680) thanks.

Re: calling an external C function having a structure for argument

Modelica spec says records are passed by pointer. OpenModelica (and your code does not). By passing it as pointer, we could pass it as void* and let C just work. By passing it as struct, you need to compile your external C-code in a separate file (link as a binary library).

Re: calling an external C function having a structure for argument

Thanks for your answer.
actually I'm getting started in OpenModelica and  I'm in need to use a struct as argument (in C), so can you please be more explicit when you talk about "link as a binary library". How do I do that ?
Thank you

Re: calling an external C function having a structure for argument

Re: calling an external C function having a structure for argument

Thanks a lot for your answer. It was very helpful.

There are 0 guests and 0 other users also viewing this topic
You are here: