Why ISO_C_BINDING

China☆狼群 提交于 2020-01-24 03:27:13

问题


I am working on some fortran-calling-C code and am unclear about the use of the iso_c_binding module.

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)

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.

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").


回答1:


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 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.

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.

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.

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.

(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.)

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.




回答2:


The ISO_C_Binding is part of the language standard and makes the interface between Fortran C standard and portable. Figuring out how interfacing works between a specific pair of compilers on a specific OS is not portable.



来源:https://stackoverflow.com/questions/19524714/why-iso-c-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!