module中的函数,和普通的外部函数一样的。直接写 Bind( C , Name ) 就可以了。
你目前的设计,最难的地方在于,你要在 type singleinfo 结构中包含 allocatable 的数组 pointchain ,这是比较困难的。
这一实现,由于fortran和C均没有标准的底层实现。所以标准的混编也就无法实现。
一般的做法是,把allocatable数组单独放在外面当做另外的参数进行传递。fortran不必关心它在C++里是静态的还是动态分配的。
排除这几个数组之外,我写了一个范例给你:
[C++] 纯文本查看 复制代码 #include <iostream>
struct singleinfo{
int controlno;
int unittype;
int solid;
double coearea;
double volume;
double center[3];
int pointcount , trianglecount;
};
extern "C" {
void cylindermeshing( double & , double & , struct singleinfo * );
double thirddet( double * );
}
using namespace std;
int main()
{
struct singleinfo cylinderinfo;
double r = 3.0;
double h = 4.0;
cylinderinfo.controlno = 1;
cylinderinfo.unittype = 2;
cylinderinfo.solid = 3;
cylinderinfo.coearea = 4;
cylinderinfo.volume = 5;
cylinderinfo.unittype = 6;
cylinderinfo.pointcount = 7;
cylinderinfo.trianglecount = 8;
cylinderinfo.center[0] = 11;
cylinderinfo.center[1] = 11;
cylinderinfo.center[2] = 11;
cylindermeshing(r, h, &cylinderinfo);
double deta[3][3];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
deta[j][i] = j*4 + i;
}
}
cout << thirddet( &deta[0][0] );
}
[Fortran] 纯文本查看 复制代码 module hydromodelUtil
implicit none
double precision, parameter :: pi=3.141593d0
type , Bind( C ) :: singleinfo
integer :: controlno
integer :: unittype
integer :: solid
double precision :: coearea
double precision :: volume
double precision, dimension(3) :: center
integer :: pointcount, trianglecount
end type singleinfo
contains
subroutine cylindermeshing(r, h, cylinderinfo) Bind(C,Name="cylindermeshing")
implicit none
double precision :: r, h
type(singleinfo) :: cylinderinfo
write(*,*) r , h , cylinderinfo
end subroutine cylindermeshing
function thirddet(deta) Bind( C , Name = "thirddet" )
double precision, dimension(3,3) :: deta
double precision :: thirddet
thirddet=deta(1,1)*(deta(2,2)*deta(3,3)-deta(3,2)*deta(2,3)) &
& -deta(2,1)*(deta(1,2)*deta(3,3)-deta(3,2)*deta(1,3)) &
& +deta(3,1)*(deta(1,2)*deta(2,3)-deta(2,2)*deta(1,3))
end function thirddet
end module hydromodelUtil |