问题说明
我的代码的主要部分是C(从Python调用)。 C部分调用Fortran编写的函数。使用错误代码和带有错误描述的错误字符串传播可能的错误。传播错误字符串:Fortran> C
问题是,我似乎无法获得正确的接口来在Fortran中编写字符串并在C中读取/复制/操作它。下面的代码概述了我想要执行的操作,带有* ... *
标记的注释表示在哪里需要扩展。
Ç
// global variable: read from Python if an error is encountered
char* error_string;
// template for the Fortan-subroutine
void fortran_calculation_(double* , int*);
int heavy_calculation(double* x)
{
int error_code;
// ... some code ...
// * should accept and write "error_string" *
fortran_calculation_(x , &error_code);
if (error_code)
{
error_string = "TO BE WRITTEN BY FORTRAN > REMOVE!!";
return 1;
}
// ... some code ...
return 0;
}
的Fortran
subroutine fortran_calculation_(x,error_code)
implicit none
! * include "error_string" as argument *
real*8 :: x
integer :: error_code
! ... some code ...
if (...) then
! * write "error_string" *
error_code = 1
return
end if
return
end subroutine
我试过很多东西,但我似乎无法得到它的工作...
你的错误字符串是否需要全局,或者它可以作为参数传递给你的Fortran例程? – Gilles
对于传递字符串,也看到这个问题:http:// stackoverflow。com/q/9972743/577108 – haraldkl
也可以作为参数传递给Fortran例程,但最后''error_string''指针应该指向字符串。这与我如何从Python读取 –