問題說明
我的代碼的主要部分是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讀取 –