msm*_*zed 1 c++ fortran fortran-iso-c-binding
有没有办法传递一个C++对象用于Fortran 77?例如:
C23456
program main
write (*,*) 'Hello from FORTRAN 77!'
call readstep('cube.stp'//CHAR(0),myshape)
stop
end
Run Code Online (Sandbox Code Playgroud)
然后使用myshape作为C++对象,它将被保存在Fortran使用的内存中,并将其传递给实际使用它的其他C++函数?
编辑:这是C++代码:
extern"C" {
void readstep_(char*,void*);
}
void readstep_(char* inputFile, void* outShape){
STEPControl_Reader reader;
reader = STEPControl_Reader();
int succeed = reader.ReadFile(inputFile);
if(!succeed){
std::cout << "There was an error with the input file" << std::endl;
return;
}
reader.NbRootsForTransfer();
reader.TransferRoots();
TopoDS_Shape myShape = reader.OneShape();
TopoDS_Shape* myShapePtr = new TopoDS_Shape();
(*myShapePtr) = myShape;
outShape = myShapePtr;
return;
}
Run Code Online (Sandbox Code Playgroud)
请阅读标签/sf/ask/tagged/fortran-iso-c-binding/的标签说明,以获得更好的选择.那里有很多问题和答案.
我将使用星形符号作为常用扩展名.
C++:
class Obj{
};
extern "C" {
void hello_();
void readstep_(char* name, Obj** ptr){
*ptr = new Obj(); //use name in the actual process
}
void pass_it_(Obj** ptr){
hello_();
delete *ptr; //some usage of the object
}
}
Run Code Online (Sandbox Code Playgroud)
由于通过引用传递,它使用指向指针的指针.
FORTRAN:
program main
integer*8 myshape
call readstep('cube.stp'//CHAR(0),myshape)
call pass_it(myshape)
end
subroutine hello
write (*,*) 'Hello from FORTRAN 77!'
end subroutine
Run Code Online (Sandbox Code Playgroud)
使用integer*4一个32位平台上.(注意STOP语句没有理由)
编译:
g++ f77c++.f f77c++.C -lgfortran
Run Code Online (Sandbox Code Playgroud)
要么
gfortran f77c++.f f77c++.C -lstdc++
> ./a.out
Hello from FORTRAN 77!
Run Code Online (Sandbox Code Playgroud)