I am a fresh in programming and I want to call a Fortran 77 common block in my C++ code. Actually I have read some Q&A similar like mine, but I was not very clear....
This common block is defined by another Fortran 77 subroutine.
Sample code is:
common.inc:
!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;
}
If I want to pass delta (from common.inc) to the C++ function printmtrx(), what should I do?
Apart from the row/column-major order issue (the 5x5 matrix would appear transposed in the C-code), perhaps you could proceed as follows (see section on Common blocks in this tutorial):
tstfunc1.f
tst01.cc
Then in order to compile: