我想建立一个工作流程,在Windows机器上使用Cython从Python到达fortran例程
经过一番搜索,我找到了:http: //www.fortran90.org/src/best-practices.html#interfacing-with-c和https://stackoverflow.com/tags/fortran-iso-c-binding/info
和一些代码pices:
Fortran方面:
pygfunc.h:
void c_gfunc(double x, int n, int m, double *a, double *b, double *c);
Run Code Online (Sandbox Code Playgroud)
pygfunc.f90
module gfunc1_interface
use iso_c_binding
use gfunc_module
implicit none
contains
subroutine c_gfunc(x, n, m, a, b, c) bind(c)
real(C_FLOAT), intent(in), value :: x
integer(C_INT), intent(in), value :: n, m
type(C_PTR), intent(in), value :: a, b
type(C_PTR), value :: c
real(C_FLOAT), dimension(:), pointer :: fa, fb
real(C_FLOAT), dimension(:,:), pointer :: fc
call c_f_pointer(a, fa, (/ n /))
call …Run Code Online (Sandbox Code Playgroud) 有没有人知道在Fortran中以一定的毫秒数睡眠的方法?我不想使用非便携式系统调用,因此Fortran或C库固有的任何东西都是首选.
我想从Python中的Fortran共享库中调用一些函数.我在网上找到了一些链接并阅读它们,根据我的发现,我应该这样做
libadd = cdll.LoadLibrary('./libbin.so')
Run Code Online (Sandbox Code Playgroud)
加载共享对象.但是,此共享对象包含来自另一个共享库的一些符号.我阅读了cdll的帮助,但似乎无法同时加载多个共享对象文件.我怎样才能调用这个Fortran库中的函数,这个库很可能是由英特尔Fortran编译器编译的?
我试图从C调用FORTRAN函数
我的问题是:
如果fortRoutine是我的fortran子程序的名称,那么我从C中调用它fortRoutine_.如果fortRoutine只包含一个字符数组参数,那么我可以像这样传递:
fortRoutine_("I am in fortran");
Run Code Online (Sandbox Code Playgroud)在调用FORTRAN子例程时,何时应该使用pass by值和何时通过引用传递?
由于我是C的新手,我对此并不了解.如果可能的话,请提供一些很好的教程链接.
我正在编写将使用Fortran的C互操作性机制从Fortran调用C函数的代码(在Fortran 2003中引入并在较新版本的gfortran和ifort中实现).
这个答案几乎就是我所需要的,但是我不能完全理解我应该在Fortran中使用哪个接口声明来获得一个看起来像这样的C函数:
int use_array(int n, char * array[]){
int i;
for(i=0; i<n; i++){
printf("Item %d = %s\n",i,array[i]);
}
return n;
}
Run Code Online (Sandbox Code Playgroud)
我不清楚Fortran端的接口应该是什么声明:
interface
function use_array(n, x) bind(C)
use iso_c_binding
integer (c_int) use_array
integer (c_int), value :: n
character(c_char) WHAT_SHOULD_GO_HERE? :: x
end function use_array
end interface
Run Code Online (Sandbox Code Playgroud)
我知道我也必须处理空终止问题.
现在,我已经对此持续了大约一个星期,并且在论坛之后搜索了论坛,以便清楚地解释如何从C发送char*到FORTRAN.为了让事情更令人沮丧,从FORTRAN向C发送char*参数是直截了当的......
从FORTRAN发送char*参数到C(这很好):
// The C header declaration (using __cdecl in a def file):
extern "C" double GetLoggingValue(char* name);
Run Code Online (Sandbox Code Playgroud)
来自FORTRAN:
! The FORTRAN interface:
INTERFACE
REAL(8) FUNCTION GetLoggingValue [C, ALIAS: '_GetLoggingValue'] (name)
USE ISO_C_BINDING
CHARACTER(LEN=1, KIND=C_CHAR), DIMENSION(*), INTENT(IN) :: name
END FUNCTION GetLoggingValue
END INTERFACE
! Calling the function:
GetLoggingValue(user_name)
Run Code Online (Sandbox Code Playgroud)
当尝试使用类似逻辑从C返回char*时,我遇到问题后出现问题.我觉得应该尝试的一个尝试是:
// The C declaration header (using __cdecl in a def file):
extern "C" const char* GetLastErrorMessage();
Run Code Online (Sandbox Code Playgroud)
和FORTRAN接口:
INTERFACE
FUNCTION GetLastErrorMessage [C, ALIAS: '_GetLastErrorMessage'] ()
USE ISO_C_BINDING
CHARACTER(LEN=1, KIND=C_CHAR), DIMENSION(255), :: …Run Code Online (Sandbox Code Playgroud) 如何在使用可选参数的C++头中引用Fortran函数?我可以在标题中为每个可能的呼叫组合设置原型吗?或者这甚至可能吗?
例如,Fortran:
subroutine foo(a, b, c) bind(c)
real, intent(in), optional :: a, b, c
...
end subroutine foo
Run Code Online (Sandbox Code Playgroud) 有没有使用Fortran 2003标准的内部模块从C库头自动生成Fortan 绑定的工具iso_c_bindings?
我对将 C 转换为Fortran 不感兴趣,但只生成绑定.
我习惯了Fortran,我使用namelist顺序读入来从文件中获取变量.这允许我有一个看起来像这样的文件
&inputDataList
n = 1000.0 ! This is the first variable
m = 1e3 ! Second
l = -2 ! Last variable
/
Run Code Online (Sandbox Code Playgroud)
我可以通过它的名称命名变量,然后分配一个值以及后面的注释来说明变量实际是什么.装载非常简单
namelist /inputDataList/ n, m, l
open( 100, file = 'input.txt' )
read( unit = 100, nml = inputDataList )
close( 100 )
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,C中有类似的东西吗?或者我是否必须通过在'='处切断字符串来手动执行此操作?
我试图将数组从C/C++传递到Fortran 2003模块,并将计算值返回到C/C++.我已经能够传递并返回单个值(标量),但是来回获取数组很难.我在标量值上发现了很多线程,并且我已经成功地完成了这些工作.
在我的工作标量函数之后,我已经模拟了基于数组的函数.
我正在使用gcc/gfortran.
这是Fortran模块(ConvertUnitsLib.f03).
module ConvertUnitsLib
use :: iso_c_binding ! for C/C++ interop
real(c_double), bind(c) :: degF, degC
public DegCtoF
contains
!
! Convert temperature degrees Celsius Fahrenheit
!
real(kind = c_double) function DegCtoF(degC) result(degF) &
& bind(c, name = "DegCtoF")
real(c_double), intent(in), dimension(:) :: degC
real(c_double), dimension(size(degC)) :: degF
do i = 1, size(degC)
degF(i) = ( degC(i) * 1.8 ) + 32
end do
end function DegCtoF
! End of module
end module ConvertUnitsLib
Run Code Online (Sandbox Code Playgroud)
和C/C++,(CFort.cpp) …