简体   繁体   English

在Fortran 77中使用C ++类对象

[英]Using a C++ class object in fortran 77

Is there a way to pass a C++ object to use with Fortran 77? 是否可以通过C ++对象与Fortran 77一起使用? For example: 例如:

C23456
      program main
      write (*,*) 'Hello from FORTRAN 77!'
      call readstep('cube.stp'//CHAR(0),myshape)
      stop
      end

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? 然后将myshape用作C ++对象,该对象将仅保存在Fortran使用的内存中,并将其传递给将实际使用它的其他C ++函数?

EDIT: Here is the C++ code: 编辑:这是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;
}

Please read tag description of the tag https://stackoverflow.com/questions/tagged/fortran-iso-c-binding for much better options. 请阅读标签https://stackoverflow.com/questions/tagged/fortran-iso-c-binding的标签说明,以获得更好的选择。 And the many questions and answers there. 还有那里的许多问题和答案。

I will use the star notation as a common extension. 我将使用星号表示法作为通用扩展名。

C++: 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
  }

}

It uses pointer to pointer because of the pass by reference. 由于通过引用传递,因此它使用指向指针的指针。

fortran: 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

Use integer*4 on a 32-bit platform. 在32位平台上使用integer*4 (note there is no reason for the STOP statement) (请注意没有理由使用STOP语句)

compile: 编译:

g++ f77c++.f f77c++.C -lgfortran

or 要么

gfortran f77c++.f f77c++.C -lstdc++

> ./a.out 
 Hello from FORTRAN 77!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM