简体   繁体   English

如何将Vector从C ++传递给Fortran?

[英]How can I pass a Vector from C++ to Fortran?

I have a code in C++ and I would like to call some functions in Fortran. 我有C ++代码,我想在Fortran中调用某些函数。 I have the following function 我有以下功能

Vector3d CWLiDAR__get_position(CWLiDAR* This)
{
    return This->get_position();
}

There is a wrapper in Fortran: Fortran中有一个包装器:

module CWLiDAR_module
    use, intrinsic :: ISO_C_Binding
    implicit none

    private
    type CWLiDAR_type
        private
        type(C_ptr) :: object = C_NULL_ptr
    end type CWLiDAR_type

    interface    
        function C_CWLiDAR__get_position (this)  result(pos)  bind(C, name="CWLiDAR__get_position")
            import
            type(C_ptr), value                          :: this
            real(C_double), intent(out), dimension(*)   :: pos
        end function C_CWLiDAR__get_position
    end interface

    interface get_position
        module procedure CWLiDAR__get_position
    end interface get_position


    public :: get_position, CWLiDAR_type


    contains

    function CWLiDAR__get_position(this) result(pos)
        type(CWLiDAR_type), intent(in)                  :: this
        double precision, dimension(0:3)                :: pos

        pos = C_CWLiDAR__get_position(this%object)
    end function CWLiDAR__get_position

end module CWLiDAR_module

However I am getting the following compilation error: 但是我收到以下编译错误:

     function C_CWLiDAR__get_position (this)  result(pos)  bind(C, name="CWL
                                                            1
Error: Assumed size array at (1) must be a dummy argument
         function C_CWLiDAR__get_position (this)  result(pos)  bind(C, name="CWL
        1
Error: Assumed size array at (1) must be a dummy argument

How can I pass a Vector3D to Fortran? 如何将Vector3D传递给Fortran?

Functions which return arrays are NOT interoperable with C. You cannot call such a function from Fortran. 返回数组的函数不能与C互操作。您不能从Fortran调用此类函数。 At least not portably using bind(C) . 至少不是可移植地使用bind(C)

If it was possible (but it isn't!) the syntax would have to be 如果有可能(但不是!),语法必须是

   interface    
        function C_CWLiDAR__get_position (this)  result(pos)  bind(C, name="CWLiDAR__get_position")
            import
            type(C_ptr), value                          :: this
            real(C_double),  dimension(some_number)   :: pos
        end function C_CWLiDAR__get_position
    end interface

The compiler is complaining that neither intent , nor dimension(*) is allowed for function results in Fortran. 编译器抱怨Fortran中的函数结果既不允许使用intent ,也不允许使用dimension(*) That is the reason for the error message. 这就是错误消息的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM