-1

私はプログラミングを始めたばかりで、C++ コードで Fortran 77 共通ブロックを呼び出したいと考えています。実際、私のようなQ&Aをいくつか読んだことがありますが、あまり明確ではありませんでした....

この共通ブロックは、別の Fortran 77 サブルーチンによって定義されます。

サンプルコードは次のとおりです。

株式会社コモン:

!test common block:
real delta(5,5)
common /test/ delta
!save /test/ delta  ! any differences if I comment this line?

tstfunc.f

subroutine tstfunc()
    implicit none
    include 'common.inc'
    integer i,j
    do i = 1, 5
        do j = 1, 5
            delta(i,j)=2
            if(i.ne.j) delta(i,j)=0
            write (*,*) delta(i,j)
        end do
    end do
end

tst01.cpp

#include <iostream>

extern "C"
{
    void tstfunc_();
};

void printmtrx(float (&a)[5][5]){
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            std::cout<<a[j][i]<<'\t';
            a[j][i]+=2;
        }
        std::cout<<std::endl;
    }
}

int main()
{
//start...
    tstfunc_();
    printmtrx(delta);//here i want to call delta and manipulate it. 
    return 0;
}

delta(common.inc から) C++ 関数に渡したい場合printmtrx()、どうすればよいですか?

4

2 に答える 2

1

C の 2D 配列は行優先であるのに対し、FORTRAN では優先であることに注意してください。そのため、いずれかの言語で配列インデックスを切り替える必要があります。

于 2015-11-16T08:43:10.920 に答える
1

行/列優先の問題 (C コードでは 5x5 行列が転置されているように見える) を除けば、おそらく次のように進めることができます (このチュートリアルの共通ブロックに関するセクションを参照してください)。

tstfunc1.f

  subroutine tstfunc()
      implicit none
      real delta(5, 5)
      common /test/ delta
      integer i,j
      do i = 1, 5
          do j = 1, 5
              delta(i,j)=2
              if(i.ne.j) delta(i,j)=0
              write (*,*) delta(i,j)
          end do
      end do
  end

tst01.cc

#include <iostream>

extern "C" {
  void tstfunc_();
  extern struct{
    float data[5][5];
  } test_;
}

void printmtrx(float (&a)[5][5]){
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
          std::cout << a[i][j] << '\t';
          a[i][j] += 2;
        }
        std::cout << std::endl;
    }
 }

int main()
{
  //start...
  tstfunc_();

  printmtrx(test_.data);//here i want to call delta and manipulate it. 
  return 0;
}

次に、コンパイルするために:

gfortran -c -o tstfunc1.o tstfunc1.f    
g++ -o tst tst01.cc tstfunc1.o -lgfortran
于 2015-11-16T08:45:01.970 に答える