[英]How C++ call Fortran 77's common blocks
我剛開始編程,我想在我的C ++代碼中調用Fortran 77通用塊。 其實我已經讀過類似我的問答集,但是我不太清楚。
此公共塊由另一個Fortran 77子例程定義。
示例代碼為:
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;
}
如果我想將delta
(來自common.inc)傳遞給C ++函數printmtrx()
,該怎么辦?
除了行/列的主要順序問題(5x5矩陣將在C代碼中轉置)之外,您還可以按照以下步驟進行操作(請參閱本教程中“公共塊”部分):
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.