问题描述
我正在处理一些fortran-calling-C代码,但不清楚使用iso_c_binding模块.
I am working on some fortran-calling-C code and am unclear about the use of the iso_c_binding module.
我的fortran和C接口在没有 iso_c_binding的情况下成功运行了,而问题是我是否仍应显式绑定函数和变量.例如,这有效:
I have fortran and C interfaces working successfully without iso_c_binding, and the question is if I should still explicitly bind functions and variables. For example, this works:
program testprog
...
interface
subroutine c_parser(param)
integer, intent(in) :: param
end subroutine
end interface
integer :: a
call c_parser(a)
..
end program
/****************/
void c_parser_ (int* param)
因此,将下划线附加到C函数,为其编写接口,然后从fortran程序中调用它.我不使用指针或可分配指针,我所有的代码都具有int,char,float和逻辑,它们需要从fortran子例程移至C.
So append an underscore to a C function, write an interface to it, and call it from a fortran program.I dont use pointers or allocatables, all my code has ints, char, float and logical that need to be moved from a fortran subroutine to C.
iso_c_binding的确切用途是什么?有陷阱吗?例如,此提到了通过绑定使用字符串时的注意事项(请参阅不幸的是,至少在GNU和Intel编译器上,该语句"部分.)
What exact purpose does iso_c_binding serve? Are there any gotchas? As an example, this mentions a caveat when using strings through the binding (see the part "Unfortunately, on at least the GNU and Intel compilers, the statement").
推荐答案
问题中的工作"方法本质上是特定于Fortran处理器的.由于历史惯例,它在某些操作系统上是常见的安排,但绝不是普遍存在的.在编译器系列中,调用约定的各个方面将随编译选项而变化,并且随编译器版本而变化,其方式可能会破坏该方法.
The "working" approach in the question is inherently Fortran processor specific. It is a common arrangement on some operating systems due to historical convention, but it is by no means ubiquitous. Within a compiler family, aspects of the calling convention will vary with compile options and have varied with compiler version in a way that may break that approach.
ISO_C_BINDING只是一个提供一些常量,某些类型和某些过程的模块.它恰好是由标准指定并由编译器供应商提供的(它是一个固有模块),但是否则它没有特殊功能.
ISO_C_BINDING is just a module that provides some constants, some types and some procedures. It happens to be one that is specified by the standard and supplied by the compiler vendor (it is an intrinsic module), but otherwise it has no special powers.
这些常量,类型和过程都可以帮助Fortran程序员以可移植的方式创建与C语言对应的数据对象,数据指针和过程指针.其中一些类型和过程很特殊-程序员不一定必须通过编写自己的Fortran代码来创建它们.
Those constants, types and procedures all help a Fortran programmer create data objects, data pointers and procedure pointers that are compatible with their C counterparts, in a portable way. Some of the the types and procedures are special - in that the programmer cannot necessarily create them by writing their own Fortran code.
C互操作性比使用固有模块要多得多.将USE ISO_C_BINDING放在作用域的顶部不会单独更改任何内容,只会使某些标识符可访问.
There is much more to C interoperability than the use of the intrinsic module. Putting USE ISO_C_BINDING at the top of a scope doesn't, on its own, change anything, bar making certain identifiers accessible.
BIND(C)属性必须应用于需要在C和Fortran之间进行互操作的变量,派生类型,通用块和过程.在某些情况下,此属性还指定事物的绑定名称(C名称).对于派生类型和通用类型,这可能会更改组件的对齐方式和顺序,而对于过程,则可能会更改过程的调用约定的各个方面.
The BIND(C) attribute must be applied to variables, derived types, common blocks and procedures that need to be interoperable between C and Fortran. In some cases, this attribute also specifies the binding name (C name) for the thing. For derived types and common this might change the alignment and ordering of components, while for procedures it might change aspects of the procedure's calling convention.
程序员还必须遵循有关可互操作的数据对象和过程的性质的一组要求.
There are also a set of requirements around the nature of data objects and procedures that are interoperable, that the programmer must follow.
(链接的fortran Wiki页面中的不幸..."注释似乎是对在Fortran中分配字符数组的工作方式的一种误解-它与C互操作性本身无关,除非要求它与C互操作性有关.是Fortran中可与C字符数组互操作的LEN = 1个字符数组.)
(The "Unfortunately..." comment in the linked fortran wiki page seems to be a misunderstanding about how assignment to a character array works in Fortran - it has nothing to do with C interoperability per se, bar the requirement that it is LEN=1 character arrays in Fortran that are interoperable with C char arrays.)
如果特定项目的目标编译器套件支持Fortran 2003的必要部分,那么我可以认为没有正当理由不使用Fortran 2003的C互操作性功能.使用此功能所产生的代码引人注目.
If the suite of targeted compilers for a particular project support the necessary parts of Fortran 2003, then I can think of no valid reason to not be using the C interoperability features of Fortran 2003. The improvement in robustness and portability of the resulting code that results from the use of this feature is compelling.
这篇关于为什么选择ISO_C_BINDING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!