Fortran接口调用C函数返回一个指针 [英] Fortran interface to call a C function that return a pointer

查看:294
本文介绍了Fortran接口调用C函数返回一个指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C函数,

double* foofunc() 
{
  /* Function Body */
}

我不知道如何在的Fortran声明一个接口这一号召 C 功能。

此外,如果指针应该是指向 GPU设备内存,我怎么能定义在 Fortran语言界面?我是否需要使用 DEVICE 属性。

Also if a pointer is supposed to be pointing to GPU device memory, how could I define that in the Fortran interface? Do I need to use DEVICE attribute.

请使用由 Fortran语言支持的功能到2003年。

Please use features supported by Fortran up to 2003.

有什么建议?

推荐答案

既然你Fortran 2003的,最简单的方式接口Fortran和C是使用ISO C绑定。 (大多数的Fortran编译器95现在支持ISO C绑定,即使它们不是完整的Fortran编译器2003)。这是目前为止,比早先的答案,这是必要的,较早时期提出的复杂的方法更好 - 它是语言的一部分,因此便携,编译器和平台无关。但Fortran 2003的版本并不涉及过的可能性。 (接下来的Fortran将增加接口的C.更多的情况下),你可以很容易地通过一个参数是C指针 - 见好就收掀起的价值atttribute了Fortran声明。指针的指针需要C_PTR。我不知道一个指针作为函数返回......我会尝试当我有时间。如果你有,做一个平凡ç胶例程转换函数的返回到一个参数的指针 - 这种情况下,很容易

Since you have Fortran 2003, the easy way to interface Fortran and C is to use the ISO C Binding. (Most Fortran 95 compilers now support the ISO C Binding, even if they aren't full Fortran 2003 compilers.) This is far, far better than the complicated methods suggested in an earlier answer, which were necessary in an earlier era -- it is part of the language and therefore portable, and compiler and platform independent. But the Fortran 2003 version doesn't cover ever possibility. (The next Fortran will add additional cases of interfacing to C.) You can easily pass an argument that is a C pointer -- just leave off the "value" atttribute in the Fortran declaration. A pointer to a pointer needs C_PTR. I don't know about a pointer as a function return... I will have to experiment when I have time. If you have to, make a trivial C glue routine that converts the pointer of the function return into an argument -- that case is easy.

重新指向到GPU设备内存 - 除非你的编译器有一个非标准的功能,它不会有设备。也许,挥发性会有帮助吗?创建一个适当的用户定义类型...

Re a pointer to GPU device memory -- unless your compiler has a non-standard feature, it won't have "DEVICE". Perhaps "volatile" will help? Create an appropriate user-defined type...

有下互操作性与C的gfortran手册的例子。由于这是语言的一部分,这个文件应该帮助,即使你不使用gfortran。

There are examples in the gfortran manual under "Interoperability with C". Since this is part of the language, this documentation should help even if you aren't using gfortran.

这篇关于Fortran接口调用C函数返回一个指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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