Modelica 外部函数:C 与 C99
在 Modelica 中可以定义外部函数。
规范第 12.9 章表示支持 C 和 Fortran77,
将来可能会支持 C++ 和 Fortran90。
现在我想知道支持哪些C 版本?
特别是我需要 C99 中可用的对数 gamma 函数,所以我尝试了以下方法:
function lgamma "logarithmic gamma function"
input Real u;
output Real y;
external "C" y = lgamma(u);
end lgamma;
但它不起作用,而 powf 有效:
function powf "power function a^b"
input Real a;
input Real b;
output Real y;
external "C" y = powf(a,b);
end powf;
这可能是因为 powf 在 C 中可用,而 lgamma 是在 C99 中引入的。
但这是否是 Modelica、Dymola 或我的编译器的限制?
有没有办法让 C99 外部函数工作?
在 C 数学运算的维基百科列表上有一些更有趣的函数,例如误差函数 erf 和 erfc,拥有这些也很好。
In Modelica it is possible to define external functions.
Chapter 12.9 of the spec says C and Fortran77 are supported,
C++ and Fortran90 might be supported in the future.
Now I wonder which versions of C are supported?
In particular I need the logarithmic gamma function which is available in 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, while 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 probably happens because powf is available in C while lgamma was introduced in C99.
But is this a limitation of Modelica, Dymola or of my compiler?
Is there a way to get C99 external functions to work?
On the Wikipedia list of C mathematical operations there are some more interesting function like error function erf and erfc, these would also be nice to have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只能假设 C89/90 代码在 Modelica 编译器中编译。不过,这主要与语法有关(如果您使用
Include
注释或Library="file.c"
)。可用的函数主要取决于编译器链接的 C 库。我猜微软的C库不包含
lgamma
,所以它不能被链接。在 Linux/OpenModelica 上,
lgamma
示例确实可以工作,因为libm
包含该函数(它使用 c90 模式进行编译,但隐式添加double lgamma(double)
代码 > 声明)。powf
示例也可以编译,但无法正常工作,因为您的external "C"
声明声明它使用双精度浮点(并且从 Modelica 3.2 开始无法更改此设置) )。powf
将读取 a 的一半并将其用作其第一个参数,然后读取 a 的后半部分并将其用作其第二个参数。 b 将被丢弃。如果将编译器标志设置为std=c99
,则会检测到错误:请注意,如果您在 Windows 上使用 Dymola,则很可能使用 Visual Studio。在这种情况下,除了从 C++ 复制的部分之外,不存在 C99 支持。
You can only assume that C89/90 code compiles in a Modelica compiler. This is mainly concerning syntax though (if you use
Include
annotations orLibrary="file.c"
).The functions that are available mainly depend on the C library that your compiler links against. I guess Microsoft's C library does not contain
lgamma
, so it cannot be linked against.On Linux/OpenModelica, the
lgamma
example does work aslibm
contains the function (it's compiling using c90 mode, but implicitly adding adouble lgamma(double)
declaration).The
powf
example also compiles, but does not work correctly since yourexternal "C"
declaration states that it uses double precision floating point (and you cannot change this as of Modelica 3.2).powf
will read half of a and use it as its first argument, then read the second half of a and use it as its second argument. b would be discarded. If you set the compiler flags tostd=c99
, the error is detected:Note that if you use Dymola on Windows, you are most likely using Visual Studio. In that case, there is no C99 support except the parts that were copied from C++.