将FORTRAN对象传递给C,反之亦然 [英] Passing a FORTRAN object to C and vice versa

查看:334
本文介绍了将FORTRAN对象传递给C,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Fortran对象,即

  this%object%a 

this%object% b

this%object%c

我想将它传递给用C编写的代码,我主要是一个FORTRAN程序员,我对C的暴露很少。我使用 iso_c_binding 传递整数和数组,但现在我需要传递对象。



我以如下方式定义对象:

  TYPE对象

INTEGER :: a

INTEGER :: b

INTEGER :: c

END TYPE object


解决方案

您可以创建可互操作的类型:

 使用iso_c_binding 

TYPE,BIND(C):: object

INTEGER(c_int):: a

INTEGER(c_int):: b

INTEGER(c_int):: c

结束类型对象

类型(对象):: o

对象的标准有一些限制。例如,它不能包含可分配或指针组件。



当您将它传递给可互操作的过程时:

<$ p

$ b $子程序sub(x)bind(C,name =sub)
类型(object),$ p> void sub(c_object * x){} intent(inout):: x
结束子程序

调用子(o)

它可以与C结构互操作

$ $ $ $ $ $ c $ typedef struct {
int a;
int b;
int c;
} c_object;

您也可以将不可互操作的类型传递给C,但您必须使用指针:

  subroutine sub2(x)bind(C,name =sub)
type(c_ptr),value :: x
结束子程序

调用sub2(loc(o))


I have my Fortran object i.e.

this%object%a

this%object%b

this%object%c

I want to pass it to a code written in C, I am predominately a FORTRAN programmer and I have had very little exposure to C. I am using iso_c_binding to pass integers and arrays but now I need to pass objects.

I define the object in the following way

    TYPE object

         INTEGER                                  :: a

         INTEGER                                  :: b

         INTEGER                                  :: c

    END TYPE object

解决方案

You can make interoperable types:

use iso_c_binding

TYPE, BIND(C) :: object

     INTEGER(c_int)                                :: a

     INTEGER(c_int)                                :: b

     INTEGER(c_int)                                :: c

END TYPE object

type(object) :: o

There are restrictions in the standard on the object. For example, it cannot contain allocatable or pointer components.

When you pass it to an interoperable procedure:

void sub(c_object* x){}

subroutine sub(x) bind(C,name="sub")
  type(object), intent(inout) :: x
end subroutine

call sub(o)

it is interoperable with a C struct

typedef struct {
  int a;
  int b;
  int c;
} c_object;

You can also pass non-interoperable types to C, but you have to use pointers:

subroutine sub2(x) bind(C,name="sub")
  type(c_ptr), value :: x
end subroutine

call sub2(loc(o))

这篇关于将FORTRAN对象传递给C,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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