Fortran,C ++ API的异常行为

时间:2019-04-16 07:37:59

标签: c++ c fortran

我已经为我的C ++代码开发了一个Fortran API。从Fortran,我将一个字符串传递给绑定的C ++函数,但是接收到的字符串是非常意外的。

我的API在以下模块(api.f)中:

    module API

          use, intrinsic          :: ISO_C_Binding, only: C_double, c_char
          use, intrinsic          :: ISO_C_Binding, only: C_int
          use, intrinsic          :: ISO_C_Binding, only: C_ptr, C_NULL_ptr


          implicit none

    !     - Type object which binds to class object                                                                                      

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

    !------------------------                                                                                                                      
    ! C function declarations                                                                                                                      
    !------------------------                                                                                                                      

          interface

             function  newcase_c (path, lenpath) result(this)
         &        bind(C, name="newcase")
             import

             type(C_ptr)                                    :: this
             character(kind=c_char)                         :: path(*)
             integer(kind=c_int)                            :: lenpath
             end function newcase_c
          end interface
      public     :: newcase_c, walldistance_c, releasemem_c
      public     :: dp_t

      CONTAINS

! Fortran wrapper routines to interface with C_wrappers                                                                                        

      subroutine newcase(this, path)

      type(dp_t), intent(out)        :: this
      character(:, kind=c_char),
     &     allocatable                     :: path
      integer(kind=c_int)                  :: sizepath

      write(*,*) "Trim Path: ", path, ":"
      write(*,*) "Len      : ", len(path)
      sizepath = len(path)
      this% object = newcase_c(path, sizepath)

      end subroutine newcase

    end module API

我在newcase中的某处致电main.f,如下所示:

      character(256, kind=c_char)   :: cwd
      character(:, kind=c_char)
     & , allocatable                :: trimpath
      type(dp_t)              :: dc

      call GETCWD(cwd)
      trimpath = trim(cwd)

      call newcase(dynacase, trimpath)

在我的.hpp文件中,我声明了C函数。注意DP是class

#ifdef __cplusplus
extern "C" {
#endif
    DP * newcase(char *dir, int lenpath);
#ifdef __cplusplus
}
#endif

在我的.cpp中,将函数newcase定义为:

DP * newcase(char *dir, int lenpath){
    std::string path = "";
    for(int i=0; i<lenpath; i++){
        path += dir[i];
    }
    std::cout << "Received path :" << path << ":" << std::endl;
    fs::path runDir(path);
    return new DP(runDir);
}

问题在于,有时newcase(。cpp)接收到的字符串是预期的:

Trim Path: /Users/abcdef/test_cases:
 Len      :           24
Received string /Users/abcdef/test_cases:

有时候这是完全出乎意料的:

Trim Path: /Users/abcdef/test_cases:
 Len      :           24
Received string /Users/abcdef/test_casesG.stl:

我的直觉是,字符串的空终止可能与此有关,我不确定如何解决该问题。我该如何解决?更清楚地说,为什么*dir(.cpp)与path中的newcase_c(.f)不同?

建议的重复项是:

  

ISO_C_BINDING Calling C routine from Fortran (with doubles and arrays)

尽管那里提出的解决方案解决了我的问题(因为我可以提取正确的字符串长度),但它没有讨论为什么我的C ++库收到的字符数组与Fortran传递的字符数组不同。

0 个答案:

没有答案