[英]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.