问题描述
有没有办法传递一个C ++对象使用Fortran 77?例如:
Is there a way to pass a C++ object to use with Fortran 77? For example:
C23456
program main
write (*,*) 'Hello from FORTRAN 77!'
call readstep('cube.stp'//CHAR(0),myshape)
stop
end
然后使用myshape作为一个C ++对象,它只保存在Fortran使用的内存中,并将它传递给其他实际使用它的C ++函数?
and then use the myshape as a C++ object which will just be kept in memory used by Fortran and to just pass it to other C++ functions that will actually use it?
编辑:这是C ++代码:
Here is the C++ code:
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;
}
推荐答案
请阅读标签为更好的选择。
Please read tag description of the tag http://stackoverflow.com/questions/tagged/fortran-iso-c-binding for much better options. And the many questions and answers there.
我将使用星号表示法作为公共扩展名。
I will use the star notation as a common extension.
:
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
(注意没有停止语句的原因)
Use integer*4
on a 32-bit platform.(note there is no reason for the STOP statement)
编译:
g++ f77c++.f f77c++.C -lgfortran
或
gfortran f77c++.f f77c++.C -lstdc++
> ./a.out
Hello from FORTRAN 77!
这篇关于在fortran 77中使用C ++类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!