【发布时间】:2015-09-22 06:51:35
【问题描述】:
问题陈述
我的代码的主要部分是 C(从 Python 调用)。 C 部分调用用 Fortran 编写的函数。使用错误代码和带有错误描述的错误字符串传播可能的错误。
问题是我似乎无法获得正确的接口来在 Fortran 中写入字符串并在 C 中读取/复制/操作它。下面的代码概述了我想要做的事情,标有 * ... * 的 cmets 表示需要扩展的地方。
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 例程?
-
要传递字符串,也可以看看这个问题:stackoverflow.com/q/9972743/577108
-
也可以作为参数传递给 Fortran 例程,但最后
error_string指针应该指向字符串。这与我如何从 Python 中读取它有关