Fortran派生类型构造函数通过C函数定义

时间:2016-12-21 20:42:08

标签: c binding fortran

我想通过C函数定义派生类型构造函数。在下面的示例中,我设法通过C接口定义了一个重载的加法运算符。尽管语法非常相似,但构造函数的定义在gfortran 4.9下失败,并出现以下错误:

module my_module
  use iso_c_binding
  type, bind(c) :: my_double
     real(c_double) :: x
  end type my_double
  interface my_double
    module procedure my_module_fortran_new_my_double
  end interface
  interface operator (+)
     module procedure my_module_fortran_add
  end interface operator (+)
  interface
     type(my_double) function my_module_fortran_new_my_double (v) bind ( c )
       use iso_c_binding
       import :: my_double
       real (c_double), intent(in) :: v
     end function my_module_fortran_new_my_double
     type(my_double) function my_module_fortran_add (v1,v2) bind ( c )
       use iso_c_binding
       import :: my_double
       type (my_double), intent(in) :: v1,v2
     end function my_module_fortran_add
  end interface
end module my_module

经过一段时间在google搜索或查看堆栈溢出时的上一篇文章后,我找不到任何解决方案。如果有人能告诉我是什么触发了这个错误以及如何纠正它,我会很高兴。

以下是我的模块的源代码:

RecurringJob.AddOrUpdate(() => GetCurrentUserNotifications("User1"), Cron.MinuteInterval(30));

    public void GetCurrentUserNotifications(string userId)
    {
        _connectionManager.GetHubContext<NotificationsHub>()
            .Clients.All.broadcastNotifications(_repository.GetNotifications()
            .Where(x => x.DateTime == DateTime.Now && x.CreatedBy == userId));
    }

1 个答案:

答案 0 :(得分:2)

外部程序不是模块程序。我想这就是你想要的:

module my_module
  use iso_c_binding
  type, bind(c) :: my_double
     real(c_double) :: x
  end type my_double

    interface
       type(my_double) function my_module_fortran_new_my_double (v) bind ( c )
         use iso_c_binding
         import :: my_double
         real (c_double), intent(in) :: v
       end function my_module_fortran_new_my_double
       type(my_double) function my_module_fortran_add (v1,v2) bind ( c )
         use iso_c_binding
         import :: my_double
         type (my_double), intent(in) :: v1,v2
       end function my_module_fortran_add
  end interface

  interface my_double
   procedure my_module_fortran_new_my_double
  end interface
  interface operator (+)
    procedure :: my_module_fortran_add
  end interface operator (+)

end module my_module