Tag: fortran iso c binding

将Fortran数组传递给C

我在将Fortran数组传递给C程序时遇到了很多麻烦。 从我以前的post收集到的是包含界面。 这摆脱了我的一些问题。 但是,我似乎无法弄清楚如何正确传递这些数组或在C内正确访问它们的值。 program f_call_c implicit none interface subroutine cfun(x,len) bind( c ) use,intrinsic :: iso_c_binding implicit none integer( c_int) :: len real(c_double) :: x(0:3,0:len) end subroutine cfun subroutine vec(r,len) bind(c) use,intrinsic :: iso_c_binding implicit none integer(c_int) :: len real(c_double) :: r(0:len) end subroutine vec end interface double precision, allocatable :: x(:,:),r(:) integer :: […]

Fortran派生类型包含可从C访问的派生类型

作为这篇文章的扩展,我已经派生出了具有成员派生类型的类型。 示例如下: module simple use iso_c_binding TYPE SIMPLEF INTEGER :: A INTEGER, POINTER :: B, C(:) END TYPE SIMPLEF TYPE COMPLEXF INTEGER :: X TYPE (SIMPLEF) :: Y END TYPE COMPLEXF end module simple 如上文所述,目标是在C中具有类似的派生类型,并且能够将值来回传递给Fortran。 解决方案可以在这里看到。 然而,这里它不仅仅是一个派生类型,它是一个派生类型,其成员本身就是派生类型。 我是否需要为每个Y成员创建COMPLEXF子程序,即SETY_A,QUERYY_A,SETY_B,QUERYY_BSIZE,SQUERYY_B等? 或者有更好的方法来解决这个问题吗?

将指针从C传递给fortran子程序

我试图从C调用fortran子程序,我可以在C中分配并安全地将指针传递给Fortran吗? 子程序中的数组是自动数组(x(nmax))。 (我正在分配x然后将其传递给fortran)

如何从C头文件准备fortran模块?

我有这个纯C文件,有6个函数,我想为fortran程序员提供: http://tinyfiledialogs.sourceforge.net C到Fortran可以“导入”吗? 我应该简单地准备类似Fortran头文件的东西吗? 我意识到我的C代码使用了几个Unix或Windows C头文件,这会使转换复杂化。 但是,确实为6 C函数提供等效的Fortran头必须是可以实现的。 char const * tinyfd_inputBox ( char const * const aTitle , char const * const aMessage , char const * const aDefaultInput ) ; // the returned value is a static array 编辑:我开始意识到模块实际上是依赖于编译器的。 也许我应该准备一个makefile来生成C对象文件和fortran模块来使用它。

在C中使用Fortran代码

我尝试在C中使用fortran例程,但我不工作。 我不知道我犯了什么错误。 这里是我的Fortran代码,包括我想在C中使用的Integration-Module: module integration implicit none contains function Integrate(func, a,b, intsteps) result(integral) interface real function func(x) real, intent(in) :: x end function func end interface real :: integral, a, b integer :: intsteps intent(in) :: a, b, intsteps optional :: intsteps real :: x, dx integer :: i,n integer, parameter :: rk = kind(x) […]

如何将C和Fortran的字符串数组传递给Fortran?

我试图将一个字符串数组从C传递给Fortran子程序以及从Fortran传递到同一个Fortran子程序。 我已成功地从C和Fortran传递单个字符串(即1D字符数组)。 但是,我遇到了字符串数组的问题。 我在Fortran端使用ISO C绑定,理想情况下我希望它在呼叫方面尽可能无缝。 我已经阅读了一些相关的问题和答案。 一些(即这个和这个 )只是“使用ISO C”而没有进一步的细节,这没有多大帮助。 这个答案非常有用(对不同问题的类似答案),但仅适用于单个字符串,其中似乎在单个Fortran字符串中识别c_null_char。 如果没有两个单独的例程,我无法弄清楚如何处理数组的情况。 我目前拥有的是一个C例程,我想传递字符串数组( string ): #include extern “C” void print_hi_array(char input_string[][255]); using namespace std; int main() { char string[3][255] = {“asdf”,”ghji”,”zxcv”}; print_hi_array(string); return 0; } 并且,类似的Fortran例程: program main implicit none call print_hi_array( (/”asdf”, “ghji”, “zxcv”/) ) end program 到目前为止,这就是我对接收端的看法: subroutine print_hi_array(input_string) bind(C) use iso_c_binding, only: C_CHAR, […]

Fortran-C互操作性和浮点数组

我有一个很大的现有Fortran95代码。 它用 real(dp), dimension(num) :: array 声明数组。 我想加入一些C代码,发现我可以通过将接口写入C函数并将数组声明为 use iso_c_binding real(c_double), allocatable, target :: array(:) 我有工作的fortran函数,它们将C函数称为 call myfunction(c_loc(array)); 将real(dp)数组传递给myfunction需要什么? 显然,我需要从它做一个C指针(如何?)。 除了复制数组之外还有其他方法吗? 是否可以确保两种类型确实引用兼容的双精度数据块? 最重要的是,该解决方案必须与GNU编译器一起使用。 注意,在现有的Fortran代码中用real(c_double)替换real(dp)并不是我现在的选择。 如果没有其他方法可以复制整个arrays,我将如何在界面中正确执行此操作?

从C ++拦截Fortran STOP

我为传统的Fortran库准备了一个C ++接口。 遗留库中的一些子例程遵循一个丑陋但可用的状态代码约定来报告错误,我使用这样的状态代码从我的C ++代码中抛出一个可读的exception:它工作得很好。 另一方面,有时遗留库会调用STOP (终止程序)。 即使条件可以恢复,它也经常这样做。 我想从C ++中捕获这个STOP ,到目前为止我一直没有成功。 以下代码很简单,但完全代表了手头的问题: Fortran遗留库fmodule.f90 : module fmodule use iso_c_binding contains subroutine fsub(x) bind(c, name=”fsub”) real(c_double) x if(x>=5) then stop ‘x >=5 : this kills the program’ else print*, x end if end subroutine fsub end module fmodule C ++接口main.cpp : #include // prototype for the external Fortran […]

如何在C中访问(动态分配)Fortran数组

我的主要问题是为什么数组做了如此奇怪的事情以及是否有任何方式以“干净”的方式执行以下操作。 我目前有一个C程序foo.c通过dlopen/dlsym连接Fortran程序bar.f90 ,大致类似于下面的代码: foo.c的: #include #include int main() { int i, k = 4; double arr[k]; char * e; void * bar = dlopen(“Code/Test/bar.so”, RTLD_NOW | RTLD_LOCAL); void (*allocArray)(int*); *(void **)(&allocArray) = dlsym(bar, “__bar_MOD_allocarray”); void (*fillArray)(double*); *(void **)(&fillArray) = dlsym(bar, “__bar_MOD_fillarray”); void (*printArray)(void); *(void **)(&printArray) = dlsym(bar, “__bar_MOD_printarray”); double *a = (double*)dlsym(bar, “__bar_MOD_a”); for(i = […]

从Fortran调用C函数,其中C函数名最初是从C传入的

由于不相关的原因,我需要将一个C / C ++函数名称传递给一个Fortran子程序,该子程序又调用该C函数。 我发现我可以成功地将函数名称传递给Fortran子例程。 在那个子程序中,我可以调用正确的C函数。 但是,C函数的参数在此调用中被破坏(当直接从C调用时它工作正常)。 我已经使用ISO C Binding试图让它工作,但无济于事。 这是一个MWE: fortranRoutine.h: extern “C” { void fortranRoutine_(void(int status)); }; calledfromFortran.h: void calledfromFortran(int status); main.cpp中: #include “fortranRoutine.h” #include “calledfromFortran.h” using namespace std; int main(int argc, char** argv) { calledfromFortran(12); fortranRoutine_(calledfromFortran); return 0; } fortranRoutine.f90: subroutine fortranRoutine(calledfromFortran) use iso_c_binding implicit none interface subroutine calledfromFortran(status) bind (c) use […]