External functions Modelica: C compared to C99

In Modelica, you can define external functions.
Chapter 12.9 of the specification states that C and Fortran77 are supported.
C ++ and Fortran90 may be supported in the future.
Now I wonder what versions of C are supported?

In particular, I need the logarithmic gamma function available on C99, so I tried the following:

function lgamma "logarithmic gamma function" input Real u; output Real y; external "C" y = lgamma(u); end lgamma; 

but it does not work, and powf works:

 function powf "power function a^b" input Real a; input Real b; output Real y; external "C" y = powf(a,b); end powf; 

This is probably because powf is available in C, and lgamma is available in C99.
But is this a limitation of Modelica, Dymola, or my compiler?
Is there a way to make C99's external features work?
On Wikipedia's list of mathematical operations C there is another interesting function, such as the erf and erfc error function, and that would be nice to have.

+4
source share
1 answer

You can only assume that the C89 / 90 code compiles in the Modelica compiler. This is mostly about syntax (if you use Include annotations or Library="file.c" ).

The functions available depend largely on the C library your compilers are associated with. I believe the Microsoft C library does not contain lgamma , so it cannot be linked. On Linux / OpenModelica, the lgamma example works like libm contains a function (it compiles using c90 mode, but implicitly adds a double lgamma(double) declaration).

The powf example also compiles, but does not work correctly, since your external "C" declaration claims to use double floating point precision (and you cannot change this as Modelica 3.2). powf will read half a and use it as the first argument, then read the second half of a and use it as the second argument. b will be discarded. If you set the compiler flags to std=c99 , an error is detected:

powf.h: 23:15: error: conflicting types for 'powf

Note that if you use Dymola on Windows, you are most likely using Visual Studio. In this case, there is no support for C99, except for parts that were copied from C ++.

+4
source

All Articles