字符串的 C 和 Fortran 互操作性 [英] C and Fortran interoperability for strings

查看:19
本文介绍了字符串的 C 和 Fortran 互操作性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 C 调用一些 Fortran 代码,但我没有找到传递 C 字符数组的正确方法.

I am trying to call some Fortran code from C but I didn't find the proper way of passing a C char array.

   SUBROUTINE My_F_Code (c_message)  BIND(C, NAME='my_f_code')
     USE ISO_C_BINDING
     IMPLICIT NONE
     CHARACTER*(C_CHAR)      c_message
     CHARACTER*(256)         f_message
     CALL C_F_POINTER( C_LOC(c_message), f_message)
     WRITE(*,*) f_message,LEN(f_message)
   END

BIND(C) 强制参数 c_message 的大小为 1.如何访问 c_message 字符串的其他元素?

The BIND(C) force the parameter c_message to be size 1. How can I access to other elements of the c_message string?

编译器:GCC 4.8.2

Compiler : GCC 4.8.2

推荐答案

我试图从 C 中调用一些 Fortran77 代码,但我没有找到传递 C 字符数组的正确方法.

I am trying to call some Fortran77 code from C but I didn't find the proper way of passing a C char array.

我曾经使用过的所有 Fortran 77 实现都提供了与 C 互操作的特定于实现的机制.通常这些涉及 C 端了解 Fortran 编译器采用的名称修改和参数传递约定,并在界面.GCC 系统的约定并不难使用.

All Fortran 77 implementations I've ever used have afforded implementation-specific mechanisms for interoperation with C. Generally these involve the C side knowing the name-mangling and argument-passing conventions employed by the Fortran compiler, and using them at the interface. The GCC system's conventions are not that hard to use.

不过,您似乎对 Fortran 2003 中引入的 Fortran/C 互操作工具感兴趣.假设您的 Fortran 77 代码也符合 Fortran 2003(或可以这样做),则应该可以在 Fortran 2003 中编写可互操作的包装器.但是请注意,C 互操作工具不提供(直接)用于 Fortran 变量或类型为 character 且长度大于 1 或类型不同于 c_char 的子程序参数的互操作性.另一方面,请记住,Fortran 字符对象的 length 与字符数组的 dimension(s) 不同.

You seem to be interested in the Fortran/C interop facility introduced in Fortran 2003, however. Supposing that your Fortran 77 code also conforms with Fortran 2003 (or can be made to do so), it should be possible to write an interoperable wrapper in Fortran 2003. Be aware, however, that the C interop facility does not provide (directly) for interoperability of Fortran variables or subprogram parameters of type character with length greater than 1 or kind different from c_char. On the other hand, keep also in mind that the length of a Fortran character object is not the same thing as the dimension(s) of a character array.

您有几个可互操作的替代方案来提供面向 C 的接口,通过该接口接受 C char 数组.也许最清楚的方法是接受一个 Fortran 假定大小的 character 数组:

You have a couple of interoperable alternatives for providing a C-facing interface by which to accept a C char array. Perhaps the clearest one would be to accept a Fortran assumed-size character array:

SUBROUTINE My_F_Code (c_message)  BIND(C, NAME='my_f_code')
    USE ISO_C_BINDING
    IMPLICIT NONE
    CHARACTER(kind=C_CHAR), dimension(*), intent(in) :: c_message
    ! ...
END

最可能的选择是直接接受 C 的数组指针:

The most likely alternative is to accept C's array pointer directly:

SUBROUTINE My_F_Code (c_message)  BIND(C, NAME='my_f_code')
    USE ISO_C_BINDING
    IMPLICIT NONE
    type(C_PTR), value :: c_message
    ! ...
END

如果 C 端数组指针可能为空,则需要后者.如果不能依赖数组以空值终止,则两者都需要传递显式长度.

The latter is necessary if the C-side array pointer might be null. Both require an explicit length to be passed as well if the array cannot be relied upon to be null terminated.

无论如何,如果您最终想要一个长度大于 1 的 Fortran character 变量(而不是维度大于 1 的数组),那么 interoperable接口不能直接提供——这些类型不属于 C 互操作规定适用的类型.除非您可以依赖默认字符类型为 c_char,否则您需要将它们与 copy-in(/copy-out) 结合起来,以便在类型之间转换字符.使用前一个接口,如何将数组复制到长度大于 1 的 Fortran 标量 character 应该很明显.对于指针变体,使用类似这样的转换函数可能很有用:

In any event, if you ultimately want a Fortran character variable having length greater than 1 (as opposed to an array having dimension greater than 1), then the interoperable interfaces can't provide that directly -- such types are not among those to which the C interop provisions are applicable. Unless you can rely on the default character kind to be c_char, you'll need to couple them with copy-in(/ copy-out) in order to convert the characters between kinds. With the former interface, it should be obvious how you could copy the array to a Fortran scalar character having length greater than 1. For the pointer variant, it might be useful to use a conversion function something like this:

subroutine C_string_ptr_to_F_string(C_string, F_string)
    use ISO_C_BINDING
    type(C_PTR), intent(in) :: C_string
    character(len=*), intent(out) :: F_string
    character(len=1, kind=C_CHAR), dimension(:), pointer :: p_chars
    integer :: i
    if (.not. C_associated(C_string)) then
      F_string = ' '
    else
      call C_F_pointer(C_string, p_chars, [huge(0)])
      do i = 1, len(F_string)
        if (p_chars(i) == C_NULL_CHAR) exit
        F_string(i:i) = p_chars(i)
      end do
      if (i <= len(F_string)) F_string(i:) = ' '
    end if
end subroutine

(源自 Fortran Wiki 的 C_interface_moduleC_F_string_ptr子程序)

(Derived from the Fortran Wiki's C_interface_module's C_F_string_ptr subroutine)

另一方面,如果您可以(或无论如何)依赖默认字符类型为 c_char,那么您还有其他选择.您可以使字符数组(例如第一个示例中的参数)与默认类型和长度大于 1 的标量字符对象相关联.特别是,如果包装函数的虚拟参数是具有假定长度的标量字符,或者具有不超过数组元素数量的固定长度,那么您可以依靠参数关联将其与包装器中的字符数组相关联.换句话说,在这种情况下,您可以将数组作为实际参数传递.

On the other hand, if you can (or anyway do) rely on the default character kind to be c_char then you have an additional alternative. You can usefully cause a character array such as the parameter in the first example to be associated with a scalar character object of default kind and length greater than one. In particular, if a dummy argument of the wrapped function is a scalar character with assumed length, or with fixed length not exceeding the number of array elements, then you can rely on argument association to associate it with the character array in the wrapper. In other words, in that case you can just pass the array as the actual argument.

这篇关于字符串的 C 和 Fortran 互操作性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆