简体   繁体   中英

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

In fact, I want to call magma from fortran. So I add magma.lib and create an interface to use the C fuction of magma:

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):

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? Thanks in advance

magma_uplo_t is an enumeration. 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.

integer(c_int), value :: uplo

You can actually create the constants using a Fortran 2003 enumeration

 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. This will survive things like the GCC's --short-enums option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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