我有一个用Fortran编写的迭代线性求解器,想用C / C ++调用它,源代码可以在
找到。https://github.com/fangq/blit/
其中src/
中有Fortran求解器代码。但是,简单的C测试代码无法正确终止,并且会引发错误
**stack smashing detected ***: <unknown> terminated**
返回之前。
可以在以下位置找到C代码
https://github.com/fangq/blit/blob/master/cpp/example/passdata.c 并且代码也列在下面:
#include <stdio.h>
#include "blit_solvers.h"
int main(){
BLQMRSolver qmr;
int size=100;
ZBLQMRCreate(&qmr,&size);
ZBLQMRPrint(&qmr);
qmr.nrhs=8;
qmr.maxit=1000;
qmr.state=-1;
qmr.dopcond=1;
qmr.isquasires=0;
qmr.debug=1;
ZBLQMRPrint(&qmr);
return 1;
}
我仅定义一个C结构,将其传递给Fortran以设置初始值(ZBLQMRCreate
),然后在Fortran中打印更新值(ZBLQMRPrint
)。
将C结构与Fortran原型一起传递给Fortran函数
type, bind(c) :: BLQMRSolver
integer(c_int) :: n, nrhs, maxit, state, dopcond, flag, iter, isquasires, debug
real(c_double) :: qtol, droptol, res, relres ! convergence tolerance
type (ILUPcond) :: ilu ! private ILU preconditioner
end type BLQMRSolver
打印功能确实显示值已正确修改,但是,代码的最后一行触发了上述堆栈粉碎错误。即使我注释掉ZBLQMRCreate
下面的所有行,我仍然遇到相同的错误。瓦尔格朗德给我这个消息
==19135== Command: bin/passdata
*** stack smashing detected ***: <unknown> terminated
==19135==
==19135== Process terminating with default action of signal 6 (SIGABRT)
==19135== at 0x6129E97: raise (raise.c:51)
==19135== by 0x612B800: abort (abort.c:79)
==19135== by 0x6174896: __libc_message (libc_fatal.c:181)
==19135== by 0x621FCD0: __fortify_fail_abort (fortify_fail.c:33)
==19135== by 0x621FC91: __stack_chk_fail (stack_chk_fail.c:29)
==19135== by 0x1099F5: main (passdata.c:21)
...
我尝试将Fortran数据结构(type BLQMRSolver
)更改为使用iso_c_binding
和c_int/c_double
作为成员,但是仍然没有任何改变。
有关如何解决此问题的任何建议,将不胜感激!
重现此错误的命令(需要先安装gfortran / gcc / lapack / umfpack):
git clone https://github.com/fangq/blit.git
cd blit/src
make clean
make static
cd ../cpp/example
make
./bin/passdata