【发布时间】:2014-01-14 20:25:21
【问题描述】:
我有一个旧的 fortran 代码,出于各种原因,我正在修改它以提供使用 c++ 的功能。
在这段代码中有两个函数,CALC 和 CALC2。这些函数需要从c++中调用,并且相互调用。
CALC 的示意图:
SUBROUTINE CALC(W,NW,DW,IDMX,JUMP,POINT) bind(C, name="CALC")
use iso_c_binding
C Some statements
IF (LO(36)) CALL CALC2(W,NW,DW,IDMX, .TRUE., 14)
C More statements
END
CALC2 的草图:
SUBROUTINE CALC2(W,NW,DW,IDMX,JUMP,POINT) bind(C, name="CALC2")
use iso_c_binding
C Some statements
IF (LO(222)) CALL CALC(W,NW,DW,IDMX, .TRUE., 2)
C More statements
END
我当前的 main.cpp:
extern "C" {
void CALC(double *w, double *nw, double *dw, int *idmx, int* jump, int* point);
void CALC2(double *w, double *nw, double *dw, int *idmx, int* jump, int* point);
}
int main()
{
int length = 600000;
int jump = 0;
int point = 0;
double *w = new double[length];
CALC( w, w, w, &length, &jump, &point);
CALC2( w, w, w, &length, &jump, &point);
return 0;
}
运行我的 make 文件,一切都可以正确编译,但是在链接阶段我得到了这个:
g++ (big list of .o files) -lgfortran -o ecis
b1_calc.o: In function `CALC':
b1_calc.f:(.text+0x16b): undefined reference to `calc2_'
bq_calc.o: In function `CALC2':
bq_calc.f:(.text+0x1e52): undefined reference to `calc_'
bq_calc.f:(.text+0x1ec0): undefined reference to `calc_'
collect2: error: ld returned 1 exit status
make: *** [ecis] Error 1
为什么会这样,我该如何解决?
【问题讨论】:
-
我不确定,但我知道 CALC 和 CALC2 各自调用大量子例程,每个子例程都在另一个文件中定义,没有问题,据我所知,唯一的区别是我对这两个子例程进行了修改,使它们可以从 C++ 中调用。
标签: c++ gcc fortran gfortran fortran-iso-c-binding