[英]Using a C++ class object in fortran 77
是否可以通过C ++对象与Fortran 77一起使用? 例如:
C23456
program main
write (*,*) 'Hello from FORTRAN 77!'
call readstep('cube.stp'//CHAR(0),myshape)
stop
end
然后将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;
}
请阅读标签https://stackoverflow.com/questions/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
}
}
由于通过引用传递,因此它使用指向指针的指针。
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
在32位平台上使用integer*4
。 (请注意没有理由使用STOP语句)
编译:
g++ f77c++.f f77c++.C -lgfortran
要么
gfortran f77c++.f f77c++.C -lstdc++
> ./a.out
Hello from FORTRAN 77!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.