Defining Types for Writing Interface in Fortran

Asked 2 years ago, Updated 2 years ago, 88 views

You need to write an FFI to call the following C function from Fortran

typedef structure{
  int x, y;
} int2;

int2f(int2a){
  int2b;
  b.x = 2 * a.x;
  b.y = 2 * a.y;
  return b;
}

I wrote the FFI to call this function as follows (test.f03)

module mymod
  type::int2
    integer::x,y
  end type
  interface
    function f(a)result(b)
      type(int2)::a,b
    end function
  end interface
end module

However, the compilation error is as follows.

 test.f03:8:16:

       type(int2)::a,b
                1
Error: Derived type 'int2' at (1) is being used before it is defined

Error messages can be read as using the int2 type before they are defined, but how (where) can I use int2 within the interface?

Compilers used:

$gfortran --version
GNU Fortran (GCC) 8.2.1 20181127
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

fortran

2022-09-30 19:46

1 Answers

I received a link, so I will answer myself.
https://stackoverflow.com/a/13858145

module mymod
  type::int2
    integer::x,y
  end type
  interface
    function f(a)result(b)
      import::int2!import explicitly
      type(int2)::a,b
    end function
  end interface
end module

You can resolve this by explicitly import as shown in .
This is a feature added in Fortran 2003, for example:
http://www.nag-j.co.jp/fortran/fortran2003/Fortran2003_8_6.html


2022-09-30 19:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.