从C调用FORTRAN子例程

时间:2011-11-21 06:19:24

标签: c fortran fortran-iso-c-binding

我试图从C

调用FORTRAN函数

我的问题是:

  1. 如果fortRoutine是我的fortran子程序的名称,那么我从C调用它 为fortRoutine_。如果fortRoutine只包含一个字符数组参数,那么我可以像这样传递:

    fortRoutine_("I am in fortran");
    
  2. 在调用FORTRAN子例程时,何时应该使用pass by值和何时通过引用传递?

  3. 由于我是C的新手,我对此没有任何线索。如果可能的话,请提供一些很好的教程链接。

3 个答案:

答案 0 :(得分:22)

现在这样做的方法是在Fortran端使用Fortran ISO C Binding。这是Fortran 2003语言标准的一部分,可在许多编译器中使用;它不是特定于gcc。本网站上的许多答案都有描述。作为语言标准的一部分,它与编译器和平台无关。而且您不需要了解编译器的内部传递约定。当在Fortran子例程或函数的声明中使用时,ISO C绑定会导致Fortran编译器使用C调用约定,以便可以直接从C调用该过程。您不需要添加隐藏的参数或名称Fortran子例程名称,即没有下划线。链接器使用的名称来自“绑定”选项。

字符串是一个很难的案例,因为在C语言中它们是字符数组,你必须在Fortran中匹配它。您还必须处理字符串的不同定义:C为空终止,Fortran固定长度并填充空格。该示例显示了这是如何工作的。数字更容易。数组的唯一问题是C是row-major和Fortran column-major,因此多维数组被转置。

int main ( void ) {

   char test [10] = "abcd";

   myfortsub (test);

   return 0;

}

subroutine myfortsub ( input_string ) bind ( C, name="myfortsub" )

   use iso_c_binding, only: C_CHAR, c_null_char
   implicit none

   character (kind=c_char, len=1), dimension (10), intent (in) :: input_string
   character (len=10) :: regular_string
   integer :: i

   regular_string = " "
   loop_string: do i=1, 10
      if ( input_string (i) == c_null_char ) then
         exit loop_string
      else
         regular_string (i:i) = input_string (i)
      end if
   end do loop_string

   write (*, *) ">", trim (regular_string), "<", len_trim (regular_string)

   return

end subroutine myfortsub

您将C编译为目标文件并使用gfortran编译fortran并链接两者:

gcc-mp-4.6   \
         -c  \
         test_fortsub.c

gfortran-mp-4.6   \
     test_fortsub.o  \
     myfortsub.f90  \
     -o test_fortsub.exe

输出是:

 >abcd<           4

答案 1 :(得分:3)

当然这完全取决于您的FORTRAN编译器,但一般来说:

  1. 不,您需要为字符串传递隐藏的长度参数。一些编译器直接在字符串之后将这些与其他参数交织。其他,将所有字符串长度参数分组在参数列表的末尾。

    char str[11] = {0};
    fortranFunc_(str, sizeof(str) - 1);
    // remember that 'str' will need to be null terminated
    // and will be padding with spaces to fit the length
    // so for C passing strings to Fortran specify the length
    // less 1 so you can add a nul terminator, and on all strings
    // being filled in by FORTRAN, trim-end all spaces.
    
  2. 几乎总是通过引用传递,但您可以使用FORTRAN端伪参数上的属性来切换此行为。

    int value = 10;
    fortranFunc_(&value);
    // INTEGER I
    
  3. 以下是一些适用于各种编译器的参考资料:

答案 2 :(得分:2)

答案取决于编译器和系统(技术上,它的ABI)。对于GCC(它是C,C ++,Ada和Fortran编译器),请阅读fortran mixed programming章节。

相关问题