通过C函数定义的Fortran派生类型构造函数 [英] Fortran derived type constructor defined though C function
问题描述
我想通过C函数定义派生类型构造函数。在下面的例子中,我设法通过C接口定义一个重载的加法运算符。尽管语法非常相似,但构造函数的定义在gfortran 4.9下失败,出现以下错误:
I want to define a derived type constructor through a C function. In the following example, I managed to defined a overloaded addition operator through a C interface. Altough the syntax is quite similar, the definition of the constructor fails under gfortran 4.9 with the following error:
test.f90:7.52:
module procedure my_module_fortran_new_my_double
1
Error: 'my_module_fortran_new_my_double' at (1) is not a module procedure
经过一段时间的谷歌搜索或者在堆栈溢出的前一篇文章,我没有找到任何解决方案。如果有人能告诉我是什么触发了这个错误以及如何纠正错误,我会非常高兴。
After some time spend in googling or looking at previous post at stack overflow, I did not find any solution. I would be very pleased if someone could tell me what triggers this error and how to correct it.
以下是我的模块的源代码:
Here is the source code of my module:
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
推荐答案
外部过程不是模块过程。我想这就是你想要的:
An external procedure isn't a module procedure. I think this is what you want:
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
这篇关于通过C函数定义的Fortran派生类型构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!