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

External C function from DLL

External C function from DLL

Hi everyone,
this is my first post on this forum! I'm a Modelica newbie, so please forgive me if I hit a very simple issue.
Basically, I tried to compile a simple DLL using mingw64 on my Windows 10 machine, then I created this very basic model:

Code:

model test_dll

  function my_sum
      input Real a;
      input Real b;
      output Real result;
    external "C" annotation(Library="libtest_om");
  end my_sum;
 
  Real result;
  parameter Real a = 1;
  parameter Real b = 2;
equation
  result = my_sum(a, b);
end test_dll;

It calls the my_sum external function from the libtest_om.dll library. The content of the library is just the function my_sum:

Code:


double my_sum(double a, double b)
{
    double res;
    res = a + b;
    return res;
}

And the DLL itself is located in the path <workdir>/Resources/Library folder.
When I launch the simulation like this, it builds ok, but it fails at startup:
https://ibb.co/gH6zhn

Now, after several failed attempts, I tried to follow the examples of the Modelica Guide, and I tried to put my dll in the <workdir> path. I modified the model as follows:

Code:


model test_dll
  function my_sum
      input Real a;
      input Real b;
      output Real result;
    external "C" annotation(Library="libtest_om", LibraryDirectory="modelica://test_dll");
  end my_sum;
 
  Real result;
  parameter Real a = 1;
  parameter Real b = 2;
equation
  result = my_sum(a, b);
end test_dll;

and now it works fine!
https://ibb.co/fS8ta7

So, the question is: do you know if there is something that I'm missing when trying to link my external function in a directory that is different from the working directory?

Thank you

Re: External C function from DLL

I suppose OMEdit doesn't add the found library directories to the PATH? Alternatively, OMC doesn't copy dll dependencies from the library directory to the directory of the executable?

Re: External C function from DLL

Yes, definitely it must be something with the PATH. But how to check? I have tried to run the compiled executable from within OMEdit, opening the terminal from there so it could load all the PATH. No output shown in the console (it crashes on startup). On the other hand, if I do the same with the working executable, an output is shown in the console:

Code:


stdout            | info    | The initialization finished successfully without homotopy method.
stdout            | info    | The simulation finished successfully.

Do you have some suggestions on how can I check this?

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