【发布时间】:2016-02-17 06:31:27
【问题描述】:
我是编程新手,我想在我的 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(),我应该怎么做?
【问题讨论】:
-
这取决于编译器。您使用哪些编译器?
-
亲爱的@MSalters,我使用 gfortran 和 g++
-
你不调用普通块。它们包含数据,而不是代码。 “我该怎么做?”不是一个可以理解的问题,也没有证据表明这里存在实际问题。您的问题尚不清楚。
-
感谢@EJP 我更新了我的问题
-
但不是你的标题,而且仍然没有任何问题的证据。
标签: c++ gcc fortran gfortran fortran-common-block