Modelica 外部函数:C 与 C99 [英] Modelica external functions: C versus C99

查看:45
本文介绍了Modelica 外部函数:C 与 C99的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Modelica 中,可以定义外部函数.
规范的第 12.9 章说支持 C 和 Fortran77,
将来可能会支持 C++ 和 Fortran90.
现在我想知道支持哪些 C 版本?

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?

特别是我需要 C99 中可用的对数伽马函数,所以我尝试了以下操作:

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;

但它不起作用,而 powf 起作用:

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;

这可能是因为 powf 在 C 中可用,而 lgamma 在 C99 中引入.
但这是否是 Modelica、Dymola 或我的编译器的限制?
有没有办法让 C99 外部函数工作?
C 数学运算的维基百科列表上,有一些更有趣的函数,如误差函数 erf 和 erfc,这些也很好.

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.

推荐答案

您只能假设 C89/90 代码在 Modelica 编译器中编译.不过,这主要与语法有关(如果您使用 Include 注释或 Library="file.c").

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

可用的函数主要取决于编译器链接的 C 库.我猜微软的 C 库不包含 lgamma,所以它不能被链接.在 Linux/OpenModelica 上,lgamma 示例确实有效,因为 libm 包含该函数(它使用 c90 模式编译,但隐式添加了 double lgamma(double)代码>声明).

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 as libm contains the function (it's compiling using c90 mode, but implicitly adding a double lgamma(double) declaration).

powf 示例也可以编译,但无法正常工作,因为您的 external "C" 声明声明它使用双精度浮点数(并且您不能将其更改为Modelica 3.2).powf 将读取 a 的一半并将其用作第一个参数,然后读取 a 的后半部分并将其用作第二个参数.b 将被丢弃.如果将编译器标志设置为 std=c99,则会检测到错误:

The powf example also compiles, but does not work correctly since your external "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 to std=c99, the error is detected:

powf.h:23:15: 错误:‘powf’的类型冲突

powf.h:23:15: error: conflicting types for ‘powf’

请注意,如果您在 Windows 上使用 Dymola,则很可能使用的是 Visual Studio.在这种情况下,除了从 C++ 复制的部分之外,没有 C99 支持.

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++.

这篇关于Modelica 外部函数:C 与 C99的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆