C可变参数函数与Fortran的互操作性 [英] Interoperability of C variadic function and Fortran

查看:141
本文介绍了C可变参数函数与Fortran的互操作性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以声明C可变参数函数并从Fortran中调用它? 我需要调用此函数来计算用字符串标记的向量之间的一些点积. 我的想法是声明如下内容,其中变量的变量列表包含字符串文字.如果参数的变量列表为空,那么我将在标准标签中进行查找并执行计算.如果用户指定了两个标签,我将检索这两个向量并获得其点积:

Is there a way to declare a C variadic function and call it from Fortran? I need to call this function to compute some dot products between vectors labeled with a string. My idea was to declare something like the following, where the variable list of arguments contains string literals. If the variable list of arguments is empty, then I would do a lookup among the standard labels and perform the calculation. If the user specified two labels I would retrieve those two vectors and get their dot product:

extern "C" void compute_dot_product(double * dot_product, ...)
{
    va_list args;
    va_start(args, NULL);
    char * label1 = va_arg(args, char *);
    if (!label1)
    {
       // Do standard label lookup and compute dot product
    }
    else
    {
       // Compute dot product between the vectors with the specified labels
       char * label2 = va_arg(args, char *);
    }
    va_end(args);
}

唯一的问题是我可以编译C库并将其链接到Fortran可执行文件,但是当我尝试访问变量的变量列表时出现运行时错误. 知道我要做什么是可能的吗? 然后,可能的解决方案是将其拆分为两个函数:一个执行标准标签查找(0个参数的情况),另一个处理非标准标签查找(2个参数的情况).我宁愿避免这种解决方案.

the only problem is that I can compile my C library and link it to a Fortran executable, but I get a runtime error when I try to access the variable list of arguments. Any idea if what I am trying to do is possible? A possible solution would then be to split into two functions: one that does the standard label lookup (0 argument case), the other that handles the non-standard label lookup (2 argument case). I would rather avoid this solution though.

推荐答案

无法以符合标准(即可移植)的方式调用可变参数函数.

It is not possible to call a variadic function in a standard conforming (i.e. portable) way.

您可以使C函数的定义仅采用两个参数(因此不再是可变参数-该函数的现有引用将需要修改),而C函数中的第二个参数为指针,或者为NULL即可.表示没有任何其他东西在传递,也没有指向指针数组(也许是NULL终止)或其他东西.在F201X中,此类函数的接口主体可以将OPTIONAL属性用作第二个参数.

You could make the definition of the C function take two parameters only (so it is no longer variadic - existing references to the function would need to be modified), with the second parameter in the C function a pointer, either NULL to indicate that there are no additional things being passed, or pointing at an array of pointers (perhaps NULL terminated) or whatever, with any additional things. In F201X an interface body for this sort of function might be able to use the OPTIONAL attribute for the second argument.

这篇关于C可变参数函数与Fortran的互操作性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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