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

Help with external C/C++ function

Help with external C/C++ function

Hello,
I am new to OpenModelica and trying to write an external function. I have searched the forum but was not able to find an answer to my questions (compiler errors). Thank you in advance for your help.

My external function file (xmathlib.cpp) is as follows:

// Begin

class Calc
{




}




void AddTwo(double a, double b, double *c)
{

*c = a+b;

}


// End


Question:
When I dont have the class definition listed, the file compiles and I can call my external function from OM.
When the class is as listed above (or even as a part of an include file), I keep getting an error as follows:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before Calc.

I have tried several different files etc., but for some reason, I cannot use a class.

We have existing numerical libraries that have been developed in C++ and I would like to write this wrapper xmathlib.c  so that I can call some of the routines from OM.


Any help or pointers are much appreciated. Thank you.

-V

Re: Help with external C/C++ function

Yes, you need to take some extra care as Modelica does not support external "C++".

You can force OMC to use a non-standard compiler (setCompiler("g++"); but your code might not work in other Modelica tools if they also use C compilers). Or you use the more portable solution: precompiled libraries with C symbols exported as entry points (as described below).

annotation(Library="myFile.o or myFile.a or myFile.so") if the function definitions have been exported using:

Code:


extern "C" {
  void AddTwo(...) {
    ...
  }
}

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