简体   繁体   English

如何使用Fortran接口调用包含用户定义类型的C函数

[英]How to use Fortran interface to call a C function which contains user defined type

In fact, I want to call magma from fortran. 实际上,我想从fortran中调用岩浆。 So I add magma.lib and create an interface to use the C fuction of magma: 因此,我添加magma.lib并创建一个接口来使用magma的C功能:

Interface
      Integer function magma_dpotrf(uplo, n, a, lda, info) BIND (C, NAME="magma_dpotrf")
        use iso_c_binding
        Implicit none
        !character (c_char), value :: uplo????
        integer (c_int), value ::n
        real (c_double) ::a(*)
        integer (c_int), value ::lda
        integer (c_int)::info
      end function
   end Interface

But the parameter uplo is a user defined type In C code (magma_uplo_t uplo): 但是参数uplo是用户在C代码中定义的类型(magma_uplo_t uplo):

typedef enum {
    MagmaUpper         = 121,
    MagmaLower         = 122,
    MagmaUpperLower    = 123,
    MagmaFull          = 123,  /* lascl, laset */
    MagmaHessenberg    = 124   /* lascl */
} magma_uplo_t;

magma_int_t
magma_dpotrf(
    magma_uplo_t uplo, magma_int_t n,
    double *A, magma_int_t lda,
    magma_int_t *info);

magma_int_t = int, Does anyone knows how to create interface for it? magma_int_t = int,有人知道如何为其创建接口吗? Thanks in advance 提前致谢

magma_uplo_t is an enumeration. magma_uplo_t是一个枚举。 There is some support for them in Fortran 2003 but you can quite safely assume it is an integer(c_int) which can take the values from 121 to 124. And in your case it is passed by value. 在Fortran 2003中对此有一些支持,但是您可以完全放心地假定它是一个integer(c_int) ,它可以接受从121到124之间的值。在您的情况下,它是按值传递的。

integer(c_int), value :: uplo

You can actually create the constants using a Fortran 2003 enumeration 您实际上可以使用Fortran 2003枚举创建常量

 enum, bind( C )
    enumerator :: MagmaUpper         = 121, &
                  MagmaLower         = 122, &
                  MagmaUpperLower    = 123, &
                  MagmaFull          = 123, &
                  MagmaHessenberg    = 124
  end enum

but the variable and then you can also try integer(kind=kind(MagmaUpper)) to be completely safe. 但是变量,然后您也可以尝试使用integer(kind=kind(MagmaUpper))来确保安全。 This will survive things like the GCC's --short-enums option. 这样可以在GCC的--short-enums选项之类的东西中幸存下来。

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

相关问题 Fortran接口以调用返回指针的C函数 - Fortran interface to call a C function that return a pointer 通过C函数定义的Fortran派生类型构造函数(II) - Fortran derived type constructor defined though C function (II) 通过C函数定义的Fortran派生类型构造函数 - Fortran derived type constructor defined though C function 如何正确定义和使用返回类型为用户定义结构的函数? (在C中) - How do I properly define and use a function whose return type is a user defined structure? (In C) 如何调用在yocto的另一层中定义的C函数? - how to call a C function which is defined in another layer of yocto? 如何处理分段错误(memory Leak) - How to Approach Segmentation Fault ( memory Leak ) Fortran function with C Interface? 用户定义的函数,相当于C中的strlen() - A user defined function which is equivalent of strlen() in C 如何在c中调用类型为static pyobject的c函数 - how to call c function which is of type static pyobject inside c 哪些 C 规则允许在函数声明器中使用以前定义为类型的标识符? - Which C rules allow to use in function declarator an identifier, which was previously defined as a type? c中用户定义函数的冲突类型 - confliction type for a user defined function in c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM