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

How to call an external function in C?

How to call an external function in C?

Hello everyone and thank you by advance for your further help,

I would like to call an external function, coded in C, to calculate the Eigen values of a Matrix of complexes.
The C-function is stored in a directory, "Eigen_Library", constituted as follows:

Eigen_Library:
       -> Eigen_Library_Header.h
       -> Eigen_Library.lib
       -> Eigen_Files:
              -> Eigen_Library_c_File.c
              -> Eigen_Library_cpp_File.cpp      (the C++ function is actually encapsulated into a C function)


The header file contains the following:

#ifndef EXAMPLE_H_INCLUDED
#define EXAMPLE_H_INCLUDED
typedef unsigned int size_t;
extern void fct_tab_c(double *, double *, size_t, size_t);
#ifdef __cplusplus
     extern "C" {
#endif
         void fct_tab(double *,double *, size_t, size_t);
#ifdef __cplusplus
     }
#endif


#endif //
EXAMPLE_H_INCLUDED


To call the function defined in the header file, I define a package, "PersoEigenLibrary", as follows:

encapsulated package PersoEigenLibrary
  import Modelica.ComplexMath.*;
  import Complex;
  import EigenLibrary;

  function Eigen_vectors_extern_function
      "Extern function that gives eigen vectors of complex 3x3 matrices"
    input Real tab[:,:];
    output Real tab_res[size(tab,1),2*size(tab,2)];
    external "C"
    fct_tab_c(tab,tab_res,size(tab_res,1),size(tab_res,2))
      annotation(Include="#include <Eigen_Library_HEADER.h>",Library="Eigen_Library");
    end Eigen_vectors_extern_function;

 
  class testofEigenLibrary
     Real ReA[3,3] = [1,1,2;4,3,2;1,2,4];
     Real ImA[3,3] = [2,2,6;4,5,4;3,2,1];
     Real A[3,6] = [ReA,ImA];
     Real res[3,12];
     algorithm
     res := Eigen_vectors_extern_function(A);
  end testofEigenLibrary;

end PersoEigenLibrary;


Then, I instantiate the class "testofEigenLibrary", defined previously in the package:

class testPersoEigenLibrary
import PersoEigenLibrary.*;
testofEigenLibrary b;
end testPersoEigenLibrary;


My problem is that I get the following messages:

https://www.openmodelica.org/images/agorapro/attachments/6364/mini_MessageError1.PNG

https://www.openmodelica.org/images/agorapro/attachments/6364/mini_MessageError2.PNG

Hence, my questions are the following:
- Where should I store the directory containing my header file,  my C and C++ functions, and their .lib file?
- How should I organize this directory?
- Is my header file correct?
- Do I need to load this directory as a Library? If yes, how?

By advance, thank you for your help.

William

Re: How to call an external function in C?

You can find all the detailed infos here: Modelica Specifications
in particular at section 12.9.4 Annotations for External Libraries and Include Files.

I attach a brief example (using MultiBody library). I hope it can be of some help.
ExternalCode.zip


BTW, do you actually have the Resource folder that OpenModelica is looking for? The one that you can see in your screenshot of the message panel?

Edited by: DarioMangoni - May-08-18 10:08:37
Attachments:

Re: How to call an external function in C?

Hello,

For some reason, the zip file is password protected. Can you please re-upload or give the password?

Thanks

Re: How to call an external function in C?

Incredibly I tried with password "modelica" and it worked.
Great subconscious memory  current/big_smile

Re: How to call an external function in C?

Thanks current/smile

When I tried to run the modelica model on Dymola, I received this Linker error:

"Compiler message:
Compiling and linking the model (Visual C++).
dsmodel.c
Creating library dymosim.lib and object dymosim.exp
LINK : warning LNK4199: /DELAYLOAD:dummyDLL.dll ignored; no imports found from dummyDLL.dll
dsmodel.obj : error LNK2019: u"

I tried also after placing the lib and dll files directly in the Library folder (tried both 46 and 64_minGW). But still getting same error.

Is there any change I need to make to the files?

Edited by: aokasha - Jan-16-20 14:15:32

Re: How to call an external function in C?

If this can be of any help, I am using external C functions with no problem in OpenModelica. Really not exactly with no problems as the new frontend doesn't support external functions yet, and I need to use the old one. I use the method recommended at Modelica by Example web site: no header file necessary. Here is an example:

function densitiesEOS_pT "Return liquid and gas densities at give temperature and pressure, by EOS"
  input AbsolutePressure p;
  input Temperature T;
  input Integer phase = 0 "0=both phases, 1= only gas, 2=only liquid";
  output Density ld;
  output Density gd;

  external "C" FF_densitiesEOS_pT(data, p, T, phase, ld, gd) annotation(
    IncludeDirectory = "modelica://FreeFluids/Resources",
    Include = "#include \"FFmodelicaMedium.c\"");
end densitiesEOS_pT;

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