Tag: fortran

c-fortran互操作性 – 带指针的派生类型

我有很长的fortran代码,必须从python中使用。 我决定做Fortran-> C-> Python接口。 我遇到了一个问题:我在包含的Fortran模块中派生了类型 double precision, allocatable 输入成员。 当我尝试用ifort编译时,我得到了(与gfortran类似): Each component of a derived type with the BIND attribute shall be a nonpointer, nonallocatable data component with interoperable type and type parameters 这实际上与英特尔编译器文档和Fortran 2003标准一致:第15.2.5点。 有没有办法访问带有allocatable或指针的Fortran“type”?

从c ++调用FORTRAN库的问题

当函数被放入用“ar rcs”创建的库中时,我遇到从C ++调用FORTRAN子例程的问题。 FORTRAN例程(tt.f90)是: Module A contains Subroutine SubIF2(ii) Integer*8, Intent(In) :: ii write(*,*) “hello”, ii End Subroutine SubIF2 End Module A 一个c ++调用者(testcpp.cpp)代码是 #include using namespace std; extern”C” { void __a_MOD_subif2(long long int *ii); } main(){ long long int ii=5; __a_MOD_subif2(&ii); return 0; } fortran调用者(testf.f90)代码是 Program test use A integer*8 :: i=1 call SubIF2(i) […]

将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)

从Fortran / C调用Python函数

我正在编写Fortran代码,我想使用Python库中的一些特殊函数和方法。 这是一个Python代码: from mpmath import * from scipy.optimize import * def g(A,B,t): return newton(lambda x: (x – A*polylog(1.5, B*exp(-t*x))), 0.0) 在fortran代码中,我想传递实数值A,B,t并获得g(A,B,t)的值(可能是复数)。 这也可以在C中完成。 我想提一下,将Python与其他语言混合对我来说是新鲜事。

如何从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, […]

C / C ++,FORTRAN,下划线和GNU Autotools

我有一个关于使用gcc和gfortran的混合语言编程(C / C ++和FORTRAN)的问题。 我搜索了很多“用语言X混合fortran”并且无法解决这个问题。 我不确定这是链接问题还是编译器问题,或两者兼而有之。 我创建了三个文件,我使用GNU Autotools构建原始应用程序,但应该能够独立于命令行构建应用程序。 C文件(main.c)将是驱动应用程序,它调用几个FORTRAN函数: /* this is a simple program */ #include /* add the extern function definition */ #include “fooonly.h” // this is not working for the mixed language programming stuff yet… /* this is the main program section */ int main( int argc, char *argv[] ) { int […]