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

passing record in external C function

passing record in external C function

Hello, i am new to openModelica. I use the Version 1.14 64bit in Windows 7. Now i am trying to passing a record in external C function and then give a Value to the member of the record. at last, the c function should return the modified record.

I do it through "struct". Hier ist the C code;

Header file: State.h
-----------------------------------------------------------------
typedef struct {

    //! Temperature
    double T;

} State;

void setState_C_impl(double p, State *state);
--------------------------------------------------------------------
Source file: State.c
--------------------------------------------------------------------
#include "State.h"
void setState_C_impl(double p,  State *state){
    state->T = p;
}
--------------------------------------------------------------------
the Modelica file:
--------------------------------------------------------------------
within;
package VersuchPointer

// the record--------
  record State
    Real T;
  end State;
//------------------the function
  function setState
    input Real p;
    output State state;
 
    external "C" setState_C_impl(p, state) annotation(
      /*Library = "State.o",*/
      IncludeDirectory = "modelica://VersuchPointer/Resources/Library",
      Include = "#include \"State.c\"");

  end setState;
//--------------the modell that calls the function
  model CallState
    Real p;
    State state;
    //Real y;
   
  equation
     p = 20;
     state = setState(p);
  end CallState;

end VersuchPointer;
//------------------------------------------------------------------
but I alway get the error report:
VersuchPointer.CallState_08bnd.c: In function 'VersuchPointer_CallState_eqFunction_3':
VersuchPointer.CallState_08bnd.c:46:58: error: incompatible types when assigning to type 'modelica_real {aka double}' from type 'VersuchPointer_State {aka struct <anonymous>}'
   data->localData[0]->realVars[1] /* state.T variable */ = omc_VersuchPointer_setState(threadData, 20.0);


also I get the Warning report:

VersuchPointer.CallState_functions.c: In function 'omc_VersuchPointer_setState':
VersuchPointer.CallState_functions.c:29:27: warning: passing argument 2 of 'setState_C_impl' from incompatible pointer type [-Wincompatible-pointer-types]
   setState_C_impl(_p_ext, &_state_ext);

                           ^
In file included from VersuchPointer.CallState_includes.h:4:0,
                 from VersuchPointer.CallState_functions.c:7:
C:/OpenModelica1.14.1-64bit/lib/omlibrary/VersuchPointer/Resources/Library/State.c:3:6: note: expected 'State * {aka struct <anonymous> *}' but argument is of type 'VersuchPointer_State * {aka struct <anonymous> *}'
void setState_C_impl(double p,  State *state){

Could someone help me, how can I passing a record in external C function and then return it to Openmodelica.


Edited by: Czzzzz - Jan-08-20 11:30:07

Re: passing record in external C function

I believe this is a bug in OpenModelica. You probably want to open a ticket. Since it works if the record has more than 1 member, it is working in most useful cases and the compiler probably has some odd edge case for records with only 1 element ...

Re: passing record in external C function

I add another member to the struct and now it worked.

C-header:-------
-----------------------------------------------------------------------------
typedef struct {

    //! Temperature
    double p;
    double M;

} State;

void setState_C_impl(double p, double M, State *state); // it works
//void setState_C_impl(double p, State *state); // it dosent
----------------------------------------------------------------------------
C-source:
----------------------------------------------------------------------------
#include "State.h"

void setState_C_impl(double p, double M,  State *state){
    state->p = p;
    state->M = M;
}

/*
void setState_C_impl(double p,  State *state){
    state->p = p;
}
*/
-----------------------------------------------------------------------------
OpenModelica file:
------------------------------------------------------------------------------
within;
package VersuchPointer

  record State
    Real T;
    Real M;
  end State;

  function setState
    input Real p;
    input Real M;
    output State state;
 
    external "C" setState_C_impl(p, M, state) annotation(
      /*Library = "State.o",*/
      IncludeDirectory = "modelica://VersuchPointer/Resources/Library",
      Include = "#include \"State.c\"");

  end setState;

  model CallState
    Real p;
    Real M;
    State state;
    //Real y;
   
  equation
     p = 20;
     M = 30;
     //state = setState(p);
     state = setState(p,M);
  end CallState;

end VersuchPointer;
------------------------------------------------------------------------------------
Thank you very much! this Problem has botherd me for a whole day

Re: passing record in external C function

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