我有一堆fortran代码,我正在使用一些c ++代码。问题领域基本归结为:
调用fortran函数的C ++函数:
extern "C"
{
void CALX(float* w, int* nw, double* dw, int* idmx, int* lo);
}
void calcPort(float* w, int* nw, double* dw, int *idmx)
{
int lo[250]; //>100 used for internal stuff
//stuff
CALX(w, nw, dw, idmx, lo);
for(int i=0; i<100; ++i)
{
cout<<lo[i]<<", ";
if(i%50 == 49)
{
cout<<endl;
}
}
//more stuff
}
fortran函数的重要部分:
SUBROUTINE CALX(W,NW,DW,LO) bind(C, name="CALX") CALX-001
use iso_c_binding
C Lots o' comments
LOGICAL LO(250) CALX-160
DOUBLE PRECISION DW(1) CALX-161
C Bunch o' common blocks
1 READ (5,1000) TITLE CALX-175
C some miscellaneous stuff dealing with a couple special title strings
DO 4 I=1,100 CALX-184
4 LO(I)=.FALSE. CALX-185
READ (5,1001) (LO(I),I=1,50) CALX-186
WRITE (6,*) SIZEOF(LO(1))
WRITE (6,1001) (LO(I),I=1,50)
C IF LO(36)=.TRUE. RETURN TO RESTART A SEARCH SAVED ON TAPE 8 CALX-187
IF (LO(36)) RETURN CALX-188
READ (5,1001) (LO(I),I=51,100) CALX-189
WRITE (6,1001) (LO(I),I=51,100)
C More stuff
在我看来这应该有效。在调用函数之后,c ++代码应该以与fortran相同的模式读取0或非零。相反,我得到的是:
来自fortran写语句的这一位(与我重定向的输入相匹配):
FFFFFFTTFFTTFFFTFFFFFFFFFFFTFFTTFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFTTTFTTTFFFFFFFFFFFFFFFFFTTFFFFFFFF
我的循环中的这一点。
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
显然有一些沟通失败。为什么?我该如何解决?
答案 0 :(得分:2)
C ++调用中有五个参数。您的Fortran代码需要四个。
(请注意,USE ISO_C_BINDING
本身不会改变代码的行为。但BIND(C)确实会这样做。)